From c8e671d34b7ec28edfc9fbff48d1767cbfb614b4 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 8 Feb 2025 18:58:44 +0100 Subject: [PATCH] Assert when using optional as init or inc part in a for loop #1942. --- lib/std/compression/qoi.c3 | 4 +- releasenotes.md | 1 + src/compiler/llvm_codegen_stmt.c | 10 +- .../statements/for_with_optional.c3t | 128 ++++++++++++++++++ 4 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 test/test_suite/statements/for_with_optional.c3t diff --git a/lib/std/compression/qoi.c3 b/lib/std/compression/qoi.c3 index 431441701..da6ff9a3b 100644 --- a/lib/std/compression/qoi.c3 +++ b/lib/std/compression/qoi.c3 @@ -153,7 +153,7 @@ fn char[]! encode(char[] input, QOIDesc* desc, Allocator allocator = allocator:: @param [in] input `The raw RGB or RGBA pixels to encode` @param [&in] desc `The descriptor of the image` *> -fn char[]! new_encode(char[] input, QOIDesc* desc, Allocator allocator = allocator::heap()) +fn char[]! new_encode(char[] input, QOIDesc* desc, Allocator allocator = allocator::heap()) @nodiscard { // check info in desc if (desc.width == 0 || desc.height == 0) return QOIError.INVALID_PARAMETERS?; @@ -311,7 +311,7 @@ fn char[]! decode(char[] data, QOIDesc* desc, QOIChannels channels = AUTO, Alloc @param [&out] desc `The descriptor to fill with the image's info` @param channels `The channels to be used` *> -fn char[]! new_decode(char[] data, QOIDesc* desc, QOIChannels channels = AUTO, Allocator allocator = allocator::heap()) +fn char[]! new_decode(char[] data, QOIDesc* desc, QOIChannels channels = AUTO, Allocator allocator = allocator::heap()) @nodiscard { // check input data if (data.len < Header.sizeof + END_OF_STREAM.len) return QOIError.INVALID_DATA?; diff --git a/releasenotes.md b/releasenotes.md index 39a7b063b..9388469bc 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -48,6 +48,7 @@ - Incorrect error message when providing too many associated values for enum #1934. - Allow function types to have a calling convention. #1938 - Issue with defer copying when triggered by break or continue #1936. +- Assert when using optional as init or inc part in a for loop #1942. ### Stdlib changes - Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter. diff --git a/src/compiler/llvm_codegen_stmt.c b/src/compiler/llvm_codegen_stmt.c index 31ca72aa0..080c8e611 100644 --- a/src/compiler/llvm_codegen_stmt.c +++ b/src/compiler/llvm_codegen_stmt.c @@ -516,9 +516,10 @@ void llvm_emit_for_stmt(GenContext *c, Ast *ast) { DEBUG_PUSH_LEXICAL_SCOPE(c, ast->span); // First, emit all inits. - BEValue value; - if (ast->for_stmt.init) llvm_emit_expr(c, &value, exprptr(ast->for_stmt.init)); - + if (ast->for_stmt.init) + { + llvm_emit_ignored_expr(c, exprptr(ast->for_stmt.init)); + } ExprId incr = ast->for_stmt.incr; LLVMBasicBlockRef inc_block = incr ? llvm_basic_block_new(c, "loop.inc") : NULL; @@ -669,8 +670,7 @@ void llvm_emit_for_stmt(GenContext *c, Ast *ast) } if (llvm_get_current_block_if_in_use(c)) { - BEValue dummy; - llvm_emit_expr(c, &dummy, incr ? exprptr(incr) : NULL); + if (incr) llvm_emit_ignored_expr(c, exprptr(incr)); } } diff --git a/test/test_suite/statements/for_with_optional.c3t b/test/test_suite/statements/for_with_optional.c3t new file mode 100644 index 000000000..bf04c5aea --- /dev/null +++ b/test/test_suite/statements/for_with_optional.c3t @@ -0,0 +1,128 @@ +// #target: macos-x64 +module test; + +fn void! test() +{ + int! n; + for (n += 1; n! < 10; n += 1); +} +fn int main() +{ + (void)test(); + int! n; + for (n += 1; n!! < 10; n += 1); + return 0; +} + +/* #expect: test.ll + +define i64 @test.test() #0 { +entry: + %n = alloca i32, align 4 + %n.f = alloca i64, align 8 + %error_var = alloca i64, align 8 + store i64 0, ptr %n.f, align 8 + store i32 0, ptr %n, align 4 + %optval = load i64, ptr %n.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 %voiderr +after_check: ; preds = %entry + %1 = load i32, ptr %n, align 4 + %add = add i32 %1, 1 + store i32 %add, ptr %n, align 4 + br label %voiderr +voiderr: ; preds = %after_check, %entry + br label %loop.cond +loop.cond: ; preds = %voiderr8, %voiderr + %optval1 = load i64, ptr %n.f, align 8 + %not_err2 = icmp eq i64 %optval1, 0 + %2 = call i1 @llvm.expect.i1(i1 %not_err2, i1 true) + br i1 %2, label %after_check3, label %assign_optional +assign_optional: ; preds = %loop.cond + store i64 %optval1, ptr %error_var, align 8 + br label %guard_block +after_check3: ; preds = %loop.cond + br label %noerr_block +guard_block: ; preds = %assign_optional + %3 = load i64, ptr %error_var, align 8 + ret i64 %3 +noerr_block: ; preds = %after_check3 + %4 = load i32, ptr %n, align 4 + %lt = icmp slt i32 %4, 10 + br i1 %lt, label %loop.body, label %loop.exit +loop.body: ; preds = %noerr_block + %optval4 = load i64, ptr %n.f, align 8 + %not_err5 = icmp eq i64 %optval4, 0 + %5 = call i1 @llvm.expect.i1(i1 %not_err5, i1 true) + br i1 %5, label %after_check6, label %voiderr8 +after_check6: ; preds = %loop.body + %6 = load i32, ptr %n, align 4 + %add7 = add i32 %6, 1 + store i32 %add7, ptr %n, align 4 + br label %voiderr8 +voiderr8: ; preds = %after_check6, %loop.body + br label %loop.cond +loop.exit: ; preds = %noerr_block + ret i64 0 +} + +define i32 @main() #0 { +entry: + %n = alloca i32, align 4 + %n.f = alloca i64, align 8 + %error_var = alloca i64, align 8 + %varargslots = alloca [1 x %any], align 16 + %indirectarg = alloca %"any[]", align 8 + %0 = call i64 @test.test() + store i64 0, ptr %n.f, align 8 + store i32 0, ptr %n, align 4 + %optval = load i64, ptr %n.f, align 8 + %not_err = icmp eq i64 %optval, 0 + %1 = call i1 @llvm.expect.i1(i1 %not_err, i1 true) + br i1 %1, label %after_check, label %voiderr +after_check: ; preds = %entry + %2 = load i32, ptr %n, align 4 + %add = add i32 %2, 1 + store i32 %add, ptr %n, align 4 + br label %voiderr +voiderr: ; preds = %after_check, %entry + br label %loop.cond +loop.cond: ; preds = %voiderr8, %voiderr + %optval1 = load i64, ptr %n.f, align 8 + %not_err2 = icmp eq i64 %optval1, 0 + %3 = call i1 @llvm.expect.i1(i1 %not_err2, i1 true) + br i1 %3, label %after_check3, label %assign_optional +assign_optional: ; preds = %loop.cond + store i64 %optval1, ptr %error_var, align 8 + br label %panic_block +after_check3: ; preds = %loop.cond + br label %noerr_block +panic_block: ; preds = %assign_optional + %4 = insertvalue %any undef, ptr %error_var, 0 + %5 = insertvalue %any %4, i64 ptrtoint (ptr @"$ct.anyfault" to i64), 1 + store %any %5, ptr %varargslots, align 16 + %6 = insertvalue %"any[]" undef, ptr %varargslots, 0 + %"$$temp" = insertvalue %"any[]" %6, i64 1, 1 + store %"any[]" %"$$temp", ptr %indirectarg, align 8 + call void @std.core.builtin.panicf( + unreachable +noerr_block: ; preds = %after_check3 + %7 = load i32, ptr %n, align 4 + %lt = icmp slt i32 %7, 10 + br i1 %lt, label %loop.body, label %loop.exit +loop.body: ; preds = %noerr_block + %optval4 = load i64, ptr %n.f, align 8 + %not_err5 = icmp eq i64 %optval4, 0 + %8 = call i1 @llvm.expect.i1(i1 %not_err5, i1 true) + br i1 %8, label %after_check6, label %voiderr8 +after_check6: ; preds = %loop.body + %9 = load i32, ptr %n, align 4 + %add7 = add i32 %9, 1 + store i32 %add7, ptr %n, align 4 + br label %voiderr8 +voiderr8: ; preds = %after_check6, %loop.body + br label %loop.cond +loop.exit: ; preds = %noerr_block + ret i32 0 +}