diff --git a/releasenotes.md b/releasenotes.md index b7d18e543..69d74ac0c 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -33,6 +33,7 @@ - Incorrectly using LLVMStructType when emitting dynamic functions on MachO #2666 - FixedThreadPool join did not work correctly. - Fix bug when creating bool vectors in certain cases. +- Compiler assert when passing returning CT failure immediately rethrown #2689. ### Stdlib changes - Add `ThreadPool` join function to wait for all threads to finish in the pool without destroying the threads. diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 447c0596d..9ce6cd516 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -2369,6 +2369,12 @@ static inline void llvm_emit_deref(GenContext *c, BEValue *value, Expr *inner, T break; } llvm_emit_expr(c, value, inner); + if (!c->current_block) + { + value->type = type_void; + *value = (BEValue) { .type = type_void, .kind = BE_VALUE, .value = NULL }; + return; + } llvm_value_rvalue(c, value); AlignSize alignment = type_abi_alignment(type); bool is_const = expr_is_const(inner); diff --git a/test/test_suite/errors/sure_fail_in_assign.c3t b/test/test_suite/errors/sure_fail_in_assign.c3t new file mode 100644 index 000000000..e4585e7df --- /dev/null +++ b/test/test_suite/errors/sure_fail_in_assign.c3t @@ -0,0 +1,48 @@ +// #target: macos-x64 +module test; +import std::io; + +faultdef OH_NO; +const int C = 1; + +fn void main() +{ + (void)do_thing(); +} + +fn void? do_thing() +{ + int* a_value; + // Change 'C' below to any integer value and the assert goes away. + *a_value = may_fault(C)!; + io::printfn("Value: %d", *a_value); +} + +macro may_fault($incoming) +{ + $switch $incoming: + $case 1: return OH_NO?; + $default: return 0; + $endswitch +} + +/* #expect: test.ll + +%"char[]" = type { ptr, i64 } + +@test.C = local_unnamed_addr constant i32 1, align 4 +@test.OH_NO = linkonce constant %"char[]" { ptr @test.OH_NO.nameof, i64 11 }, align 8 +@test.OH_NO.nameof = internal constant [12 x i8] c"test::OH_NO\00", align 1 + +define i64 @test.do_thing() #0 { +entry: + %a_value = alloca ptr, align 8 + %error_var = alloca i64, align 8 + store ptr null, ptr %a_value, align 8 + store i64 ptrtoint (ptr @test.OH_NO to i64), ptr %error_var, align 8 + br label %guard_block + +guard_block: ; preds = %entry + %0 = load i64, ptr %error_var, align 8 + ret i64 %0 +}