authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-12 15:04:43+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-12 12:49:12-05:00
logc96131f30caaf6d7cd1d202891a28ee0df8b577e
treedb1f02cdbc1955ef07b523c4c7ba0acb69476127
parentc4770e7aa54a7a88eacf9e7780b0fe860f29251f

Propagate errors in for loop bodies

Closes #3819

2 files changed, 11 insertions(+), 0 deletions(-)

src/ir.cpp+2
...@@ -7456,6 +7456,8 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo...@@ -7456,6 +7456,8 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
7456 // it's actually in break statements, handled similarly to return statements.7456 // it's actually in break statements, handled similarly to return statements.
7457 // That is why we set those values in loop_scope above and not in this ir_gen_node call.7457 // That is why we set those values in loop_scope above and not in this ir_gen_node call.
7458 IrInstruction *body_result = ir_gen_node(irb, body_node, &loop_scope->base);7458 IrInstruction *body_result = ir_gen_node(irb, body_node, &loop_scope->base);
7459 if (body_result == irb->codegen->invalid_instruction)
7460 return irb->codegen->invalid_instruction;
74597461
7460 if (!instr_is_unreachable(body_result)) {7462 if (!instr_is_unreachable(body_result)) {
7461 ir_mark_gen(ir_build_check_statement_is_void(irb, child_scope, node->data.for_expr.body, body_result));7463 ir_mark_gen(ir_build_check_statement_is_void(irb, child_scope, node->data.for_expr.body, body_result));
test/compile_errors.zig+9
...@@ -2,6 +2,15 @@ const tests = @import("tests.zig");...@@ -2,6 +2,15 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.addTest("errors in for loop bodies are propagated",
6 \\pub export fn entry() void {
7 \\ var arr: [100]u8 = undefined;
8 \\ for (arr) |bits| _ = @popCount(bits);
9 \\}
10 , &[_][]const u8{
11 "tmp.zig:3:26: error: expected 2 arguments, found 1",
12 });
13
5 cases.addTest("error in struct initializer doesn't crash the compiler",14 cases.addTest("error in struct initializer doesn't crash the compiler",
6 \\pub export fn entry() void {15 \\pub export fn entry() void {
7 \\ const bitfield = struct {16 \\ const bitfield = struct {