Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix compiler crash when an if block ends with an assignment that has no result value. #3670

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/issue-3669.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Fix compiler crash when an if block ends with an assignment that has no result value

This release fixes a compiler crash that used to occur when certain kinds of assignment expressions that have no logical result value were used as the last expression in an `if` block, and possibly other control flow constructs as well. Now the compiler makes sure that the code generation for those cases always bears a value, even though the type system guarantees that the value will never be used in such a case. This prevents generating invalid LLVM IR blocks that have no proper terminator instruction.
13 changes: 9 additions & 4 deletions src/libponyc/codegen/genoperator.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,10 @@ static LLVMValueRef assign_rvalue(compile_t* c, ast_t* left, ast_t* r_type,
case TK_EMBEDREF:
{
// Do nothing. The embed field was already passed as the receiver.
return GEN_NOVALUE;
// But we can't return GEN_NOVALUE - that's reserved only for branches
// that "jump away". So we just return the r_value we were given.
// The type system ensures that nobody will be able to use this value.
return r_value;
}

case TK_VARREF:
Expand All @@ -501,9 +504,11 @@ static LLVMValueRef assign_rvalue(compile_t* c, ast_t* left, ast_t* r_type,
case TK_DONTCAREREF:
case TK_MATCH_DONTCARE:
{
// Do nothing. The type checker has already ensured that nobody will try
// to use the result of the assignment.
return GEN_NOVALUE;
// Do nothing.
// But we can't return GEN_NOVALUE - that's reserved only for branches
// that "jump away". So we just return the r_value we were given.
// The type system ensures that nobody will be able to use this value.
return r_value;
}

case TK_TUPLE:
Expand Down
43 changes: 43 additions & 0 deletions test/libponyc/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -946,3 +946,46 @@ TEST_F(CodegenTest, TryThenClauseContinueNested)
ASSERT_TRUE(run_program(&exit_code));
ASSERT_EQ(exit_code, 42);
}

TEST_F(CodegenTest, IfBlockEndingWithEmbedAssign)
{
// From issue #3669
const char* src =
"class B\n"
"class A\n"
" embed embedded: B\n"
" new create() => \n"
" if true\n"
" then embedded = B\n"
" else embedded = B\n"
" end\n"

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

TEST_COMPILE(src);

// LLVM Module verification fails if bug is present:
int exit_code = 0;
ASSERT_TRUE(run_program(&exit_code));
}

TEST_F(CodegenTest, IfBlockEndingWithDontCareAssign)
{
// From issue #3669
const char* src =
"class B\n"

"actor Main\n"
" new create(env: Env) =>\n"
" if true\n"
" then _ = B\n"
" end";

TEST_COMPILE(src);

// LLVM Module verification fails if bug is present:
int exit_code = 0;
ASSERT_TRUE(run_program(&exit_code));
}