diff --git a/lib/std/zig/AstGen.zig b/lib/std/zig/AstGen.zig index 675fe095a26e..f90ccad79a17 100644 --- a/lib/std/zig/AstGen.zig +++ b/lib/std/zig/AstGen.zig @@ -7811,9 +7811,7 @@ fn switchExpr( const switch_block = try parent_gz.makeBlockInst(switch_tag, node); if (switch_full.label_token) |label_token| { - block_scope.break_block = switch_block.toOptional(); block_scope.continue_block = switch_block.toOptional(); - // `break_result_info` already set above block_scope.continue_result_info = .{ .rl = if (any_payload_is_ref) .{ .ref_coerced_ty = raw_operand_ty_ref } @@ -7825,6 +7823,8 @@ fn switchExpr( .token = label_token, .block_inst = switch_block, }; + // `break` can target this via `label.block_inst` + // `break_result_info` already set by `setBreakResultInfo` } // We re-use this same scope for all cases, including the special prong, if any. diff --git a/test/behavior/switch.zig b/test/behavior/switch.zig index f1ded573a029..d7f9af9d1d13 100644 --- a/test/behavior/switch.zig +++ b/test/behavior/switch.zig @@ -985,3 +985,14 @@ test "labeled switch with break" { comptime assert(comptime_val); } + +test "unlabeled break ignores switch" { + const result = while (true) { + _ = s: switch (@as(u32, 1)) { + 1 => continue :s 123, + else => |x| break x, + }; + comptime unreachable; // control flow never breaks from the switch + }; + try expect(result == 123); +}