Implicitly unwrapped optional value in defer incorrectly copied #1982.

This commit is contained in:
Christoffer Lerno
2025-02-20 03:44:22 +01:00
parent 8bf9ca89a1
commit 341a70bd5d
4 changed files with 70 additions and 1 deletions

View File

@@ -58,7 +58,7 @@ fn void main(String[] args)
// Print the test result
io::printfn("Found %d tests: %.1f%% (%d / %d) passed (%d skipped).",
test_count, 100 * success_count / math::max(1, test_count - skip_count),
test_count, (100.0 * success_count) / math::max(1, test_count - skip_count),
success_count, test_count - skip_count, skip_count);
libc::exit(success_count == test_count - skip_count ? 0 : 1);
}

View File

@@ -0,0 +1,53 @@
// #target: macos-x64
module test;
import std::io;
// Issue 1982
fn int main()
{
defer
{
int! x = 1;
if (catch err = x) unreachable();
int y = x;
}
return 0;
}
/* #expect: test.ll
define i32 @main() #0 {
entry:
%x = alloca i32, align 4
%x.f = alloca i64, align 8
%err = alloca i64, align 8
%y = alloca i32, align 4
store i32 1, ptr %x, align 4
store i64 0, ptr %x.f, align 8
br label %testblock
testblock: ; preds = %entry
%optval = load i64, ptr %x.f, align 8
%not_err = icmp eq i64 %optval, 0
%0 = call i1 @llvm.expect.i1(i1 %not_err, i1 true)
br i1 %0, label %after_check, label %assign_optional
assign_optional: ; preds = %testblock
store i64 %optval, ptr %err, align 8
br label %end_block
after_check: ; preds = %testblock
store i64 0, ptr %err, align 8
br label %end_block
end_block: ; preds = %after_check, %assign_optional
%1 = load i64, ptr %err, align 8
%i2b = icmp ne i64 %1, 0
br i1 %i2b, label %if.then, label %if.exit
if.then: ; preds = %end_block
unreachable
if.exit: ; preds = %end_block
%2 = load i32, ptr %x, align 4
store i32 %2, ptr %y, align 4
ret i32 0
}