stage1: compile error for loop expr val ignored

closes #2460
This commit is contained in:
Michael Dusan 2019-05-10 05:23:26 -04:00 committed by Andrew Kelley
parent 6b10f03b4a
commit d065f297ab
2 changed files with 20 additions and 1 deletions

View File

@ -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);

View File

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