zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit d065f297ab6de5ca35636d169195ce5da6235786 (tree)
parent 6b10f03b4a868cc02fc0535193ea0108c77ddd1f
Author: Michael Dusan <michael.dusan@gmail.com>
Date:   Fri, 10 May 2019 05:23:26 -0400

stage1: compile error for loop expr val ignored

closes #2460

Diffstat:
Msrc/ir.cpp | 4+++-
Mtest/compile_errors.zig | 17+++++++++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)

diff --git 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 @@ -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", + ); }