Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix "iftype" expressions not being usable in lambdas or object literals #3763

Merged
merged 1 commit into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}