Commit d065f297ab

Michael Dusan <michael.dusan@gmail.com>
2019-05-10 11:23:26
stage1: compile error for loop expr val ignored
closes #2460
1 parent 6b10f03
Changed files (2)
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);
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",
+    );
 }