Skip to content

Commit

Permalink
Fix bug where a > 0 ? f() : g() could cause a compiler crash if bot…
Browse files Browse the repository at this point in the history
…h returned `void!`.
  • Loading branch information
lerno committed Nov 10, 2024
1 parent acab957 commit 746046c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changes / improvements

### Fixes
- Fix bug where `a > 0 ? f() : g()` could cause a compiler crash if both returned `void!`.

### Stdlib changes

Expand Down
3 changes: 2 additions & 1 deletion src/compiler/llvm_codegen_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -4979,11 +4979,12 @@ void gencontext_emit_ternary_expr(GenContext *c, BEValue *value, Expr *expr)
return;
}

if (expr->type == type_void)
if (type_lowering(expr->type) == type_void)
{
llvm_value_set(value, NULL, expr->type);
return;
}

llvm_new_phi(c, value, "val", expr->type, lhs_value, lhs_exit, rhs_value, rhs_exit);
}
static LLVMValueRef llvm_emit_real(LLVMTypeRef type, Float f)
Expand Down
71 changes: 71 additions & 0 deletions test/test_suite/errors/ternary_void_fault.c3t
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// #target: macos-x64
module test;
fn void! x() {}
fn void main()
{
int a;
@catch(a > 0 ? x() : x());
}

/* #expect: test.ll

define void @test.main() #0 {
entry:
%a = alloca i32, align 4
%blockret = alloca i64, align 8
%f = alloca i64, align 8
store i32 0, ptr %a, align 4
br label %testblock

testblock: ; preds = %entry
%0 = load i32, ptr %a, align 4
%gt = icmp sgt i32 %0, 0
br i1 %gt, label %cond.lhs, label %cond.rhs

cond.lhs: ; preds = %testblock
%1 = call i64 @test.x()
%not_err = icmp eq i64 %1, 0
%2 = call i1 @llvm.expect.i1(i1 %not_err, i1 true)
br i1 %2, label %after_check, label %assign_optional

assign_optional: ; preds = %cond.lhs
store i64 %1, ptr %f, align 8
br label %end_block

after_check: ; preds = %cond.lhs
br label %cond.phi

cond.rhs: ; preds = %testblock
%3 = call i64 @test.x()
%not_err1 = icmp eq i64 %3, 0
%4 = call i1 @llvm.expect.i1(i1 %not_err1, i1 true)
br i1 %4, label %after_check3, label %assign_optional2

assign_optional2: ; preds = %cond.rhs
store i64 %3, ptr %f, align 8
br label %end_block

after_check3: ; preds = %cond.rhs
br label %cond.phi

cond.phi: ; preds = %after_check3, %after_check
store i64 0, ptr %f, align 8
br label %end_block

end_block: ; preds = %cond.phi, %assign_optional2, %assign_optional
%5 = load i64, ptr %f, align 8
%neq = icmp ne i64 %5, 0
br i1 %neq, label %if.then, label %if.exit

if.then: ; preds = %end_block
%6 = load i64, ptr %f, align 8
store i64 %6, ptr %blockret, align 8
br label %expr_block.exit

if.exit: ; preds = %end_block
store i64 0, ptr %blockret, align 8
br label %expr_block.exit

expr_block.exit: ; preds = %if.exit, %if.then
ret void
}

0 comments on commit 746046c

Please sign in to comment.