Commit 9daf0140e5

Andrew Kelley <andrew@ziglang.org>
2019-07-03 03:14:42
add missing compile error for comptime continue inside runtime catch
See #2604
1 parent b84ff1d
Changed files (2)
src/ir.cpp
@@ -7624,26 +7624,27 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
             is_comptime);
 
     ir_set_cursor_at_end_and_append_block(irb, err_block);
+    Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, parent_scope, is_comptime);
     Scope *err_scope;
     if (var_node) {
         assert(var_node->type == NodeTypeSymbol);
         Buf *var_name = var_node->data.symbol_expr.symbol;
         bool is_const = true;
         bool is_shadowable = false;
-        ZigVar *var = ir_create_var(irb, node, parent_scope, var_name,
+        ZigVar *var = ir_create_var(irb, node, subexpr_scope, var_name,
             is_const, is_const, is_shadowable, is_comptime);
         err_scope = var->child_scope;
         IrInstruction *err_ptr = ir_build_unwrap_err_code(irb, err_scope, node, err_union_ptr);
         ir_build_var_decl_src(irb, err_scope, var_node, var, nullptr, err_ptr);
     } else {
-        err_scope = parent_scope;
+        err_scope = subexpr_scope;
     }
     IrInstruction *err_result = ir_gen_node_extra(irb, op2_node, err_scope, LValNone, &peer_parent->peers.at(0)->base);
     if (err_result == irb->codegen->invalid_instruction)
         return irb->codegen->invalid_instruction;
     IrBasicBlock *after_err_block = irb->current_basic_block;
     if (!instr_is_unreachable(err_result))
-        ir_mark_gen(ir_build_br(irb, err_scope, node, end_block, is_comptime));
+        ir_mark_gen(ir_build_br(irb, parent_scope, node, end_block, is_comptime));
 
     ir_set_cursor_at_end_and_append_block(irb, ok_block);
     IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, parent_scope, node, err_union_ptr, false, false);
test/compile_errors.zig
@@ -1148,6 +1148,22 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
         "tmp.zig:4:5: error: no-inline call of inline function",
     );
 
+    cases.add(
+        "comptime continue inside runtime catch",
+        \\export fn entry(c: bool) void {
+        \\    const ints = [_]u8{ 1, 2 };
+        \\    inline for (ints) |_| {
+        \\        bad() catch |_| continue;
+        \\    }
+        \\}
+        \\fn bad() !void {
+        \\    return error.Bad;
+        \\}
+    ,
+        "tmp.zig:4:25: error: comptime control flow inside runtime block",
+        "tmp.zig:4:15: note: runtime block created here",
+    );
+
     cases.add(
         "comptime continue inside runtime switch",
         \\export fn entry() void {