Skip to content

Commit d065f29

Browse files
mikdusanandrewrk
authored andcommitted
stage1: compile error for loop expr val ignored
closes #2460
1 parent 6b10f03 commit d065f29

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/ir.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5808,8 +5808,10 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
58085808

58095809
IrInstruction *body_result = ir_gen_node(irb, body_node, &loop_scope->base);
58105810

5811-
if (!instr_is_unreachable(body_result))
5811+
if (!instr_is_unreachable(body_result)) {
5812+
ir_mark_gen(ir_build_check_statement_is_void(irb, child_scope, node->data.for_expr.body, body_result));
58125813
ir_mark_gen(ir_build_br(irb, child_scope, node, continue_block, is_comptime));
5814+
}
58135815

58145816
ir_set_cursor_at_end_and_append_block(irb, continue_block);
58155817
IrInstruction *new_index_val = ir_build_bin_op(irb, child_scope, node, IrBinOpAdd, index_val, one, false);

test/compile_errors.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5918,4 +5918,21 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59185918
,
59195919
"tmp.zig:3:23: error: expected type '[]u32', found '*const u32'",
59205920
);
5921+
5922+
cases.add(
5923+
"for loop body expression ignored",
5924+
\\fn returns() usize {
5925+
\\ return 2;
5926+
\\}
5927+
\\export fn f1() void {
5928+
\\ for ("hello") |_| returns();
5929+
\\}
5930+
\\export fn f2() void {
5931+
\\ var x: anyerror!i32 = error.Bad;
5932+
\\ for ("hello") |_| returns() else unreachable;
5933+
\\}
5934+
,
5935+
"tmp.zig:5:30: error: expression value is ignored",
5936+
"tmp.zig:9:30: error: expression value is ignored",
5937+
);
59215938
}

0 commit comments

Comments
 (0)