Skip to content

Commit

Permalink
WIP fix for issue 2508 with debug
Browse files Browse the repository at this point in the history
I couldn't come up with a case that went through else "Giving up".

Here is the output:
```
$ ./build/debug/ponyc minimal-cases/issue-2508/
Building builtin -> /home/wink/prgs/pony/ponyc/packages/builtin
Building minimal-cases/issue-2508/ -> /home/wink/prgs/pony/ponyc/minimal-cases/issue-2508
suggest_alt_name:+ name=doit suggest_alt_name:  ast (reference (id doit))
suggest_alt_name:  case_ast (fun:scope box (id doIt) x x (nominal (id $0) (id None) x val x x) x (seq (reference (id None)) (reference (id None))) x)
suggest_alt_name:  first box
suggest_alt_name:  second (id doIt)
suggest_alt_name:  Using second, tk=8
suggest_alt_name:- name=doit try_name=doIt suggest_alt_name:  ast (reference (id doit))
suggest_alt_name:+ name=astr suggest_alt_name:  ast (reference (id astr))
suggest_alt_name:  case_ast (fvar (id aStr) (nominal (id $0) (id String) x val x x) x)
suggest_alt_name:  first (id aStr)
suggest_alt_name:  second (nominal (id $0) (id String) x val x x)
suggest_alt_name:  Using first, tk=8
suggest_alt_name:- name=astr try_name=aStr suggest_alt_name:  ast (reference (id astr))
Error:
/home/wink/prgs/pony/ponyc/minimal-cases/issue-2508/main.pony:5:5: can't find declaration of 'doit', did you mean 'doIt'?
    doit() // crash compiler
    ^
Error:
/home/wink/prgs/pony/ponyc/minimal-cases/issue-2508/main.pony:10:5: can't find declaration of 'astr', did you mean 'aStr'?
    astr
    ^
```
  • Loading branch information
winksaville committed Feb 14, 2018
1 parent 61a7a2e commit 2de3ff3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
14 changes: 14 additions & 0 deletions minimal-cases/issue-2508/main.pony
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
actor Main
var aStr: String = "help"

new create(env: Env) =>
doit() // crash compiler
//doIt() // OK
aStr = xx()

fun xx(): String =>
astr
//aStr // OK

fun doIt() =>
None
1 change: 0 additions & 1 deletion src/libponyc/ast/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,6 @@ void ast_fprint(FILE* fp, ast_t* ast, size_t width)
return;

print(fp, ast, 0, NOT_SPECIAL, width);
fprintf(fp, "\n");
}

void ast_fprintverbose(FILE* fp, ast_t* ast)
Expand Down
79 changes: 73 additions & 6 deletions src/libponyc/pass/refer.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@
#include "ponyassert.h"
#include <string.h>

#include "../../libponyrt/dbg/dbg_util.h"

// Uncomment to enable
#define DBG_ENABLED true
#include "../../libponyrt/dbg/dbg.h"

// Uncomment to enable AST debug
#define DBG_AST_ENABLED true
#include "../ast/dbg_ast.h"

// Initialize debug context
static dbg_ctx_t* dc = NULL;
INITIALIZER(initialize)
{
if(dc == NULL)
{
dc = dbg_ctx_create_with_dst_file(32, stderr);
dbg_sb(dc, 0, 1);
}
}

// Finalize debug context
FINALIZER(finalize)
{
if(dc != NULL)
{
dbg_sb(dc, 0, 0);
dbg_ctx_destroy(dc);
}
dc = NULL;
}

/**
* Make sure the definition of something occurs before its use. This is for
* both fields and local variable.
Expand Down Expand Up @@ -194,6 +226,8 @@ static const char* suggest_alt_name(ast_t* ast, const char* name)
pony_assert(ast != NULL);
pony_assert(name != NULL);

DBG_PFE(dc, 0, "name=%s ", name); DBG_AST(dc, 0, ast);

size_t name_len = strlen(name);

if(is_name_private(name))
Expand All @@ -202,7 +236,10 @@ static const char* suggest_alt_name(ast_t* ast, const char* name)
const char* try_name = stringtab(name + 1);

if(ast_get(ast, try_name, NULL) != NULL)
{
DBG_PFX(dc, 0, "name=%s try_name=%s ", name, try_name); DBG_AST(dc, 0, ast);
return try_name;
}
}
else
{
Expand All @@ -213,26 +250,56 @@ static const char* suggest_alt_name(ast_t* ast, const char* name)
const char* try_name = stringtab_consume(buf, name_len + 2);

if(ast_get(ast, try_name, NULL) != NULL)
{
DBG_PFX(dc, 0, "name=%s try_name=%s ", name, try_name); DBG_AST(dc, 0, ast);
return try_name;
}
}

// Try with a different case (without crossing type/value boundary)
ast_t* case_ast = ast_get_case(ast, name, NULL);
if(case_ast != NULL)
{
DBG_AST(dc, 0, case_ast);

ast_t* id = case_ast;

if(ast_id(id) != TK_ID)
id = ast_child(id);
int tk = ast_id(id);
if(tk != TK_ID)
{
AST_GET_CHILDREN(case_ast, first, second);
DBG_AST(dc, 0, first);
DBG_AST(dc, 0, second);

pony_assert(ast_id(id) == TK_ID);
const char* try_name = ast_name(id);
if((tk = ast_id(first)) == TK_ID)
{
// First is a TK_ID give it as a suggestion
DBG_PFN(dc, 0, "Using first, tk=%d\n", tk);
id = first;
} else if((tk = ast_id(second)) == TK_ID) {
// Second is a TK_ID give it as a suggestion
DBG_PFN(dc, 0, "Using second, tk=%d\n", tk);
id = second;
} else {
// Giving up
DBG_PSN(dc, 0, "Giving up\n");
}
}

if(ast_get(ast, try_name, NULL) != NULL)
return try_name;
if(tk == TK_ID)
{
const char* try_name = ast_name(id);

if(ast_get(ast, try_name, NULL) != NULL)
{
DBG_PFX(dc, 0, "name=%s try_name=%s ", name, try_name); DBG_AST(dc, 0, ast);
return try_name;
}
}
}

// Give up
DBG_PFX(dc, 0, "name=%s ", name); DBG_AST(dc, 0, ast);
return NULL;
}

Expand Down

0 comments on commit 2de3ff3

Please sign in to comment.