Skip to content

Commit

Permalink
Fix assertion error on iftype inside lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
ergl authored and SeanTAllen committed May 27, 2021
1 parent 847c834 commit 29431e4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .release-notes/3763.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Fix "iftype" expressions not being usable in lambdas or object literals

This release fixes an issue where the compiler would hit an assertion error when compiling programs that placed `iftype` expressions inside lambdas or object literals.
6 changes: 6 additions & 0 deletions src/libponyc/pass/scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ static ast_result_t scope_iftype(pass_opt_t* opt, ast_t* ast)
pony_assert(ast_id(ast) == TK_IFTYPE);

AST_GET_CHILDREN(ast, subtype, supertype, body, typeparam_store);
// Prevent this from running again, if, for example, the iftype
// occurs inside an object literal (or lambda). In those cases,
// a "catch up" will take place and the compiler will try to run
// this pass again.
if(ast_id(typeparam_store) != TK_NONE)
return AST_IGNORE;

ast_t* typeparams = ast_from(ast, TK_TYPEPARAMS);

Expand Down
21 changes: 21 additions & 0 deletions test/libponyc/iftype.cc
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,24 @@ TEST_F(IftypeTest, ReplaceTypeargs)
" new create(env: Env) => None";
TEST_COMPILE(src);
}

TEST_F(IftypeTest, InsideLambda)
{
// From #3762
const char* src =
"primitive DoIt[T: (U8 | I8)]\n"
" fun apply(arg: T): T =>\n"
" let do_it = {(v: T): T =>\n"
" iftype T <: U8 then\n"
" v\n"
" else\n"
" v\n"
" end\n"
" }\n"
" do_it(arg)\n"

"actor Main\n"
" new create(env: Env) => None";

TEST_COMPILE(src);
}

0 comments on commit 29431e4

Please sign in to comment.