Assert when using optional as init or inc part in a for loop #1942.

This commit is contained in:
Christoffer Lerno
2025-02-08 18:58:44 +01:00
parent 46c7e9aefa
commit c8e671d34b
4 changed files with 136 additions and 7 deletions

View File

@@ -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] input `The raw RGB or RGBA pixels to encode`
@param [&in] desc `The descriptor of the image` @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 // check info in desc
if (desc.width == 0 || desc.height == 0) return QOIError.INVALID_PARAMETERS?; 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 [&out] desc `The descriptor to fill with the image's info`
@param channels `The channels to be used` @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 // check input data
if (data.len < Header.sizeof + END_OF_STREAM.len) return QOIError.INVALID_DATA?; if (data.len < Header.sizeof + END_OF_STREAM.len) return QOIError.INVALID_DATA?;

View File

@@ -48,6 +48,7 @@
- Incorrect error message when providing too many associated values for enum #1934. - Incorrect error message when providing too many associated values for enum #1934.
- Allow function types to have a calling convention. #1938 - Allow function types to have a calling convention. #1938
- Issue with defer copying when triggered by break or continue #1936. - 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 ### Stdlib changes
- Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter. - Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter.

View File

@@ -516,9 +516,10 @@ void llvm_emit_for_stmt(GenContext *c, Ast *ast)
{ {
DEBUG_PUSH_LEXICAL_SCOPE(c, ast->span); DEBUG_PUSH_LEXICAL_SCOPE(c, ast->span);
// First, emit all inits. // First, emit all inits.
BEValue value; if (ast->for_stmt.init)
if (ast->for_stmt.init) llvm_emit_expr(c, &value, exprptr(ast->for_stmt.init)); {
llvm_emit_ignored_expr(c, exprptr(ast->for_stmt.init));
}
ExprId incr = ast->for_stmt.incr; ExprId incr = ast->for_stmt.incr;
LLVMBasicBlockRef inc_block = incr ? llvm_basic_block_new(c, "loop.inc") : NULL; 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)) if (llvm_get_current_block_if_in_use(c))
{ {
BEValue dummy; if (incr) llvm_emit_ignored_expr(c, exprptr(incr));
llvm_emit_expr(c, &dummy, incr ? exprptr(incr) : NULL);
} }
} }

View File

@@ -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
}