Codegen error in if (try x = (true ? io::EOF? : 1)), i.e. using if-try with a known Empty.

This commit is contained in:
Christoffer Lerno
2025-08-03 13:33:28 +02:00
parent 1c4f7a4b61
commit 9fe6c77d28
3 changed files with 34 additions and 5 deletions

View File

@@ -9,6 +9,7 @@
### Fixes ### Fixes
- List.remove_at would incorrectly trigger ASAN. - List.remove_at would incorrectly trigger ASAN.
- With avx512, passing a 512 bit vector in a union would be lowered incorrectly, causing an assert. #2362 - With avx512, passing a 512 bit vector in a union would be lowered incorrectly, causing an assert. #2362
- Codegen error in `if (try x = (true ? io::EOF? : 1))`, i.e. using if-try with a known Empty.
### Stdlib changes ### Stdlib changes
- Add `==` to `Pair`, `Triple` and TzDateTime. Add print to `Pair` and `Triple`. - Add `==` to `Pair`, `Triple` and TzDateTime. Add print to `Pair` and `Triple`.

View File

@@ -4263,18 +4263,26 @@ void llvm_emit_try_assign_try_catch(GenContext *c, bool is_try, BEValue *be_valu
// 6. If we haven't jumped yet, do it here (on error) to the catch block. // 6. If we haven't jumped yet, do it here (on error) to the catch block.
llvm_value_fold_optional(c, be_value); llvm_value_fold_optional(c, be_value);
// 7. If we have a variable, then we make the store. // 7. Store the success block.
if (var_addr) LLVMBasicBlockRef success_block = llvm_get_current_block_if_in_use(c);
// 8. If we have a variable, then we make the store.
if (success_block && var_addr)
{ {
ASSERT(is_try && "Storing will only happen on try."); ASSERT(is_try && "Storing will only happen on try.");
llvm_store(c, var_addr, be_value); llvm_store(c, var_addr, be_value);
} }
// 8. Restore the error stack. // 9. Restore the error stack.
POP_CATCH(); POP_CATCH();
// 9. Store the success block. // 10. Special handling if no success.
LLVMBasicBlockRef success_block = c->current_block; if (!success_block)
{
llvm_emit_block(c, catch_block);
llvm_value_set(be_value, LLVMConstInt(c->bool_type, is_try ? 0 : 1, false), type_bool);
return;
}
// 10. Jump to the phi // 10. Jump to the phi
llvm_emit_br(c, phi_catch); llvm_emit_br(c, phi_catch);

View File

@@ -0,0 +1,20 @@
// #target: macos-x64
module test;
import std;
fn int main()
{
if (try x = (true ? io::EOF? : 3))
{}
return 0;
}
/* #expect: test.ll
entry:
%x = alloca i32, align 4
store i32 0, ptr %x, align 4
br label %catch_landing
catch_landing: ; preds = %entry
ret i32 0
}