authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-08-31 00:33:45+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-01 18:31:01+01:00
logb7a55cd6c3ca0c4c97f266b72f741b980416456a
treeb164dd3a629abb7d1ca3aaa697712b8817e684cc
parentfd70d9db9960a98fb97def91aa34f56c15499ebf
signaturelock-open Commit is signed but in an unrecognized format.

AstGen: allow breaking from labeled switch

Also, don't use the special switch lowering for errors if the switch is labeled; this isn't currently supported. Related: #20627.

2 files changed, 38 insertions(+), 21 deletions(-)

lib/std/zig/AstGen.zig+14-21
......@@ -857,13 +857,10 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
857857 const if_full = tree.fullIf(node).?;
858858 no_switch_on_err: {
859859 const error_token = if_full.error_token orelse break :no_switch_on_err;
860 switch (node_tags[if_full.ast.else_expr]) {
861 .@"switch", .switch_comma => {},
862 else => break :no_switch_on_err,
863 }
864 const switch_operand = node_datas[if_full.ast.else_expr].lhs;
865 if (node_tags[switch_operand] != .identifier) break :no_switch_on_err;
866 if (!mem.eql(u8, tree.tokenSlice(error_token), tree.tokenSlice(main_tokens[switch_operand]))) break :no_switch_on_err;
860 const full_switch = tree.fullSwitch(if_full.ast.else_expr) orelse break :no_switch_on_err;
861 if (full_switch.label_token != null) break :no_switch_on_err;
862 if (node_tags[full_switch.ast.condition] != .identifier) break :no_switch_on_err;
863 if (!mem.eql(u8, tree.tokenSlice(error_token), tree.tokenSlice(main_tokens[full_switch.ast.condition]))) break :no_switch_on_err;
867864 return switchExprErrUnion(gz, scope, ri.br(), node, .@"if");
868865 }
869866 return ifExpr(gz, scope, ri.br(), node, if_full);
......@@ -1049,13 +1046,10 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
10491046 null;
10501047 no_switch_on_err: {
10511048 const capture_token = payload_token orelse break :no_switch_on_err;
1052 switch (node_tags[node_datas[node].rhs]) {
1053 .@"switch", .switch_comma => {},
1054 else => break :no_switch_on_err,
1055 }
1056 const switch_operand = node_datas[node_datas[node].rhs].lhs;
1057 if (node_tags[switch_operand] != .identifier) break :no_switch_on_err;
1058 if (!mem.eql(u8, tree.tokenSlice(capture_token), tree.tokenSlice(main_tokens[switch_operand]))) break :no_switch_on_err;
1049 const full_switch = tree.fullSwitch(node_datas[node].rhs) orelse break :no_switch_on_err;
1050 if (full_switch.label_token != null) break :no_switch_on_err;
1051 if (node_tags[full_switch.ast.condition] != .identifier) break :no_switch_on_err;
1052 if (!mem.eql(u8, tree.tokenSlice(capture_token), tree.tokenSlice(main_tokens[full_switch.ast.condition]))) break :no_switch_on_err;
10591053 return switchExprErrUnion(gz, scope, ri.br(), node, .@"catch");
10601054 }
10611055 switch (ri.rl) {
......@@ -2160,11 +2154,6 @@ fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) Inn
21602154 if (break_label != 0) {
21612155 if (block_gz.label) |*label| {
21622156 if (try astgen.tokenIdentEql(label.token, break_label)) {
2163 const maybe_switch_tag = astgen.instructions.items(.tag)[@intFromEnum(label.block_inst)];
2164 switch (maybe_switch_tag) {
2165 .switch_block, .switch_block_ref => return astgen.failNode(node, "cannot break from switch", .{}),
2166 else => {},
2167 }
21682157 label.used = true;
21692158 break :blk label.block_inst;
21702159 }
......@@ -2278,6 +2267,7 @@ fn continueExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index)
22782267 }
22792268
22802269 label.used = true;
2270 label.used_for_continue = true;
22812271 break :blk;
22822272 }
22832273 }
......@@ -7760,7 +7750,7 @@ fn switchExpr(
77607750 const raw_operand = try expr(parent_gz, scope, operand_ri, operand_node);
77617751 const item_ri: ResultInfo = .{ .rl = .none };
77627752
7763 // If this switch is labeled, it will have `continue`s targeting it, and thus we need the operand type
7753 // If this switch is labeled, it may have `continue`s targeting it, and thus we need the operand type
77647754 // to provide a result type.
77657755 const raw_operand_ty_ref = if (switch_full.label_token != null) t: {
77667756 break :t try parent_gz.addUnNode(.typeof, raw_operand, operand_node);
......@@ -7790,7 +7780,9 @@ fn switchExpr(
77907780 const switch_block = try parent_gz.makeBlockInst(switch_tag, node);
77917781
77927782 if (switch_full.label_token) |label_token| {
7783 block_scope.break_block = switch_block.toOptional();
77937784 block_scope.continue_block = switch_block.toOptional();
7785 // `break_result_info` already set above
77947786 block_scope.continue_result_info = .{
77957787 .rl = if (any_payload_is_ref)
77967788 .{ .ref_coerced_ty = raw_operand_ty_ref }
......@@ -8024,7 +8016,7 @@ fn switchExpr(
80248016 .has_under = special_prong == .under,
80258017 .any_has_tag_capture = any_has_tag_capture,
80268018 .any_non_inline_capture = any_non_inline_capture,
8027 .has_continue = switch_full.label_token != null,
8019 .has_continue = switch_full.label_token != null and block_scope.label.?.used_for_continue,
80288020 .scalar_cases_len = @intCast(scalar_cases_len),
80298021 },
80308022 });
......@@ -11963,6 +11955,7 @@ const GenZir = struct {
1196311955 token: Ast.TokenIndex,
1196411956 block_inst: Zir.Inst.Index,
1196511957 used: bool = false,
11958 used_for_continue: bool = false,
1196611959 };
1196711960
1196811961 /// Assumes nothing stacked on `gz`.
test/behavior/switch.zig+24
......@@ -961,3 +961,27 @@ test "block error return trace index is reset between prongs" {
961961 };
962962 try result;
963963}
964
965test "labeled switch with break" {
966 var six: u32 = undefined;
967 six = 6;
968
969 const val = s: switch (six) {
970 0...4 => break :s false,
971 5 => break :s false,
972 6...7 => break :s true,
973 else => break :s false,
974 };
975
976 try expect(val);
977
978 // Make sure the switch is implicitly comptime!
979 const comptime_val = s: switch (@as(u32, 6)) {
980 0...4 => break :s false,
981 5 => break :s false,
982 6...7 => break :s true,
983 else => break :s false,
984 };
985
986 comptime assert(comptime_val);
987}