authorgravatar for justus@klausecker.deJustus Klausecker <justus@klausecker.de> 2025-07-08 01:32:49+02:00
committergravatar for justus@klausecker.deJustus Klausecker <justus@klausecker.de> 2025-08-07 13:57:57+02:00
log1d9b1c021273037efea6623e669800ad538c8075
tree95efb3febc06d875e14cb1df105d686b678ff09f
parentfd9cfc39f53fae7fad4eadf9f1d8943f7e2c03b1

Permit explicit tags with '_' switch prong

Mainly affects ZIR representation of switch_block[_ref] and special prong (detection) logic for switch. Adds a new SpecialProng tag 'absorbing_under' that allows specifying additional explicit tags in a '_' prong which are respected when checking that every value is handled during semantic analysis but are not transformed into AIR and instead 'absorbed' by the '_' branch.

10 files changed, 333 insertions(+), 127 deletions(-)

lib/std/zig/Ast.zig+18
......@@ -2877,6 +2877,24 @@ pub const full = struct {
28772877 arrow_token: TokenIndex,
28782878 target_expr: Node.Index,
28792879 };
2880
2881 /// Returns:
2882 /// `null` if case is not special
2883 /// `.none` if case is else prong
2884 /// Index of underscore otherwise
2885 pub fn isSpecial(case: *const SwitchCase, tree: *const Ast) ?Node.OptionalIndex {
2886 if (case.ast.values.len == 0) {
2887 return .none;
2888 }
2889 for (case.ast.values) |val| {
2890 if (tree.nodeTag(val) == .identifier and
2891 mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(val)), "_"))
2892 {
2893 return val.toOptional();
2894 }
2895 }
2896 return null;
2897 }
28802898 };
28812899
28822900 pub const Asm = struct {
lib/std/zig/AstGen.zig+91-59
......@@ -7666,6 +7666,7 @@ fn switchExpr(
76667666 var special_node: Ast.Node.OptionalIndex = .none;
76677667 var else_src: ?Ast.TokenIndex = null;
76687668 var underscore_src: ?Ast.TokenIndex = null;
7669 var underscore_node: Ast.Node.OptionalIndex = .none;
76697670 for (case_nodes) |case_node| {
76707671 const case = tree.fullSwitchCase(case_node).?;
76717672 if (case.payload_token) |payload_token| {
......@@ -7686,7 +7687,7 @@ fn switchExpr(
76867687 any_non_inline_capture = true;
76877688 }
76887689 }
7689 // Check for else/`_` prong.
7690 // Check for else prong.
76907691 if (case.ast.values.len == 0) {
76917692 const case_src = case.ast.arrow_token - 1;
76927693 if (else_src) |src| {
......@@ -7725,56 +7726,60 @@ fn switchExpr(
77257726 special_prong = .@"else";
77267727 else_src = case_src;
77277728 continue;
7728 } else if (case.ast.values.len == 1 and
7729 tree.nodeTag(case.ast.values[0]) == .identifier and
7730 mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(case.ast.values[0])), "_"))
7731 {
7732 const case_src = case.ast.arrow_token - 1;
7733 if (underscore_src) |src| {
7734 return astgen.failTokNotes(
7735 case_src,
7736 "multiple '_' prongs in switch expression",
7737 .{},
7738 &[_]u32{
7739 try astgen.errNoteTok(
7740 src,
7741 "previous '_' prong here",
7742 .{},
7743 ),
7744 },
7745 );
7746 } else if (else_src) |some_else| {
7747 return astgen.failNodeNotes(
7748 node,
7749 "else and '_' prong in switch expression",
7750 .{},
7751 &[_]u32{
7752 try astgen.errNoteTok(
7753 some_else,
7754 "else prong here",
7755 .{},
7756 ),
7757 try astgen.errNoteTok(
7758 case_src,
7759 "'_' prong here",
7760 .{},
7761 ),
7762 },
7763 );
7764 }
7765 if (case.inline_token != null) {
7766 return astgen.failTok(case_src, "cannot inline '_' prong", .{});
7767 }
7768 special_node = case_node.toOptional();
7769 special_prong = .under;
7770 underscore_src = case_src;
7771 continue;
77727729 }
77737730
7731 // Check for '_' prong.
7732 var found_underscore = false;
77747733 for (case.ast.values) |val| {
7775 if (tree.nodeTag(val) == .string_literal)
7776 return astgen.failNode(val, "cannot switch on strings", .{});
7734 switch (tree.nodeTag(val)) {
7735 .identifier => if (mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(val)), "_")) {
7736 const case_src = case.ast.arrow_token - 1;
7737 if (underscore_src) |src| {
7738 return astgen.failTokNotes(
7739 case_src,
7740 "multiple '_' prongs in switch expression",
7741 .{},
7742 &[_]u32{
7743 try astgen.errNoteTok(
7744 src,
7745 "previous '_' prong here",
7746 .{},
7747 ),
7748 },
7749 );
7750 } else if (else_src) |some_else| {
7751 return astgen.failNodeNotes(
7752 node,
7753 "else and '_' prong in switch expression",
7754 .{},
7755 &[_]u32{
7756 try astgen.errNoteTok(
7757 some_else,
7758 "else prong here",
7759 .{},
7760 ),
7761 try astgen.errNoteTok(
7762 case_src,
7763 "'_' prong here",
7764 .{},
7765 ),
7766 },
7767 );
7768 }
7769 if (case.inline_token != null) {
7770 return astgen.failTok(case_src, "cannot inline '_' prong", .{});
7771 }
7772 special_node = case_node.toOptional();
7773 special_prong = if (case.ast.values.len == 1) .under else .absorbing_under;
7774 underscore_src = case_src;
7775 underscore_node = val.toOptional();
7776 found_underscore = true;
7777 },
7778 .string_literal => return astgen.failNode(val, "cannot switch on strings", .{}),
7779 else => {},
7780 }
77777781 }
7782 if (found_underscore) continue;
77787783
77797784 if (case.ast.values.len == 1 and tree.nodeTag(case.ast.values[0]) != .switch_range) {
77807785 scalar_cases_len += 1;
......@@ -7938,14 +7943,23 @@ fn switchExpr(
79387943
79397944 const header_index: u32 = @intCast(payloads.items.len);
79407945 const body_len_index = if (is_multi_case) blk: {
7941 payloads.items[multi_case_table + multi_case_index] = header_index;
7942 multi_case_index += 1;
7946 if (case_node.toOptional() == special_node) {
7947 assert(special_prong == .absorbing_under);
7948 payloads.items[case_table_start] = header_index;
7949 } else {
7950 payloads.items[multi_case_table + multi_case_index] = header_index;
7951 multi_case_index += 1;
7952 }
79437953 try payloads.resize(gpa, header_index + 3); // items_len, ranges_len, body_len
79447954
79457955 // items
79467956 var items_len: u32 = 0;
79477957 for (case.ast.values) |item_node| {
7948 if (tree.nodeTag(item_node) == .switch_range) continue;
7958 if (item_node.toOptional() == underscore_node or
7959 tree.nodeTag(item_node) == .switch_range)
7960 {
7961 continue;
7962 }
79497963 items_len += 1;
79507964
79517965 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
......@@ -7955,7 +7969,9 @@ fn switchExpr(
79557969 // ranges
79567970 var ranges_len: u32 = 0;
79577971 for (case.ast.values) |range| {
7958 if (tree.nodeTag(range) != .switch_range) continue;
7972 if (tree.nodeTag(range) != .switch_range) {
7973 continue;
7974 }
79597975 ranges_len += 1;
79607976
79617977 const first_node, const last_node = tree.nodeData(range).node_and_node;
......@@ -7970,6 +7986,7 @@ fn switchExpr(
79707986 payloads.items[header_index + 1] = ranges_len;
79717987 break :blk header_index + 2;
79727988 } else if (case_node.toOptional() == special_node) blk: {
7989 assert(special_prong != .absorbing_under);
79737990 payloads.items[case_table_start] = header_index;
79747991 try payloads.resize(gpa, header_index + 1); // body_len
79757992 break :blk header_index;
......@@ -8025,15 +8042,13 @@ fn switchExpr(
80258042 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.SwitchBlock).@"struct".fields.len +
80268043 @intFromBool(multi_cases_len != 0) +
80278044 @intFromBool(any_has_tag_capture) +
8028 payloads.items.len - case_table_end +
8029 (case_table_end - case_table_start) * @typeInfo(Zir.Inst.As).@"struct".fields.len);
8045 payloads.items.len - scratch_top);
80308046
80318047 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.SwitchBlock{
80328048 .operand = raw_operand,
80338049 .bits = Zir.Inst.SwitchBlock.Bits{
80348050 .has_multi_cases = multi_cases_len != 0,
8035 .has_else = special_prong == .@"else",
8036 .has_under = special_prong == .under,
8051 .special_prong = special_prong,
80378052 .any_has_tag_capture = any_has_tag_capture,
80388053 .any_non_inline_capture = any_non_inline_capture,
80398054 .has_continue = switch_full.label_token != null and block_scope.label.?.used_for_continue,
......@@ -8052,13 +8067,30 @@ fn switchExpr(
80528067 const zir_datas = astgen.instructions.items(.data);
80538068 zir_datas[@intFromEnum(switch_block)].pl_node.payload_index = payload_index;
80548069
8055 for (payloads.items[case_table_start..case_table_end], 0..) |start_index, i| {
8070 var normal_case_table_start = case_table_start;
8071 if (special_prong != .none) {
8072 normal_case_table_start += 1;
8073
8074 const start_index = payloads.items[case_table_start];
80568075 var body_len_index = start_index;
80578076 var end_index = start_index;
8058 const table_index = case_table_start + i;
8059 if (table_index < scalar_case_table) {
8077 if (special_prong == .absorbing_under) {
8078 body_len_index += 2;
8079 const items_len = payloads.items[start_index];
8080 const ranges_len = payloads.items[start_index + 1];
8081 end_index += 3 + items_len + 2 * ranges_len;
8082 } else {
80608083 end_index += 1;
8061 } else if (table_index < multi_case_table) {
8084 }
8085 const prong_info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(payloads.items[body_len_index]);
8086 end_index += prong_info.body_len;
8087 astgen.extra.appendSliceAssumeCapacity(payloads.items[start_index..end_index]);
8088 }
8089 for (payloads.items[normal_case_table_start..case_table_end], 0..) |start_index, i| {
8090 var body_len_index = start_index;
8091 var end_index = start_index;
8092 const table_index = normal_case_table_start + i;
8093 if (table_index < multi_case_table) {
80628094 body_len_index += 1;
80638095 end_index += 2;
80648096 } else {
lib/std/zig/Zir.zig+41-19
......@@ -3226,8 +3226,15 @@ pub const Inst = struct {
32263226
32273227 /// 0. multi_cases_len: u32 // If has_multi_cases is set.
32283228 /// 1. tag_capture_inst: u32 // If any_has_tag_capture is set. Index of instruction prongs use to refer to the inline tag capture.
3229 /// 2. else_body { // If has_else or has_under is set.
3229 /// 2. else_body { // If special_prong != .none
3230 /// items_len: u32, // If special_prong == .absorbing_under
3231 /// ranges_len: u32, // If special_prong == .absorbing_under
32303232 /// info: ProngInfo,
3233 /// item: Ref, // for every items_len
3234 /// ranges: { // for every ranges_len
3235 /// item_first: Ref,
3236 /// item_last: Ref,
3237 /// }
32313238 /// body member Index for every info.body_len
32323239 /// }
32333240 /// 3. scalar_cases: { // for every scalar_cases_len
......@@ -3239,7 +3246,7 @@ pub const Inst = struct {
32393246 /// items_len: u32,
32403247 /// ranges_len: u32,
32413248 /// info: ProngInfo,
3242 /// item: Ref // for every items_len
3249 /// item: Ref, // for every items_len
32433250 /// ranges: { // for every ranges_len
32443251 /// item_first: Ref,
32453252 /// item_last: Ref,
......@@ -3275,10 +3282,8 @@ pub const Inst = struct {
32753282 pub const Bits = packed struct(u32) {
32763283 /// If true, one or more prongs have multiple items.
32773284 has_multi_cases: bool,
3278 /// If true, there is an else prong. This is mutually exclusive with `has_under`.
3279 has_else: bool,
3280 /// If true, there is an underscore prong. This is mutually exclusive with `has_else`.
3281 has_under: bool,
3285 /// Information about the special prong.
3286 special_prong: SpecialProng,
32823287 /// If true, at least one prong has an inline tag capture.
32833288 any_has_tag_capture: bool,
32843289 /// If true, at least one prong has a capture which may not
......@@ -3288,17 +3293,6 @@ pub const Inst = struct {
32883293 scalar_cases_len: ScalarCasesLen,
32893294
32903295 pub const ScalarCasesLen = u26;
3291
3292 pub fn specialProng(bits: Bits) SpecialProng {
3293 const has_else: u2 = @intFromBool(bits.has_else);
3294 const has_under: u2 = @intFromBool(bits.has_under);
3295 return switch ((has_else << 1) | has_under) {
3296 0b00 => .none,
3297 0b01 => .under,
3298 0b10 => .@"else",
3299 0b11 => unreachable,
3300 };
3301 }
33023296 };
33033297
33043298 pub const MultiProng = struct {
......@@ -3874,7 +3868,18 @@ pub const Inst = struct {
38743868 };
38753869};
38763870
3877pub const SpecialProng = enum { none, @"else", under };
3871pub const SpecialProng = enum(u2) {
3872 none,
3873 /// Simple else prong.
3874 /// `else => {}`
3875 @"else",
3876 /// Simple '_' prong.
3877 /// `_ => {}`
3878 under,
3879 /// '_' prong with additional items.
3880 /// `a, _, b => {}`
3881 absorbing_under,
3882};
38783883
38793884pub const DeclIterator = struct {
38803885 extra_index: u32,
......@@ -4718,7 +4723,7 @@ fn findTrackableSwitch(
47184723 }
47194724
47204725 const has_special = switch (kind) {
4721 .normal => extra.data.bits.specialProng() != .none,
4726 .normal => extra.data.bits.special_prong != .none,
47224727 .err_union => has_special: {
47234728 // Handle `non_err_body` first.
47244729 const prong_info: Inst.SwitchBlock.ProngInfo = @bitCast(zir.extra[extra_index]);
......@@ -4733,6 +4738,23 @@ fn findTrackableSwitch(
47334738 };
47344739
47354740 if (has_special) {
4741 if (kind == .normal) {
4742 if (extra.data.bits.special_prong == .absorbing_under) {
4743 const items_len = zir.extra[extra_index];
4744 extra_index += 1;
4745 const ranges_len = zir.extra[extra_index];
4746 extra_index += 1;
4747 const prong_info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(zir.extra[extra_index]);
4748 extra_index += 1;
4749
4750 extra_index += items_len + ranges_len * 2;
4751
4752 const body = zir.bodySlice(extra_index, prong_info.body_len);
4753 extra_index += body.len;
4754
4755 try zir.findTrackableBody(gpa, contents, defers, body);
4756 }
4757 }
47364758 const prong_info: Inst.SwitchBlock.ProngInfo = @bitCast(zir.extra[extra_index]);
47374759 extra_index += 1;
47384760 const body = zir.bodySlice(extra_index, prong_info.body_len);
src/Sema.zig+52-11
......@@ -11335,7 +11335,10 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1133511335 var case_vals = try std.ArrayListUnmanaged(Air.Inst.Ref).initCapacity(gpa, scalar_cases_len + 2 * multi_cases_len);
1133611336 defer case_vals.deinit(gpa);
1133711337
11338 const special_prong = extra.data.bits.specialProng();
11338 var absorbed_items: []const Zir.Inst.Ref = &.{};
11339 var absorbed_ranges: []const Zir.Inst.Ref = &.{};
11340
11341 const special_prong = extra.data.bits.special_prong;
1133911342 const special: SpecialProng = switch (special_prong) {
1134011343 .none => .{
1134111344 .body = &.{},
......@@ -11355,6 +11358,26 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1135511358 .has_tag_capture = info.has_tag_capture,
1135611359 };
1135711360 },
11361 .absorbing_under => blk: {
11362 var extra_index = header_extra_index;
11363 const items_len = sema.code.extra[extra_index];
11364 extra_index += 1;
11365 const ranges_len = sema.code.extra[extra_index];
11366 extra_index += 1;
11367 const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]);
11368 extra_index += 1;
11369 absorbed_items = sema.code.refSlice(extra_index, items_len);
11370 extra_index += items_len;
11371 absorbed_ranges = sema.code.refSlice(extra_index, ranges_len * 2);
11372 extra_index += ranges_len * 2;
11373 break :blk .{
11374 .body = sema.code.bodySlice(extra_index, info.body_len),
11375 .end = extra_index + info.body_len,
11376 .capture = info.capture,
11377 .is_inline = info.is_inline,
11378 .has_tag_capture = info.has_tag_capture,
11379 };
11380 },
1135811381 };
1135911382
1136011383 // Duplicate checking variables later also used for `inline else`.
......@@ -11375,7 +11398,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1137511398 var else_error_ty: ?Type = null;
1137611399
1137711400 // Validate usage of '_' prongs.
11378 if (special_prong == .under and !raw_operand_ty.isNonexhaustiveEnum(zcu)) {
11401 if ((special_prong == .under or special_prong == .absorbing_under) and
11402 !raw_operand_ty.isNonexhaustiveEnum(zcu))
11403 {
1137911404 const msg = msg: {
1138011405 const msg = try sema.errMsg(
1138111406 src,
......@@ -11409,6 +11434,22 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1140911434 @memset(seen_enum_fields, null);
1141011435 // `range_set` is used for non-exhaustive enum values that do not correspond to any tags.
1141111436
11437 for (absorbed_items, 0..) |item_ref, item_i| {
11438 _ = try sema.validateSwitchItemEnum(
11439 block,
11440 seen_enum_fields,
11441 &range_set,
11442 item_ref,
11443 cond_ty,
11444 block.src(.{ .switch_case_item = .{
11445 .switch_node_offset = src_node_offset,
11446 .case_idx = .special,
11447 .item_idx = .{ .kind = .single, .index = @intCast(item_i) },
11448 } }),
11449 );
11450 }
11451 try sema.validateSwitchNoRange(block, @intCast(absorbed_ranges.len), cond_ty, src_node_offset);
11452
1141211453 var extra_index: usize = special.end;
1141311454 {
1141411455 var scalar_i: u32 = 0;
......@@ -11692,7 +11733,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1169211733 );
1169311734 }
1169411735 },
11695 .under, .none => {
11736 .under, .absorbing_under, .none => {
1169611737 if (true_count + false_count < 2) {
1169711738 return sema.fail(
1169811739 block,
......@@ -11892,7 +11933,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1189211933 special.capture,
1189311934 block.src(.{ .switch_capture = .{
1189411935 .switch_node_offset = src_node_offset,
11895 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
11936 .case_idx = .special,
1189611937 } }),
1189711938 undefined, // case_vals may be undefined for special prongs
1189811939 .none,
......@@ -12344,7 +12385,7 @@ fn analyzeSwitchRuntimeBlock(
1234412385 special.capture,
1234512386 child_block.src(.{ .switch_capture = .{
1234612387 .switch_node_offset = switch_node_offset,
12347 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12388 .case_idx = .special,
1234812389 } }),
1234912390 &.{item_ref},
1235012391 item_ref,
......@@ -12399,7 +12440,7 @@ fn analyzeSwitchRuntimeBlock(
1239912440 special.capture,
1240012441 child_block.src(.{ .switch_capture = .{
1240112442 .switch_node_offset = switch_node_offset,
12402 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12443 .case_idx = .special,
1240312444 } }),
1240412445 &.{item_ref},
1240512446 item_ref,
......@@ -12439,7 +12480,7 @@ fn analyzeSwitchRuntimeBlock(
1243912480 special.capture,
1244012481 child_block.src(.{ .switch_capture = .{
1244112482 .switch_node_offset = switch_node_offset,
12442 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12483 .case_idx = .special,
1244312484 } }),
1244412485 &.{item_ref},
1244512486 item_ref,
......@@ -12476,7 +12517,7 @@ fn analyzeSwitchRuntimeBlock(
1247612517 special.capture,
1247712518 child_block.src(.{ .switch_capture = .{
1247812519 .switch_node_offset = switch_node_offset,
12479 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12520 .case_idx = .special,
1248012521 } }),
1248112522 &.{.bool_true},
1248212523 .bool_true,
......@@ -12511,7 +12552,7 @@ fn analyzeSwitchRuntimeBlock(
1251112552 special.capture,
1251212553 child_block.src(.{ .switch_capture = .{
1251312554 .switch_node_offset = switch_node_offset,
12514 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12555 .case_idx = .special,
1251512556 } }),
1251612557 &.{.bool_false},
1251712558 .bool_false,
......@@ -12571,7 +12612,7 @@ fn analyzeSwitchRuntimeBlock(
1257112612 special.capture,
1257212613 child_block.src(.{ .switch_capture = .{
1257312614 .switch_node_offset = switch_node_offset,
12574 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12615 .case_idx = .special,
1257512616 } }),
1257612617 undefined, // case_vals may be undefined for special prongs
1257712618 .none,
......@@ -12836,7 +12877,7 @@ fn resolveSwitchComptime(
1283612877 special.capture,
1283712878 child_block.src(.{ .switch_capture = .{
1283812879 .switch_node_offset = switch_node_offset,
12839 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12880 .case_idx = .special,
1284012881 } }),
1284112882 undefined, // case_vals may be undefined for special prongs
1284212883 if (special.is_inline) cond_operand else .none,
src/Zcu.zig+26-21
......@@ -1684,13 +1684,13 @@ pub const SrcLoc = struct {
16841684 const case_nodes = tree.extraDataSlice(tree.extraData(extra_index, Ast.Node.SubRange), Ast.Node.Index);
16851685 for (case_nodes) |case_node| {
16861686 const case = tree.fullSwitchCase(case_node).?;
1687 const is_special = (case.ast.values.len == 0) or
1688 (case.ast.values.len == 1 and
1689 tree.nodeTag(case.ast.values[0]) == .identifier and
1690 mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(case.ast.values[0])), "_"));
1691 if (!is_special) continue;
1692
1693 return tree.nodeToSpan(case_node);
1687 if (case.isSpecial(tree)) |special_node| {
1688 return tree.tokensToSpan(
1689 tree.firstToken(case_node),
1690 tree.lastToken(case_node),
1691 tree.nodeMainToken(special_node.unwrap() orelse case_node),
1692 );
1693 }
16941694 } else unreachable;
16951695 },
16961696
......@@ -1701,11 +1701,9 @@ pub const SrcLoc = struct {
17011701 const case_nodes = tree.extraDataSlice(tree.extraData(extra_index, Ast.Node.SubRange), Ast.Node.Index);
17021702 for (case_nodes) |case_node| {
17031703 const case = tree.fullSwitchCase(case_node).?;
1704 const is_special = (case.ast.values.len == 0) or
1705 (case.ast.values.len == 1 and
1706 tree.nodeTag(case.ast.values[0]) == .identifier and
1707 mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(case.ast.values[0])), "_"));
1708 if (is_special) continue;
1704 if (case.isSpecial(tree)) |maybe_else| {
1705 if (maybe_else == .none) continue;
1706 }
17091707
17101708 for (case.ast.values) |item_node| {
17111709 if (tree.nodeTag(item_node) == .switch_range) {
......@@ -2111,17 +2109,21 @@ pub const SrcLoc = struct {
21112109
21122110 var multi_i: u32 = 0;
21132111 var scalar_i: u32 = 0;
2112 var found_special = false;
2113 var underscore_node: Ast.Node.OptionalIndex = .none;
21142114 const case = for (case_nodes) |case_node| {
21152115 const case = tree.fullSwitchCase(case_node).?;
21162116 const is_special = special: {
2117 if (case.ast.values.len == 0) break :special true;
2118 if (case.ast.values.len == 1 and tree.nodeTag(case.ast.values[0]) == .identifier) {
2119 break :special mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(case.ast.values[0])), "_");
2117 if (found_special) break :special false;
2118 if (case.isSpecial(tree)) |special_node| {
2119 underscore_node = special_node;
2120 found_special = true;
2121 break :special true;
21202122 }
21212123 break :special false;
21222124 };
21232125 if (is_special) {
2124 if (want_case_idx.isSpecial()) {
2126 if (want_case_idx == LazySrcLoc.Offset.SwitchCaseIndex.special) {
21252127 break case;
21262128 }
21272129 continue;
......@@ -2171,7 +2173,11 @@ pub const SrcLoc = struct {
21712173 .single => {
21722174 var item_i: u32 = 0;
21732175 for (case.ast.values) |item_node| {
2174 if (tree.nodeTag(item_node) == .switch_range) continue;
2176 if (item_node.toOptional() == underscore_node or
2177 tree.nodeTag(item_node) == .switch_range)
2178 {
2179 continue;
2180 }
21752181 if (item_i != want_item.index) {
21762182 item_i += 1;
21772183 continue;
......@@ -2182,7 +2188,9 @@ pub const SrcLoc = struct {
21822188 .range => {
21832189 var range_i: u32 = 0;
21842190 for (case.ast.values) |item_node| {
2185 if (tree.nodeTag(item_node) != .switch_range) continue;
2191 if (tree.nodeTag(item_node) != .switch_range) {
2192 continue;
2193 }
21862194 if (range_i != want_item.index) {
21872195 range_i += 1;
21882196 continue;
......@@ -2561,9 +2569,6 @@ pub const LazySrcLoc = struct {
25612569 index: u31,
25622570
25632571 pub const special: SwitchCaseIndex = @bitCast(@as(u32, std.math.maxInt(u32)));
2564 pub fn isSpecial(idx: SwitchCaseIndex) bool {
2565 return @as(u32, @bitCast(idx)) == @as(u32, @bitCast(special));
2566 }
25672572 };
25682573
25692574 pub const SwitchItemIndex = packed struct(u32) {
src/print_zir.zig+45-15
......@@ -2088,27 +2088,57 @@ const Writer = struct {
20882088 self.indent += 2;
20892089
20902090 else_prong: {
2091 const special_prong = extra.data.bits.specialProng();
2092 const prong_name = switch (special_prong) {
2093 .@"else" => "else",
2094 .under => "_",
2095 else => break :else_prong,
2096 };
2091 const special_prong = extra.data.bits.special_prong;
2092 if (special_prong == .none) break :else_prong;
20972093
2094 var items_len: u32 = 0;
2095 var ranges_len: u32 = 0;
2096 if (special_prong == .absorbing_under) {
2097 items_len = self.code.extra[extra_index];
2098 extra_index += 1;
2099 ranges_len = self.code.extra[extra_index];
2100 extra_index += 1;
2101 }
20982102 const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(self.code.extra[extra_index]));
2099 const capture_text = switch (info.capture) {
2100 .none => "",
2101 .by_val => "by_val ",
2102 .by_ref => "by_ref ",
2103 };
2104 const inline_text = if (info.is_inline) "inline " else "";
21052103 extra_index += 1;
2106 const body = self.code.bodySlice(extra_index, info.body_len);
2107 extra_index += body.len;
2104 const items = self.code.refSlice(extra_index, items_len);
2105 extra_index += items_len;
21082106
21092107 try stream.writeAll(",\n");
21102108 try stream.splatByteAll(' ', self.indent);
2111 try stream.print("{s}{s}{s} => ", .{ capture_text, inline_text, prong_name });
2109 switch (info.capture) {
2110 .none => {},
2111 .by_val => try stream.writeAll("by_val "),
2112 .by_ref => try stream.writeAll("by_ref "),
2113 }
2114 if (info.is_inline) try stream.writeAll("inline ");
2115 switch (special_prong) {
2116 .@"else" => try stream.writeAll("else"),
2117 .under, .absorbing_under => try stream.writeAll("_"),
2118 .none => unreachable,
2119 }
2120
2121 for (items) |item_ref| {
2122 try stream.writeAll(", ");
2123 try self.writeInstRef(stream, item_ref);
2124 }
2125
2126 var range_i: usize = 0;
2127 while (range_i < ranges_len) : (range_i += 1) {
2128 const item_first = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
2129 extra_index += 1;
2130 const item_last = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
2131 extra_index += 1;
2132
2133 try stream.writeAll(", ");
2134 try self.writeInstRef(stream, item_first);
2135 try stream.writeAll("...");
2136 try self.writeInstRef(stream, item_last);
2137 }
2138
2139 const body = self.code.bodySlice(extra_index, info.body_len);
2140 extra_index += info.body_len;
2141 try stream.writeAll(" => ");
21122142 try self.writeBracedBody(stream, body);
21132143 }
21142144
test/behavior/switch.zig+25
......@@ -1073,3 +1073,28 @@ test "switch on 8-bit mod result" {
10731073 else => unreachable,
10741074 }
10751075}
1076
1077test "switch on non-exhaustive enum" {
1078 const E = enum(u32) {
1079 a,
1080 b,
1081 c,
1082 _,
1083 };
1084
1085 var e: E = .a;
1086 _ = &e;
1087 switch (e) {
1088 .a, .b => {},
1089 else => return error.TestFailed,
1090 }
1091 switch (e) {
1092 .a, .b => {},
1093 .c => return error.TestFailed,
1094 _ => return error.TestFailed,
1095 }
1096 switch (e) {
1097 .a, .b => {},
1098 .c, _ => return error.TestFailed,
1099 }
1100}
test/cases/compile_errors/switch_expression-non_exhaustive_absorbing.zig created+33
......@@ -0,0 +1,33 @@
1const E = enum(u8) {
2 a,
3 b,
4 _,
5};
6const U = union(E) {
7 a: i32,
8 b: u32,
9};
10pub export fn entry1() void {
11 const e: E = .b;
12 switch (e) { // error: switch not handling the tag `b`
13 .a, _ => {},
14 }
15}
16pub export fn entry2() void {
17 const u = U{ .a = 2 };
18 switch (u) { // error: `_` prong not allowed when switching on tagged union
19 .a => {},
20 .b, _ => {},
21 }
22}
23
24// error
25// backend=stage2
26// target=native
27//
28// :12:5: error: switch must handle all possibilities
29// :3:5: note: unhandled enumeration value: 'b'
30// :1:11: note: enum 'tmp.E' declared here
31// :18:5: error: '_' prong only allowed when switching on non-exhaustive enums
32// :20:13: note: '_' prong here
33// :18:5: note: consider using 'else'
test/cases/compile_errors/switching_with_exhaustive_enum_has___prong_.zig+1-1
......@@ -16,5 +16,5 @@ pub export fn entry() void {
1616// target=native
1717//
1818// :7:5: error: '_' prong only allowed when switching on non-exhaustive enums
19// :10:11: note: '_' prong here
19// :10:9: note: '_' prong here
2020// :7:5: note: consider using 'else'
test/cases/compile_errors/switching_with_non-exhaustive_enums.zig+1-1
......@@ -39,5 +39,5 @@ pub export fn entry3() void {
3939// :1:11: note: enum 'tmp.E' declared here
4040// :19:5: error: switch on non-exhaustive enum must include 'else' or '_' prong
4141// :26:5: error: '_' prong only allowed when switching on non-exhaustive enums
42// :29:11: note: '_' prong here
42// :29:9: note: '_' prong here
4343// :26:5: note: consider using 'else'