Skip to content

Commit

Permalink
Use compiler error instead of crash when a this. reference is not in …
Browse files Browse the repository at this point in the history
…the trait.

Resolves #1878.
  • Loading branch information
jemc committed May 3, 2017
1 parent 2f1405b commit 5187555
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/libponyc/pass/refer.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,20 @@ static bool refer_this_dot(pass_opt_t* opt, ast_t* ast)
ast_t* def = ast_get(ast, name, &status);
ast_setdata(ast, (void*)def);

// If nothing was found, we fail, but also try to suggest an alternate name.
if(def == NULL)
{
const char* alt_name = suggest_alt_name(ast, name);

if(alt_name == NULL)
ast_error(opt->check.errors, ast, "can't find declaration of '%s'", name);
else
ast_error(opt->check.errors, ast,
"can't find declaration of '%s', did you mean '%s'?", name, alt_name);

return false;
}

switch(ast_id(def))
{
case TK_FVAR:
Expand Down
14 changes: 14 additions & 0 deletions test/libponyc/badpony.cc
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,17 @@ TEST_F(BadPonyTest, MatchCasePatternConstructorTooFewArguments)

TEST_ERRORS_1(src, "not enough arguments");
}

TEST_F(BadPonyTest, ThisDotWhereDefIsntInTheTrait)
{
// From issue #1878
const char* src =
"trait T\n"
" fun foo(): USize => this.u\n"

"class C is T\n"
" var u: USize = 0\n";

TEST_ERRORS_1(src,
"can't find declaration of 'u'");
}

0 comments on commit 5187555

Please sign in to comment.