From d065f297ab6de5ca35636d169195ce5da6235786 Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Fri, 10 May 2019 05:23:26 -0400 Subject: [PATCH] stage1: compile error for loop expr val ignored closes #2460 --- src/ir.cpp | 4 +++- test/compile_errors.zig | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ir.cpp b/src/ir.cpp index f764cbd983..d1d1c44043 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -5808,8 +5808,10 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo IrInstruction *body_result = ir_gen_node(irb, body_node, &loop_scope->base); - if (!instr_is_unreachable(body_result)) + if (!instr_is_unreachable(body_result)) { + ir_mark_gen(ir_build_check_statement_is_void(irb, child_scope, node->data.for_expr.body, body_result)); ir_mark_gen(ir_build_br(irb, child_scope, node, continue_block, is_comptime)); + } ir_set_cursor_at_end_and_append_block(irb, continue_block); IrInstruction *new_index_val = ir_build_bin_op(irb, child_scope, node, IrBinOpAdd, index_val, one, false); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 63835e8c1b..9edad2662a 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -5918,4 +5918,21 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { , "tmp.zig:3:23: error: expected type '[]u32', found '*const u32'", ); + + cases.add( + "for loop body expression ignored", + \\fn returns() usize { + \\ return 2; + \\} + \\export fn f1() void { + \\ for ("hello") |_| returns(); + \\} + \\export fn f2() void { + \\ var x: anyerror!i32 = error.Bad; + \\ for ("hello") |_| returns() else unreachable; + \\} + , + "tmp.zig:5:30: error: expression value is ignored", + "tmp.zig:9:30: error: expression value is ignored", + ); }