authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-08-13 13:54:15+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-08-13 13:54:15+01:00
log6e90ce25364b02555a3ca46013f85b2e80e98705
treeaa3fe7f082466ba5fde47450d1c40c163e667d59
parentb8124d9c0b01e8ac7cd0daf93a0ed018da5f2352
parentaaee26bb1914a3d4e385bd120515813ece80311d
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #24381 from Justus2308/switch-better-underscore

Enhance switch on non-exhaustive enums

12 files changed, 925 insertions(+), 322 deletions(-)

lib/std/zig/AstGen.zig+122-85
......@@ -7662,10 +7662,12 @@ fn switchExpr(
76627662 var scalar_cases_len: u32 = 0;
76637663 var multi_cases_len: u32 = 0;
76647664 var inline_cases_len: u32 = 0;
7665 var special_prong: Zir.SpecialProng = .none;
7666 var special_node: Ast.Node.OptionalIndex = .none;
7665 var else_case_node: Ast.Node.OptionalIndex = .none;
76677666 var else_src: ?Ast.TokenIndex = null;
7667 var underscore_case_node: Ast.Node.OptionalIndex = .none;
7668 var underscore_node: Ast.Node.OptionalIndex = .none;
76687669 var underscore_src: ?Ast.TokenIndex = null;
7670 var underscore_additional_items: Zir.SpecialProngs.AdditionalItems = .none;
76697671 for (case_nodes) |case_node| {
76707672 const case = tree.fullSwitchCase(case_node).?;
76717673 if (case.payload_token) |payload_token| {
......@@ -7686,7 +7688,8 @@ fn switchExpr(
76867688 any_non_inline_capture = true;
76877689 }
76887690 }
7689 // Check for else/`_` prong.
7691
7692 // Check for else prong.
76907693 if (case.ast.values.len == 0) {
76917694 const case_src = case.ast.arrow_token - 1;
76927695 if (else_src) |src| {
......@@ -7702,79 +7705,51 @@ fn switchExpr(
77027705 ),
77037706 },
77047707 );
7705 } else if (underscore_src) |some_underscore| {
7706 return astgen.failNodeNotes(
7707 node,
7708 "else and '_' prong in switch expression",
7709 .{},
7710 &[_]u32{
7711 try astgen.errNoteTok(
7712 case_src,
7713 "else prong here",
7714 .{},
7715 ),
7716 try astgen.errNoteTok(
7717 some_underscore,
7718 "'_' prong here",
7719 .{},
7720 ),
7721 },
7722 );
77237708 }
7724 special_node = case_node.toOptional();
7725 special_prong = .@"else";
7709 else_case_node = case_node.toOptional();
77267710 else_src = case_src;
77277711 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;
77727712 }
77737713
7714 // Check for '_' prong.
7715 var case_has_underscore = false;
77747716 for (case.ast.values) |val| {
7775 if (tree.nodeTag(val) == .string_literal)
7776 return astgen.failNode(val, "cannot switch on strings", .{});
7717 switch (tree.nodeTag(val)) {
7718 .identifier => if (mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(val)), "_")) {
7719 const val_src = tree.nodeMainToken(val);
7720 if (underscore_src) |src| {
7721 return astgen.failTokNotes(
7722 val_src,
7723 "multiple '_' prongs in switch expression",
7724 .{},
7725 &[_]u32{
7726 try astgen.errNoteTok(
7727 src,
7728 "previous '_' prong here",
7729 .{},
7730 ),
7731 },
7732 );
7733 }
7734 if (case.inline_token != null) {
7735 return astgen.failTok(val_src, "cannot inline '_' prong", .{});
7736 }
7737 underscore_case_node = case_node.toOptional();
7738 underscore_src = val_src;
7739 underscore_node = val.toOptional();
7740 underscore_additional_items = switch (case.ast.values.len) {
7741 0 => unreachable,
7742 1 => .none,
7743 2 => .one,
7744 else => .many,
7745 };
7746 case_has_underscore = true;
7747 },
7748 .string_literal => return astgen.failNode(val, "cannot switch on strings", .{}),
7749 else => {},
7750 }
77777751 }
7752 if (case_has_underscore) continue;
77787753
77797754 if (case.ast.values.len == 1 and tree.nodeTag(case.ast.values[0]) != .switch_range) {
77807755 scalar_cases_len += 1;
......@@ -7786,6 +7761,14 @@ fn switchExpr(
77867761 }
77877762 }
77887763
7764 const special_prongs: Zir.SpecialProngs = .init(
7765 else_src != null,
7766 underscore_src != null,
7767 underscore_additional_items,
7768 );
7769 const has_else = special_prongs.hasElse();
7770 const has_under = special_prongs.hasUnder();
7771
77897772 const operand_ri: ResultInfo = .{ .rl = if (any_payload_is_ref) .ref else .none };
77907773
77917774 astgen.advanceSourceCursorToNode(operand_node);
......@@ -7806,7 +7789,9 @@ fn switchExpr(
78067789 const payloads = &astgen.scratch;
78077790 const scratch_top = astgen.scratch.items.len;
78087791 const case_table_start = scratch_top;
7809 const scalar_case_table = case_table_start + @intFromBool(special_prong != .none);
7792 const else_case_index = if (has_else) case_table_start else undefined;
7793 const under_case_index = if (has_under) case_table_start + @intFromBool(has_else) else undefined;
7794 const scalar_case_table = case_table_start + @intFromBool(has_else) + @intFromBool(has_under);
78107795 const multi_case_table = scalar_case_table + scalar_cases_len;
78117796 const case_table_end = multi_case_table + multi_cases_len;
78127797 try astgen.scratch.resize(gpa, case_table_end);
......@@ -7938,14 +7923,33 @@ fn switchExpr(
79387923
79397924 const header_index: u32 = @intCast(payloads.items.len);
79407925 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;
7926 if (case_node.toOptional() == underscore_case_node) {
7927 payloads.items[under_case_index] = header_index;
7928 if (special_prongs.hasOneAdditionalItem()) {
7929 try payloads.resize(gpa, header_index + 2); // item, body_len
7930 const maybe_item_node = case.ast.values[0];
7931 const item_node = if (maybe_item_node.toOptional() == underscore_node)
7932 case.ast.values[1]
7933 else
7934 maybe_item_node;
7935 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
7936 payloads.items[header_index] = @intFromEnum(item_inst);
7937 break :blk header_index + 1;
7938 }
7939 } else {
7940 payloads.items[multi_case_table + multi_case_index] = header_index;
7941 multi_case_index += 1;
7942 }
79437943 try payloads.resize(gpa, header_index + 3); // items_len, ranges_len, body_len
79447944
79457945 // items
79467946 var items_len: u32 = 0;
79477947 for (case.ast.values) |item_node| {
7948 if (tree.nodeTag(item_node) == .switch_range) continue;
7948 if (item_node.toOptional() == underscore_node or
7949 tree.nodeTag(item_node) == .switch_range)
7950 {
7951 continue;
7952 }
79497953 items_len += 1;
79507954
79517955 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
......@@ -7955,7 +7959,9 @@ fn switchExpr(
79557959 // ranges
79567960 var ranges_len: u32 = 0;
79577961 for (case.ast.values) |range| {
7958 if (tree.nodeTag(range) != .switch_range) continue;
7962 if (tree.nodeTag(range) != .switch_range) {
7963 continue;
7964 }
79597965 ranges_len += 1;
79607966
79617967 const first_node, const last_node = tree.nodeData(range).node_and_node;
......@@ -7969,8 +7975,13 @@ fn switchExpr(
79697975 payloads.items[header_index] = items_len;
79707976 payloads.items[header_index + 1] = ranges_len;
79717977 break :blk header_index + 2;
7972 } else if (case_node.toOptional() == special_node) blk: {
7973 payloads.items[case_table_start] = header_index;
7978 } else if (case_node.toOptional() == else_case_node) blk: {
7979 payloads.items[else_case_index] = header_index;
7980 try payloads.resize(gpa, header_index + 1); // body_len
7981 break :blk header_index;
7982 } else if (case_node.toOptional() == underscore_case_node) blk: {
7983 assert(!special_prongs.hasAdditionalItems());
7984 payloads.items[under_case_index] = header_index;
79747985 try payloads.resize(gpa, header_index + 1); // body_len
79757986 break :blk header_index;
79767987 } else blk: {
......@@ -8025,15 +8036,13 @@ fn switchExpr(
80258036 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.SwitchBlock).@"struct".fields.len +
80268037 @intFromBool(multi_cases_len != 0) +
80278038 @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);
8039 payloads.items.len - scratch_top);
80308040
80318041 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.SwitchBlock{
80328042 .operand = raw_operand,
80338043 .bits = Zir.Inst.SwitchBlock.Bits{
80348044 .has_multi_cases = multi_cases_len != 0,
8035 .has_else = special_prong == .@"else",
8036 .has_under = special_prong == .under,
8045 .special_prongs = special_prongs,
80378046 .any_has_tag_capture = any_has_tag_capture,
80388047 .any_non_inline_capture = any_non_inline_capture,
80398048 .has_continue = switch_full.label_token != null and block_scope.label.?.used_for_continue,
......@@ -8052,13 +8061,41 @@ fn switchExpr(
80528061 const zir_datas = astgen.instructions.items(.data);
80538062 zir_datas[@intFromEnum(switch_block)].pl_node.payload_index = payload_index;
80548063
8055 for (payloads.items[case_table_start..case_table_end], 0..) |start_index, i| {
8064 if (has_else) {
8065 const start_index = payloads.items[else_case_index];
8066 var end_index = start_index + 1;
8067 const prong_info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(payloads.items[start_index]);
8068 end_index += prong_info.body_len;
8069 astgen.extra.appendSliceAssumeCapacity(payloads.items[start_index..end_index]);
8070 }
8071 if (has_under) {
8072 const start_index = payloads.items[under_case_index];
80568073 var body_len_index = start_index;
80578074 var end_index = start_index;
8058 const table_index = case_table_start + i;
8059 if (table_index < scalar_case_table) {
8060 end_index += 1;
8061 } else if (table_index < multi_case_table) {
8075 switch (underscore_additional_items) {
8076 .none => {
8077 end_index += 1;
8078 },
8079 .one => {
8080 body_len_index += 1;
8081 end_index += 2;
8082 },
8083 .many => {
8084 body_len_index += 2;
8085 const items_len = payloads.items[start_index];
8086 const ranges_len = payloads.items[start_index + 1];
8087 end_index += 3 + items_len + 2 * ranges_len;
8088 },
8089 }
8090 const prong_info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(payloads.items[body_len_index]);
8091 end_index += prong_info.body_len;
8092 astgen.extra.appendSliceAssumeCapacity(payloads.items[start_index..end_index]);
8093 }
8094 for (payloads.items[scalar_case_table..case_table_end], 0..) |start_index, i| {
8095 var body_len_index = start_index;
8096 var end_index = start_index;
8097 const table_index = scalar_case_table + i;
8098 if (table_index < multi_case_table) {
80628099 body_len_index += 1;
80638100 end_index += 2;
80648101 } else {
lib/std/zig/Zir.zig+116-27
......@@ -3226,20 +3226,32 @@ 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.hasElse() is set.
32303230 /// info: ProngInfo,
32313231 /// body member Index for every info.body_len
32323232 /// }
3233 /// 3. scalar_cases: { // for every scalar_cases_len
3233 /// 3. under_body { // If special_prong.hasUnder() is set.
3234 /// item: Ref, // If special_prong.hasOneAdditionalItem() is set.
3235 /// items_len: u32, // If special_prong.hasManyAdditionalItems() is set.
3236 /// ranges_len: u32, // If special_prong.hasManyAdditionalItems() is set.
3237 /// info: ProngInfo,
3238 /// item: Ref, // for every items_len
3239 /// ranges: { // for every ranges_len
3240 /// item_first: Ref,
3241 /// item_last: Ref,
3242 /// }
3243 /// body member Index for every info.body_len
3244 /// }
3245 /// 4. scalar_cases: { // for every scalar_cases_len
32343246 /// item: Ref,
32353247 /// info: ProngInfo,
32363248 /// body member Index for every info.body_len
32373249 /// }
3238 /// 4. multi_cases: { // for every multi_cases_len
3250 /// 5. multi_cases: { // for every multi_cases_len
32393251 /// items_len: u32,
32403252 /// ranges_len: u32,
32413253 /// info: ProngInfo,
3242 /// item: Ref // for every items_len
3254 /// item: Ref, // for every items_len
32433255 /// ranges: { // for every ranges_len
32443256 /// item_first: Ref,
32453257 /// item_last: Ref,
......@@ -3275,30 +3287,18 @@ pub const Inst = struct {
32753287 pub const Bits = packed struct(u32) {
32763288 /// If true, one or more prongs have multiple items.
32773289 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,
3290 /// Information about the special prong.
3291 special_prongs: SpecialProngs,
32823292 /// If true, at least one prong has an inline tag capture.
32833293 any_has_tag_capture: bool,
32843294 /// If true, at least one prong has a capture which may not
32853295 /// be comptime-known via `inline`.
32863296 any_non_inline_capture: bool,
3297 /// If true, at least one prong contains a `continue`.
32873298 has_continue: bool,
32883299 scalar_cases_len: ScalarCasesLen,
32893300
3290 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 }
3301 pub const ScalarCasesLen = u25;
33023302 };
33033303
33043304 pub const MultiProng = struct {
......@@ -3874,7 +3874,68 @@ pub const Inst = struct {
38743874 };
38753875};
38763876
3877pub const SpecialProng = enum { none, @"else", under };
3877pub const SpecialProngs = enum(u3) {
3878 none = 0b000,
3879 /// Simple `else` prong.
3880 /// `else => {},`
3881 @"else" = 0b001,
3882 /// Simple `_` prong.
3883 /// `_ => {},`
3884 under = 0b010,
3885 /// Both an `else` and a `_` prong.
3886 /// `else => {},`
3887 /// `_ => {},`
3888 under_and_else = 0b011,
3889 /// `_` prong with 1 additional item.
3890 /// `a, _ => {},`
3891 under_one_item = 0b100,
3892 /// Both an `else` and a `_` prong with 1 additional item.
3893 /// `else => {},`
3894 /// `a, _ => {},`
3895 under_one_item_and_else = 0b101,
3896 /// `_` prong with >1 additional items.
3897 /// `a, _, b => {},`
3898 under_many_items = 0b110,
3899 /// Both an `else` and a `_` prong with >1 additional items.
3900 /// `else => {},`
3901 /// `a, _, b => {},`
3902 under_many_items_and_else = 0b111,
3903
3904 pub const AdditionalItems = enum(u3) {
3905 none = @intFromEnum(SpecialProngs.under),
3906 one = @intFromEnum(SpecialProngs.under_one_item),
3907 many = @intFromEnum(SpecialProngs.under_many_items),
3908 };
3909
3910 pub fn init(has_else: bool, has_under: bool, additional_items: AdditionalItems) SpecialProngs {
3911 const else_bit: u3 = @intFromBool(has_else);
3912 const under_bits: u3 = if (has_under)
3913 @intFromEnum(additional_items)
3914 else
3915 @intFromEnum(SpecialProngs.none);
3916 return @enumFromInt(else_bit | under_bits);
3917 }
3918
3919 pub fn hasElse(special_prongs: SpecialProngs) bool {
3920 return (@intFromEnum(special_prongs) & 0b001) != 0;
3921 }
3922
3923 pub fn hasUnder(special_prongs: SpecialProngs) bool {
3924 return (@intFromEnum(special_prongs) & 0b110) != 0;
3925 }
3926
3927 pub fn hasAdditionalItems(special_prongs: SpecialProngs) bool {
3928 return (@intFromEnum(special_prongs) & 0b100) != 0;
3929 }
3930
3931 pub fn hasOneAdditionalItem(special_prongs: SpecialProngs) bool {
3932 return (@intFromEnum(special_prongs) & 0b110) == @intFromEnum(SpecialProngs.under_one_item);
3933 }
3934
3935 pub fn hasManyAdditionalItems(special_prongs: SpecialProngs) bool {
3936 return (@intFromEnum(special_prongs) & 0b110) == @intFromEnum(SpecialProngs.under_many_items);
3937 }
3938};
38783939
38793940pub const DeclIterator = struct {
38803941 extra_index: u32,
......@@ -4718,7 +4779,7 @@ fn findTrackableSwitch(
47184779 }
47194780
47204781 const has_special = switch (kind) {
4721 .normal => extra.data.bits.specialProng() != .none,
4782 .normal => extra.data.bits.special_prongs != .none,
47224783 .err_union => has_special: {
47234784 // Handle `non_err_body` first.
47244785 const prong_info: Inst.SwitchBlock.ProngInfo = @bitCast(zir.extra[extra_index]);
......@@ -4733,12 +4794,40 @@ fn findTrackableSwitch(
47334794 };
47344795
47354796 if (has_special) {
4736 const prong_info: Inst.SwitchBlock.ProngInfo = @bitCast(zir.extra[extra_index]);
4737 extra_index += 1;
4738 const body = zir.bodySlice(extra_index, prong_info.body_len);
4739 extra_index += body.len;
4797 const has_else = if (kind == .normal)
4798 extra.data.bits.special_prongs.hasElse()
4799 else
4800 true;
4801 if (has_else) {
4802 const prong_info: Inst.SwitchBlock.ProngInfo = @bitCast(zir.extra[extra_index]);
4803 extra_index += 1;
4804 const body = zir.bodySlice(extra_index, prong_info.body_len);
4805 extra_index += body.len;
47404806
4741 try zir.findTrackableBody(gpa, contents, defers, body);
4807 try zir.findTrackableBody(gpa, contents, defers, body);
4808 }
4809 if (kind == .normal) {
4810 const special_prongs = extra.data.bits.special_prongs;
4811
4812 if (special_prongs.hasUnder()) {
4813 var trailing_items_len: u32 = 0;
4814 if (special_prongs.hasOneAdditionalItem()) {
4815 extra_index += 1;
4816 } else if (special_prongs.hasManyAdditionalItems()) {
4817 const items_len = zir.extra[extra_index];
4818 extra_index += 1;
4819 const ranges_len = zir.extra[extra_index];
4820 extra_index += 1;
4821 trailing_items_len = items_len + ranges_len * 2;
4822 }
4823 const prong_info: Inst.SwitchBlock.ProngInfo = @bitCast(zir.extra[extra_index]);
4824 extra_index += 1 + trailing_items_len;
4825 const body = zir.bodySlice(extra_index, prong_info.body_len);
4826 extra_index += body.len;
4827
4828 try zir.findTrackableBody(gpa, contents, defers, body);
4829 }
4830 }
47424831 }
47434832
47444833 {
src/Sema.zig+410-156
......@@ -10927,7 +10927,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1092710927 const switch_src = block.nodeOffset(inst_data.src_node);
1092810928 const switch_src_node_offset = inst_data.src_node;
1092910929 const switch_operand_src = block.src(.{ .node_offset_switch_operand = switch_src_node_offset });
10930 const else_prong_src = block.src(.{ .node_offset_switch_special_prong = switch_src_node_offset });
10930 const else_prong_src = block.src(.{ .node_offset_switch_else_prong = switch_src_node_offset });
1093110931 const extra = sema.code.extraData(Zir.Inst.SwitchBlockErrUnion, inst_data.payload_index);
1093210932 const main_operand_src = block.src(.{ .node_offset_if_cond = extra.data.main_src_node_offset });
1093310933 const main_src = block.src(.{ .node_offset_main_token = extra.data.main_src_node_offset });
......@@ -11121,6 +11121,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1112111121 err_val,
1112211122 operand_err_set_ty,
1112311123 switch_src_node_offset,
11124 null,
1112411125 .{
1112511126 .body = else_case.body,
1112611127 .end = else_case.end,
......@@ -11128,6 +11129,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1112811129 .is_inline = else_case.is_inline,
1112911130 .has_tag_capture = false,
1113011131 },
11132 false,
1113111133 case_vals,
1113211134 scalar_cases_len,
1113311135 multi_cases_len,
......@@ -11199,6 +11201,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1119911201 true,
1120011202 switch_src_node_offset,
1120111203 else_prong_src,
11204 false,
1120211205 undefined,
1120311206 seen_errors,
1120411207 undefined,
......@@ -11206,6 +11209,10 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1120611209 undefined,
1120711210 cond_dbg_node_index,
1120811211 true,
11212 null,
11213 undefined,
11214 &.{},
11215 &.{},
1120911216 );
1121011217
1121111218 try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.CondBr).@"struct".fields.len +
......@@ -11242,12 +11249,14 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1124211249
1124311250 const pt = sema.pt;
1124411251 const zcu = pt.zcu;
11252 const ip = &zcu.intern_pool;
1124511253 const gpa = sema.gpa;
1124611254 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1124711255 const src = block.nodeOffset(inst_data.src_node);
1124811256 const src_node_offset = inst_data.src_node;
1124911257 const operand_src = block.src(.{ .node_offset_switch_operand = src_node_offset });
11250 const special_prong_src = block.src(.{ .node_offset_switch_special_prong = src_node_offset });
11258 const else_prong_src = block.src(.{ .node_offset_switch_else_prong = src_node_offset });
11259 const under_prong_src = block.src(.{ .node_offset_switch_under_prong = src_node_offset });
1125111260 const extra = sema.code.extraData(Zir.Inst.SwitchBlock, inst_data.payload_index);
1125211261
1125311262 const operand: SwitchProngAnalysis.Operand, const raw_operand_ty: Type = op: {
......@@ -11334,27 +11343,63 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1133411343 var case_vals = try std.ArrayListUnmanaged(Air.Inst.Ref).initCapacity(gpa, scalar_cases_len + 2 * multi_cases_len);
1133511344 defer case_vals.deinit(gpa);
1133611345
11337 const special_prong = extra.data.bits.specialProng();
11338 const special: SpecialProng = switch (special_prong) {
11339 .none => .{
11340 .body = &.{},
11341 .end = header_extra_index,
11342 .capture = .none,
11343 .is_inline = false,
11344 .has_tag_capture = false,
11345 },
11346 .under, .@"else" => blk: {
11347 const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[header_extra_index]);
11348 const extra_body_start = header_extra_index + 1;
11349 break :blk .{
11350 .body = sema.code.bodySlice(extra_body_start, info.body_len),
11351 .end = extra_body_start + info.body_len,
11352 .capture = info.capture,
11353 .is_inline = info.is_inline,
11354 .has_tag_capture = info.has_tag_capture,
11355 };
11356 },
11346 var single_absorbed_item: Zir.Inst.Ref = .none;
11347 var absorbed_items: []const Zir.Inst.Ref = &.{};
11348 var absorbed_ranges: []const Zir.Inst.Ref = &.{};
11349
11350 const special_prongs = extra.data.bits.special_prongs;
11351 const has_else = special_prongs.hasElse();
11352 const has_under = special_prongs.hasUnder();
11353 const special_else: SpecialProng = if (has_else) blk: {
11354 const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[header_extra_index]);
11355 const extra_body_start = header_extra_index + 1;
11356 break :blk .{
11357 .body = sema.code.bodySlice(extra_body_start, info.body_len),
11358 .end = extra_body_start + info.body_len,
11359 .capture = info.capture,
11360 .is_inline = info.is_inline,
11361 .has_tag_capture = info.has_tag_capture,
11362 };
11363 } else .{
11364 .body = &.{},
11365 .end = header_extra_index,
11366 .capture = .none,
11367 .is_inline = false,
11368 .has_tag_capture = false,
1135711369 };
11370 const special_under: SpecialProng = if (has_under) blk: {
11371 var extra_index = special_else.end;
11372 var trailing_items_len: usize = 0;
11373 if (special_prongs.hasOneAdditionalItem()) {
11374 single_absorbed_item = @enumFromInt(sema.code.extra[extra_index]);
11375 extra_index += 1;
11376 absorbed_items = @ptrCast(&single_absorbed_item);
11377 } else if (special_prongs.hasManyAdditionalItems()) {
11378 const items_len = sema.code.extra[extra_index];
11379 extra_index += 1;
11380 const ranges_len = sema.code.extra[extra_index];
11381 extra_index += 1;
11382 absorbed_items = sema.code.refSlice(extra_index + 1, items_len);
11383 absorbed_ranges = sema.code.refSlice(extra_index + 1 + items_len, ranges_len * 2);
11384 trailing_items_len = items_len + ranges_len * 2;
11385 }
11386 const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]);
11387 extra_index += 1 + trailing_items_len;
11388 break :blk .{
11389 .body = sema.code.bodySlice(extra_index, info.body_len),
11390 .end = extra_index + info.body_len,
11391 .capture = info.capture,
11392 .is_inline = info.is_inline,
11393 .has_tag_capture = info.has_tag_capture,
11394 };
11395 } else .{
11396 .body = &.{},
11397 .end = special_else.end,
11398 .capture = .none,
11399 .is_inline = false,
11400 .has_tag_capture = false,
11401 };
11402 const special_end = special_under.end;
1135811403
1135911404 // Duplicate checking variables later also used for `inline else`.
1136011405 var seen_enum_fields: []?LazySrcLoc = &.{};
......@@ -11374,7 +11419,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1137411419 var else_error_ty: ?Type = null;
1137511420
1137611421 // Validate usage of '_' prongs.
11377 if (special_prong == .under and !raw_operand_ty.isNonexhaustiveEnum(zcu)) {
11422 if (has_under and !raw_operand_ty.isNonexhaustiveEnum(zcu)) {
1137811423 const msg = msg: {
1137911424 const msg = try sema.errMsg(
1138011425 src,
......@@ -11383,7 +11428,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1138311428 );
1138411429 errdefer msg.destroy(gpa);
1138511430 try sema.errNote(
11386 special_prong_src,
11431 under_prong_src,
1138711432 msg,
1138811433 "'_' prong here",
1138911434 .{},
......@@ -11408,7 +11453,23 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1140811453 @memset(seen_enum_fields, null);
1140911454 // `range_set` is used for non-exhaustive enum values that do not correspond to any tags.
1141011455
11411 var extra_index: usize = special.end;
11456 for (absorbed_items, 0..) |item_ref, item_i| {
11457 _ = try sema.validateSwitchItemEnum(
11458 block,
11459 seen_enum_fields,
11460 &range_set,
11461 item_ref,
11462 cond_ty,
11463 block.src(.{ .switch_case_item = .{
11464 .switch_node_offset = src_node_offset,
11465 .case_idx = .special_under,
11466 .item_idx = .{ .kind = .single, .index = @intCast(item_i) },
11467 } }),
11468 );
11469 }
11470 try sema.validateSwitchNoRange(block, @intCast(absorbed_ranges.len), cond_ty, src_node_offset);
11471
11472 var extra_index: usize = special_end;
1141211473 {
1141311474 var scalar_i: u32 = 0;
1141411475 while (scalar_i < scalar_cases_len) : (scalar_i += 1) {
......@@ -11466,13 +11527,22 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1146611527 if (seen_src == null) break false;
1146711528 } else true;
1146811529
11469 if (special_prong == .@"else") {
11470 if (all_tags_handled and !cond_ty.isNonexhaustiveEnum(zcu)) return sema.fail(
11471 block,
11472 special_prong_src,
11473 "unreachable else prong; all cases already handled",
11474 .{},
11475 );
11530 if (has_else) {
11531 if (all_tags_handled) {
11532 if (cond_ty.isNonexhaustiveEnum(zcu)) {
11533 if (has_under) return sema.fail(
11534 block,
11535 else_prong_src,
11536 "unreachable else prong; all explicit cases already handled",
11537 .{},
11538 );
11539 } else return sema.fail(
11540 block,
11541 else_prong_src,
11542 "unreachable else prong; all cases already handled",
11543 .{},
11544 );
11545 }
1147611546 } else if (!all_tags_handled) {
1147711547 const msg = msg: {
1147811548 const msg = try sema.errMsg(
......@@ -11490,7 +11560,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1149011560 i,
1149111561 msg,
1149211562 "unhandled enumeration value: '{f}'",
11493 .{field_name.fmt(&zcu.intern_pool)},
11563 .{field_name.fmt(ip)},
1149411564 );
1149511565 }
1149611566 try sema.errNote(
......@@ -11502,11 +11572,11 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1150211572 break :msg msg;
1150311573 };
1150411574 return sema.failWithOwnedErrorMsg(block, msg);
11505 } else if (special_prong == .none and cond_ty.isNonexhaustiveEnum(zcu) and !union_originally) {
11575 } else if (special_prongs == .none and cond_ty.isNonexhaustiveEnum(zcu) and !union_originally) {
1150611576 return sema.fail(
1150711577 block,
1150811578 src,
11509 "switch on non-exhaustive enum must include 'else' or '_' prong",
11579 "switch on non-exhaustive enum must include 'else' or '_' prong or both",
1151011580 .{},
1151111581 );
1151211582 }
......@@ -11520,11 +11590,11 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1152011590 inst_data,
1152111591 scalar_cases_len,
1152211592 multi_cases_len,
11523 .{ .body = special.body, .end = special.end, .src = special_prong_src },
11524 special_prong == .@"else",
11593 .{ .body = special_else.body, .end = special_else.end, .src = else_prong_src },
11594 has_else,
1152511595 ),
1152611596 .int, .comptime_int => {
11527 var extra_index: usize = special.end;
11597 var extra_index: usize = special_end;
1152811598 {
1152911599 var scalar_i: u32 = 0;
1153011600 while (scalar_i < scalar_cases_len) : (scalar_i += 1) {
......@@ -11606,10 +11676,10 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1160611676 const min_int = try cond_ty.minInt(pt, cond_ty);
1160711677 const max_int = try cond_ty.maxInt(pt, cond_ty);
1160811678 if (try range_set.spans(min_int.toIntern(), max_int.toIntern())) {
11609 if (special_prong == .@"else") {
11679 if (has_else) {
1161011680 return sema.fail(
1161111681 block,
11612 special_prong_src,
11682 else_prong_src,
1161311683 "unreachable else prong; all cases already handled",
1161411684 .{},
1161511685 );
......@@ -11617,7 +11687,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1161711687 break :check_range;
1161811688 }
1161911689 }
11620 if (special_prong != .@"else") {
11690 if (special_prongs == .none) {
1162111691 return sema.fail(
1162211692 block,
1162311693 src,
......@@ -11628,7 +11698,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1162811698 }
1162911699 },
1163011700 .bool => {
11631 var extra_index: usize = special.end;
11701 var extra_index: usize = special_end;
1163211702 {
1163311703 var scalar_i: u32 = 0;
1163411704 while (scalar_i < scalar_cases_len) : (scalar_i += 1) {
......@@ -11680,31 +11750,28 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1168011750 try sema.validateSwitchNoRange(block, ranges_len, cond_ty, src_node_offset);
1168111751 }
1168211752 }
11683 switch (special_prong) {
11684 .@"else" => {
11685 if (true_count + false_count == 2) {
11686 return sema.fail(
11687 block,
11688 special_prong_src,
11689 "unreachable else prong; all cases already handled",
11690 .{},
11691 );
11692 }
11693 },
11694 .under, .none => {
11695 if (true_count + false_count < 2) {
11696 return sema.fail(
11697 block,
11698 src,
11699 "switch must handle all possibilities",
11700 .{},
11701 );
11702 }
11703 },
11753 if (has_else) {
11754 if (true_count + false_count == 2) {
11755 return sema.fail(
11756 block,
11757 else_prong_src,
11758 "unreachable else prong; all cases already handled",
11759 .{},
11760 );
11761 }
11762 } else {
11763 if (true_count + false_count < 2) {
11764 return sema.fail(
11765 block,
11766 src,
11767 "switch must handle all possibilities",
11768 .{},
11769 );
11770 }
1170411771 }
1170511772 },
1170611773 .enum_literal, .void, .@"fn", .pointer, .type => {
11707 if (special_prong != .@"else") {
11774 if (!has_else) {
1170811775 return sema.fail(
1170911776 block,
1171011777 src,
......@@ -11716,7 +11783,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1171611783 var seen_values = ValueSrcMap{};
1171711784 defer seen_values.deinit(gpa);
1171811785
11719 var extra_index: usize = special.end;
11786 var extra_index: usize = special_end;
1172011787 {
1172111788 var scalar_i: u32 = 0;
1172211789 while (scalar_i < scalar_cases_len) : (scalar_i += 1) {
......@@ -11789,6 +11856,16 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1178911856 }),
1179011857 }
1179111858
11859 var special_members_only: ?SpecialProng = null;
11860 var special_members_only_src: LazySrcLoc = undefined;
11861 const special_generic, const special_generic_src = if (has_under) b: {
11862 if (has_else) {
11863 special_members_only = special_else;
11864 special_members_only_src = else_prong_src;
11865 }
11866 break :b .{ special_under, under_prong_src };
11867 } else .{ special_else, else_prong_src };
11868
1179211869 const spa: SwitchProngAnalysis = .{
1179311870 .sema = sema,
1179411871 .parent_block = block,
......@@ -11835,11 +11912,14 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1183511912 defer child_block.instructions.deinit(gpa);
1183611913 defer merges.deinit(gpa);
1183711914
11838 if (scalar_cases_len + multi_cases_len == 0 and !special.is_inline) {
11915 if (scalar_cases_len + multi_cases_len == 0 and
11916 special_members_only == null and
11917 !special_generic.is_inline)
11918 {
1183911919 if (empty_enum) {
1184011920 return .void_value;
1184111921 }
11842 if (special_prong == .none) {
11922 if (special_prongs == .none) {
1184311923 return sema.fail(block, src, "switch must handle all possibilities", .{});
1184411924 }
1184511925 const init_cond = switch (operand) {
......@@ -11853,7 +11933,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1185311933 const ok = try block.addUnOp(.is_named_enum_value, init_cond);
1185411934 try sema.addSafetyCheck(block, src, ok, .corrupt_switch);
1185511935 }
11856 if (err_set and try sema.maybeErrorUnwrap(block, special.body, init_cond, operand_src, false)) {
11936 if (err_set and try sema.maybeErrorUnwrap(block, special_generic.body, init_cond, operand_src, false)) {
1185711937 return .unreachable_value;
1185811938 }
1185911939 }
......@@ -11873,7 +11953,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1187311953 cond_ty,
1187411954 cond_val,
1187511955 src_node_offset,
11876 special,
11956 special_members_only,
11957 special_generic,
11958 has_under,
1187711959 case_vals,
1187811960 scalar_cases_len,
1187911961 multi_cases_len,
......@@ -11883,15 +11965,19 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1188311965 );
1188411966 }
1188511967
11886 if (scalar_cases_len + multi_cases_len == 0 and !special.is_inline and !extra.data.bits.has_continue) {
11968 if (scalar_cases_len + multi_cases_len == 0 and
11969 special_members_only == null and
11970 !special_generic.is_inline and
11971 !extra.data.bits.has_continue)
11972 {
1188711973 return spa.resolveProngComptime(
1188811974 &child_block,
1188911975 .special,
11890 special.body,
11891 special.capture,
11976 special_generic.body,
11977 special_generic.capture,
1189211978 block.src(.{ .switch_capture = .{
1189311979 .switch_node_offset = src_node_offset,
11894 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
11980 .case_idx = if (has_under) .special_under else .special_else,
1189511981 } }),
1189611982 undefined, // case_vals may be undefined for special prongs
1189711983 .none,
......@@ -11907,6 +11993,87 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1190711993 unreachable;
1190811994 }
1190911995
11996 var extra_case_vals: struct {
11997 items: std.ArrayListUnmanaged(Air.Inst.Ref),
11998 ranges: std.ArrayListUnmanaged([2]Air.Inst.Ref),
11999 } = .{ .items = .empty, .ranges = .empty };
12000 defer {
12001 extra_case_vals.items.deinit(gpa);
12002 extra_case_vals.ranges.deinit(gpa);
12003 }
12004
12005 // Runtime switch, if we have a special_members_only prong we need to unroll
12006 // it to a prong with explicit items.
12007 // Although this is potentially the same as `inline else` it does not count
12008 // towards the backward branch quota because it's an implementation detail.
12009 if (special_members_only != null) gen: {
12010 assert(cond_ty.isNonexhaustiveEnum(zcu));
12011
12012 var min_i: usize = math.maxInt(usize);
12013 var max_i: usize = 0;
12014 var seen_field_count: usize = 0;
12015 for (seen_enum_fields, 0..) |seen, enum_i| {
12016 if (seen != null) {
12017 seen_field_count += 1;
12018 } else {
12019 min_i = @min(min_i, enum_i);
12020 max_i = @max(max_i, enum_i);
12021 }
12022 }
12023 if (min_i == max_i) {
12024 seen_enum_fields[min_i] = special_members_only_src;
12025 const item_val = try pt.enumValueFieldIndex(cond_ty, @intCast(min_i));
12026 const item_ref = Air.internedToRef(item_val.toIntern());
12027 try extra_case_vals.items.append(gpa, item_ref);
12028 break :gen;
12029 }
12030 const missing_field_count = seen_enum_fields.len - seen_field_count;
12031
12032 extra_case_vals.items = try .initCapacity(gpa, missing_field_count / 2);
12033 extra_case_vals.ranges = try .initCapacity(gpa, missing_field_count / 4);
12034 const int_ty = cond_ty.intTagType(zcu);
12035
12036 var last_val = try pt.enumValueFieldIndex(cond_ty, @intCast(min_i));
12037 var first_ref = Air.internedToRef(last_val.toIntern());
12038 seen_enum_fields[min_i] = special_members_only_src;
12039 for (seen_enum_fields[(min_i + 1)..(max_i + 1)], (min_i + 1)..) |seen, enum_i| {
12040 if (seen != null) continue;
12041 seen_enum_fields[enum_i] = special_members_only_src;
12042
12043 const item_val = try pt.enumValueFieldIndex(cond_ty, @intCast(enum_i));
12044 const item_ref = Air.internedToRef(item_val.toIntern());
12045
12046 const is_next = is_next: {
12047 const prev_int = ip.indexToKey(last_val.toIntern()).enum_tag.int;
12048
12049 const result = try arith.incrementDefinedInt(sema, int_ty, .fromInterned(prev_int));
12050 if (result.overflow) break :is_next false;
12051
12052 const item_int = ip.indexToKey(item_val.toIntern()).enum_tag.int;
12053 break :is_next try sema.valuesEqual(.fromInterned(item_int), result.val, int_ty);
12054 };
12055
12056 if (is_next) {
12057 last_val = item_val;
12058 } else {
12059 const last_ref = Air.internedToRef(last_val.toIntern());
12060 if (first_ref == last_ref) {
12061 try extra_case_vals.items.append(gpa, first_ref);
12062 } else {
12063 try extra_case_vals.ranges.append(gpa, .{ first_ref, last_ref });
12064 }
12065 first_ref = item_ref;
12066 last_val = item_val;
12067 }
12068 }
12069 const last_ref = Air.internedToRef(last_val.toIntern());
12070 if (first_ref == last_ref) {
12071 try extra_case_vals.items.append(gpa, first_ref);
12072 } else {
12073 try extra_case_vals.ranges.append(gpa, .{ first_ref, last_ref });
12074 }
12075 }
12076
1191012077 const air_switch_ref = try sema.analyzeSwitchRuntimeBlock(
1191112078 spa,
1191212079 &child_block,
......@@ -11918,14 +12085,15 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1191812085 cond_ty,
1191912086 operand_src,
1192012087 case_vals,
11921 special,
12088 special_generic,
1192212089 scalar_cases_len,
1192312090 multi_cases_len,
1192412091 union_originally,
1192512092 raw_operand_ty,
1192612093 err_set,
1192712094 src_node_offset,
11928 special_prong_src,
12095 special_generic_src,
12096 has_under,
1192912097 seen_enum_fields,
1193012098 seen_errors,
1193112099 range_set,
......@@ -11933,6 +12101,10 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1193312101 false_count,
1193412102 cond_dbg_node_index,
1193512103 false,
12104 special_members_only,
12105 special_members_only_src,
12106 extra_case_vals.items.items,
12107 extra_case_vals.ranges.items,
1193612108 );
1193712109
1193812110 for (merges.extra_insts.items, merges.extra_src_locs.items) |placeholder_inst, dispatch_src| {
......@@ -12016,14 +12188,15 @@ fn analyzeSwitchRuntimeBlock(
1201612188 operand_ty: Type,
1201712189 operand_src: LazySrcLoc,
1201812190 case_vals: std.ArrayListUnmanaged(Air.Inst.Ref),
12019 special: SpecialProng,
12191 else_prong: SpecialProng,
1202012192 scalar_cases_len: usize,
1202112193 multi_cases_len: usize,
1202212194 union_originally: bool,
1202312195 maybe_union_ty: Type,
1202412196 err_set: bool,
1202512197 switch_node_offset: std.zig.Ast.Node.Offset,
12026 special_prong_src: LazySrcLoc,
12198 else_prong_src: LazySrcLoc,
12199 else_prong_is_underscore: bool,
1202712200 seen_enum_fields: []?LazySrcLoc,
1202812201 seen_errors: SwitchErrorSet,
1202912202 range_set: RangeSet,
......@@ -12031,6 +12204,11 @@ fn analyzeSwitchRuntimeBlock(
1203112204 false_count: u8,
1203212205 cond_dbg_node_index: Zir.Inst.Index,
1203312206 allow_err_code_unwrap: bool,
12207 extra_prong: ?SpecialProng,
12208 /// May be `undefined` if `extra_prong` is `null`
12209 extra_prong_src: LazySrcLoc,
12210 extra_prong_items: []const Air.Inst.Ref,
12211 extra_prong_ranges: []const [2]Air.Inst.Ref,
1203412212) CompileError!Air.Inst.Ref {
1203512213 const pt = sema.pt;
1203612214 const zcu = pt.zcu;
......@@ -12054,7 +12232,7 @@ fn analyzeSwitchRuntimeBlock(
1205412232 case_block.need_debug_scope = null; // this body is emitted regardless
1205512233 defer case_block.instructions.deinit(gpa);
1205612234
12057 var extra_index: usize = special.end;
12235 var extra_index: usize = else_prong.end;
1205812236
1205912237 var scalar_i: usize = 0;
1206012238 while (scalar_i < scalar_cases_len) : (scalar_i += 1) {
......@@ -12116,23 +12294,42 @@ fn analyzeSwitchRuntimeBlock(
1211612294
1211712295 var cases_len = scalar_cases_len;
1211812296 var case_val_idx: usize = scalar_cases_len;
12297 const multi_cases_len_with_extra_prong = multi_cases_len + @intFromBool(extra_prong != null);
1211912298 var multi_i: u32 = 0;
12120 while (multi_i < multi_cases_len) : (multi_i += 1) {
12121 const items_len = sema.code.extra[extra_index];
12122 extra_index += 1;
12123 const ranges_len = sema.code.extra[extra_index];
12124 extra_index += 1;
12125 const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]);
12126 extra_index += 1 + items_len + 2 * ranges_len;
12299 while (multi_i < multi_cases_len_with_extra_prong) : (multi_i += 1) {
12300 const is_extra_prong = multi_i == multi_cases_len;
12301 var items: []const Air.Inst.Ref = undefined;
12302 var info: Zir.Inst.SwitchBlock.ProngInfo = undefined;
12303 var ranges: []const [2]Air.Inst.Ref = undefined;
12304 var body: []const Zir.Inst.Index = undefined;
12305 if (is_extra_prong) {
12306 const prong = extra_prong.?;
12307 items = extra_prong_items;
12308 ranges = extra_prong_ranges;
12309 body = prong.body;
12310 info = .{
12311 .body_len = undefined,
12312 .capture = prong.capture,
12313 .is_inline = prong.is_inline,
12314 .has_tag_capture = prong.has_tag_capture,
12315 };
12316 } else {
12317 @branchHint(.likely);
12318 const items_len = sema.code.extra[extra_index];
12319 extra_index += 1;
12320 const ranges_len = sema.code.extra[extra_index];
12321 extra_index += 1;
12322 info = @bitCast(sema.code.extra[extra_index]);
12323 extra_index += 1 + items_len + ranges_len * 2;
1212712324
12128 const items = case_vals.items[case_val_idx..][0..items_len];
12129 case_val_idx += items_len;
12130 // TODO: @ptrCast slice once Sema supports it
12131 const ranges: []const [2]Air.Inst.Ref = @as([*]const [2]Air.Inst.Ref, @ptrCast(case_vals.items[case_val_idx..]))[0..ranges_len];
12132 case_val_idx += ranges_len * 2;
12325 items = case_vals.items[case_val_idx..][0..items_len];
12326 case_val_idx += items_len;
12327 ranges = @ptrCast(case_vals.items[case_val_idx..][0 .. ranges_len * 2]);
12328 case_val_idx += ranges_len * 2;
1213312329
12134 const body = sema.code.bodySlice(extra_index, info.body_len);
12135 extra_index += info.body_len;
12330 body = sema.code.bodySlice(extra_index, info.body_len);
12331 extra_index += info.body_len;
12332 }
1213612333
1213712334 case_block.instructions.shrinkRetainingCapacity(0);
1213812335 case_block.error_return_trace_index = child_block.error_return_trace_index;
......@@ -12142,14 +12339,29 @@ fn analyzeSwitchRuntimeBlock(
1214212339 var emit_bb = false;
1214312340
1214412341 for (ranges, 0..) |range_items, range_i| {
12145 var item = sema.resolveConstDefinedValue(block, LazySrcLoc.unneeded, range_items[0], undefined) catch unreachable;
12146 const item_last = sema.resolveConstDefinedValue(block, LazySrcLoc.unneeded, range_items[1], undefined) catch unreachable;
12342 var item = sema.resolveConstDefinedValue(block, .unneeded, range_items[0], undefined) catch unreachable;
12343 const item_last = sema.resolveConstDefinedValue(block, .unneeded, range_items[1], undefined) catch unreachable;
1214712344
1214812345 while (item.compareScalar(.lte, item_last, operand_ty, zcu)) : ({
1214912346 // Previous validation has resolved any possible lazy values.
12150 const result = try arith.incrementDefinedInt(sema, operand_ty, item);
12347 const int_val: Value, const int_ty: Type = switch (operand_ty.zigTypeTag(zcu)) {
12348 .int => .{ item, operand_ty },
12349 .@"enum" => b: {
12350 const int_val = Value.fromInterned(ip.indexToKey(item.toIntern()).enum_tag.int);
12351 break :b .{ int_val, int_val.typeOf(zcu) };
12352 },
12353 else => unreachable,
12354 };
12355 const result = try arith.incrementDefinedInt(sema, int_ty, int_val);
1215112356 assert(!result.overflow);
12152 item = result.val;
12357 item = switch (operand_ty.zigTypeTag(zcu)) {
12358 .int => result.val,
12359 .@"enum" => .fromInterned(try pt.intern(.{ .enum_tag = .{
12360 .ty = operand_ty.toIntern(),
12361 .int = result.val.toIntern(),
12362 } })),
12363 else => unreachable,
12364 };
1215312365 }) {
1215412366 cases_len += 1;
1215512367
......@@ -12158,11 +12370,14 @@ fn analyzeSwitchRuntimeBlock(
1215812370 case_block.instructions.shrinkRetainingCapacity(0);
1215912371 case_block.error_return_trace_index = child_block.error_return_trace_index;
1216012372
12161 if (emit_bb) try sema.emitBackwardBranch(block, block.src(.{ .switch_case_item = .{
12162 .switch_node_offset = switch_node_offset,
12163 .case_idx = .{ .kind = .multi, .index = @intCast(multi_i) },
12164 .item_idx = .{ .kind = .range, .index = @intCast(range_i) },
12165 } }));
12373 if (emit_bb) {
12374 const bb_src = if (is_extra_prong) extra_prong_src else block.src(.{ .switch_case_item = .{
12375 .switch_node_offset = switch_node_offset,
12376 .case_idx = .{ .kind = .multi, .index = @intCast(multi_i) },
12377 .item_idx = .{ .kind = .range, .index = @intCast(range_i) },
12378 } });
12379 try sema.emitBackwardBranch(block, bb_src);
12380 }
1216612381 emit_bb = true;
1216712382
1216812383 const prong_hint = try spa.analyzeProngRuntime(
......@@ -12207,11 +12422,14 @@ fn analyzeSwitchRuntimeBlock(
1220712422 break :blk field_ty.zigTypeTag(zcu) != .noreturn;
1220812423 } else true;
1220912424
12210 if (emit_bb) try sema.emitBackwardBranch(block, block.src(.{ .switch_case_item = .{
12211 .switch_node_offset = switch_node_offset,
12212 .case_idx = .{ .kind = .multi, .index = @intCast(multi_i) },
12213 .item_idx = .{ .kind = .single, .index = @intCast(item_i) },
12214 } }));
12425 if (emit_bb) {
12426 const bb_src = if (is_extra_prong) extra_prong_src else block.src(.{ .switch_case_item = .{
12427 .switch_node_offset = switch_node_offset,
12428 .case_idx = .{ .kind = .multi, .index = @intCast(multi_i) },
12429 .item_idx = .{ .kind = .single, .index = @intCast(item_i) },
12430 } });
12431 try sema.emitBackwardBranch(block, bb_src);
12432 }
1221512433 emit_bb = true;
1221612434
1221712435 const prong_hint: std.builtin.BranchHint = if (analyze_body) h: {
......@@ -12287,11 +12505,11 @@ fn analyzeSwitchRuntimeBlock(
1228712505 try branch_hints.append(gpa, prong_hint);
1228812506
1228912507 try cases_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.SwitchBr.Case).@"struct".fields.len +
12290 items.len + 2 * ranges_len +
12508 items.len + ranges.len * 2 +
1229112509 case_block.instructions.items.len);
1229212510 cases_extra.appendSliceAssumeCapacity(&payloadToExtraItems(Air.SwitchBr.Case{
1229312511 .items_len = @intCast(items.len),
12294 .ranges_len = @intCast(ranges_len),
12512 .ranges_len = @intCast(ranges.len),
1229512513 .body_len = @intCast(case_block.instructions.items.len),
1229612514 }));
1229712515
......@@ -12308,12 +12526,14 @@ fn analyzeSwitchRuntimeBlock(
1230812526 cases_extra.appendSliceAssumeCapacity(@ptrCast(case_block.instructions.items));
1230912527 }
1231012528
12311 const else_body: []const Air.Inst.Index = if (special.body.len != 0 or case_block.wantSafety()) else_body: {
12529 const else_body: []const Air.Inst.Index = if (else_prong.body.len != 0 or case_block.wantSafety()) else_body: {
1231212530 var emit_bb = false;
12313 if (special.is_inline) switch (operand_ty.zigTypeTag(zcu)) {
12531 // If this is true we must have a 'true' else prong and not an underscore because
12532 // underscore prongs can never be inlined. We've already checked for this.
12533 if (else_prong.is_inline) switch (operand_ty.zigTypeTag(zcu)) {
1231412534 .@"enum" => {
1231512535 if (operand_ty.isNonexhaustiveEnum(zcu) and !union_originally) {
12316 return sema.fail(block, special_prong_src, "cannot enumerate values of type '{f}' for 'inline else'", .{
12536 return sema.fail(block, else_prong_src, "cannot enumerate values of type '{f}' for 'inline else'", .{
1231712537 operand_ty.fmt(pt),
1231812538 });
1231912539 }
......@@ -12332,22 +12552,22 @@ fn analyzeSwitchRuntimeBlock(
1233212552 break :blk field_ty.zigTypeTag(zcu) != .noreturn;
1233312553 } else true;
1233412554
12335 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
12555 if (emit_bb) try sema.emitBackwardBranch(block, else_prong_src);
1233612556 emit_bb = true;
1233712557
1233812558 const prong_hint: std.builtin.BranchHint = if (analyze_body) h: {
1233912559 break :h try spa.analyzeProngRuntime(
1234012560 &case_block,
1234112561 .special,
12342 special.body,
12343 special.capture,
12562 else_prong.body,
12563 else_prong.capture,
1234412564 child_block.src(.{ .switch_capture = .{
1234512565 .switch_node_offset = switch_node_offset,
12346 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12566 .case_idx = .special_else,
1234712567 } }),
1234812568 &.{item_ref},
1234912569 item_ref,
12350 special.has_tag_capture,
12570 else_prong.has_tag_capture,
1235112571 );
1235212572 } else h: {
1235312573 _ = try case_block.addNoOp(.unreach);
......@@ -12369,7 +12589,7 @@ fn analyzeSwitchRuntimeBlock(
1236912589 },
1237012590 .error_set => {
1237112591 if (operand_ty.isAnyError(zcu)) {
12372 return sema.fail(block, special_prong_src, "cannot enumerate values of type '{f}' for 'inline else'", .{
12592 return sema.fail(block, else_prong_src, "cannot enumerate values of type '{f}' for 'inline else'", .{
1237312593 operand_ty.fmt(pt),
1237412594 });
1237512595 }
......@@ -12388,21 +12608,21 @@ fn analyzeSwitchRuntimeBlock(
1238812608 case_block.instructions.shrinkRetainingCapacity(0);
1238912609 case_block.error_return_trace_index = child_block.error_return_trace_index;
1239012610
12391 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
12611 if (emit_bb) try sema.emitBackwardBranch(block, else_prong_src);
1239212612 emit_bb = true;
1239312613
1239412614 const prong_hint = try spa.analyzeProngRuntime(
1239512615 &case_block,
1239612616 .special,
12397 special.body,
12398 special.capture,
12617 else_prong.body,
12618 else_prong.capture,
1239912619 child_block.src(.{ .switch_capture = .{
1240012620 .switch_node_offset = switch_node_offset,
12401 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12621 .case_idx = .special_else,
1240212622 } }),
1240312623 &.{item_ref},
1240412624 item_ref,
12405 special.has_tag_capture,
12625 else_prong.has_tag_capture,
1240612626 );
1240712627 try branch_hints.append(gpa, prong_hint);
1240812628
......@@ -12428,21 +12648,21 @@ fn analyzeSwitchRuntimeBlock(
1242812648 case_block.instructions.shrinkRetainingCapacity(0);
1242912649 case_block.error_return_trace_index = child_block.error_return_trace_index;
1243012650
12431 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
12651 if (emit_bb) try sema.emitBackwardBranch(block, else_prong_src);
1243212652 emit_bb = true;
1243312653
1243412654 const prong_hint = try spa.analyzeProngRuntime(
1243512655 &case_block,
1243612656 .special,
12437 special.body,
12438 special.capture,
12657 else_prong.body,
12658 else_prong.capture,
1243912659 child_block.src(.{ .switch_capture = .{
1244012660 .switch_node_offset = switch_node_offset,
12441 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12661 .case_idx = .special_else,
1244212662 } }),
1244312663 &.{item_ref},
1244412664 item_ref,
12445 special.has_tag_capture,
12665 else_prong.has_tag_capture,
1244612666 );
1244712667 try branch_hints.append(gpa, prong_hint);
1244812668
......@@ -12465,21 +12685,21 @@ fn analyzeSwitchRuntimeBlock(
1246512685 case_block.instructions.shrinkRetainingCapacity(0);
1246612686 case_block.error_return_trace_index = child_block.error_return_trace_index;
1246712687
12468 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
12688 if (emit_bb) try sema.emitBackwardBranch(block, else_prong_src);
1246912689 emit_bb = true;
1247012690
1247112691 const prong_hint = try spa.analyzeProngRuntime(
1247212692 &case_block,
1247312693 .special,
12474 special.body,
12475 special.capture,
12694 else_prong.body,
12695 else_prong.capture,
1247612696 child_block.src(.{ .switch_capture = .{
1247712697 .switch_node_offset = switch_node_offset,
12478 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12698 .case_idx = .special_else,
1247912699 } }),
1248012700 &.{.bool_true},
1248112701 .bool_true,
12482 special.has_tag_capture,
12702 else_prong.has_tag_capture,
1248312703 );
1248412704 try branch_hints.append(gpa, prong_hint);
1248512705
......@@ -12500,21 +12720,21 @@ fn analyzeSwitchRuntimeBlock(
1250012720 case_block.instructions.shrinkRetainingCapacity(0);
1250112721 case_block.error_return_trace_index = child_block.error_return_trace_index;
1250212722
12503 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
12723 if (emit_bb) try sema.emitBackwardBranch(block, else_prong_src);
1250412724 emit_bb = true;
1250512725
1250612726 const prong_hint = try spa.analyzeProngRuntime(
1250712727 &case_block,
1250812728 .special,
12509 special.body,
12510 special.capture,
12729 else_prong.body,
12730 else_prong.capture,
1251112731 child_block.src(.{ .switch_capture = .{
1251212732 .switch_node_offset = switch_node_offset,
12513 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12733 .case_idx = .special_else,
1251412734 } }),
1251512735 &.{.bool_false},
1251612736 .bool_false,
12517 special.has_tag_capture,
12737 else_prong.has_tag_capture,
1251812738 );
1251912739 try branch_hints.append(gpa, prong_hint);
1252012740
......@@ -12530,7 +12750,7 @@ fn analyzeSwitchRuntimeBlock(
1253012750 cases_extra.appendSliceAssumeCapacity(@ptrCast(case_block.instructions.items));
1253112751 }
1253212752 },
12533 else => return sema.fail(block, special_prong_src, "cannot enumerate values of type '{f}' for 'inline else'", .{
12753 else => return sema.fail(block, else_prong_src, "cannot enumerate values of type '{f}' for 'inline else'", .{
1253412754 operand_ty.fmt(pt),
1253512755 }),
1253612756 };
......@@ -12539,7 +12759,7 @@ fn analyzeSwitchRuntimeBlock(
1253912759 case_block.error_return_trace_index = child_block.error_return_trace_index;
1254012760
1254112761 if (zcu.backendSupportsFeature(.is_named_enum_value) and
12542 special.body.len != 0 and block.wantSafety() and
12762 else_prong.body.len != 0 and block.wantSafety() and
1254312763 operand_ty.zigTypeTag(zcu) == .@"enum" and
1254412764 (!operand_ty.isNonexhaustiveEnum(zcu) or union_originally))
1254512765 {
......@@ -12548,7 +12768,12 @@ fn analyzeSwitchRuntimeBlock(
1254812768 try sema.addSafetyCheck(&case_block, src, ok, .corrupt_switch);
1254912769 }
1255012770
12551 const analyze_body = if (union_originally and !special.is_inline)
12771 const else_src_idx: LazySrcLoc.Offset.SwitchCaseIndex = if (else_prong_is_underscore)
12772 .special_under
12773 else
12774 .special_else;
12775
12776 const analyze_body = if (union_originally and !else_prong.is_inline)
1255212777 for (seen_enum_fields, 0..) |seen_field, index| {
1255312778 if (seen_field != null) continue;
1255412779 const union_obj = zcu.typeToUnion(maybe_union_ty).?;
......@@ -12557,20 +12782,20 @@ fn analyzeSwitchRuntimeBlock(
1255712782 } else false
1255812783 else
1255912784 true;
12560 const else_hint: std.builtin.BranchHint = if (special.body.len != 0 and err_set and
12561 try sema.maybeErrorUnwrap(&case_block, special.body, operand, operand_src, allow_err_code_unwrap))
12785 const else_hint: std.builtin.BranchHint = if (else_prong.body.len != 0 and err_set and
12786 try sema.maybeErrorUnwrap(&case_block, else_prong.body, operand, operand_src, allow_err_code_unwrap))
1256212787 h: {
1256312788 // nothing to do here. weight against error branch
1256412789 break :h .unlikely;
12565 } else if (special.body.len != 0 and analyze_body and !special.is_inline) h: {
12790 } else if (else_prong.body.len != 0 and analyze_body and !else_prong.is_inline) h: {
1256612791 break :h try spa.analyzeProngRuntime(
1256712792 &case_block,
1256812793 .special,
12569 special.body,
12570 special.capture,
12794 else_prong.body,
12795 else_prong.capture,
1257112796 child_block.src(.{ .switch_capture = .{
1257212797 .switch_node_offset = switch_node_offset,
12573 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
12798 .case_idx = else_src_idx,
1257412799 } }),
1257512800 undefined, // case_vals may be undefined for special prongs
1257612801 .none,
......@@ -12644,7 +12869,9 @@ fn resolveSwitchComptimeLoop(
1264412869 cond_ty: Type,
1264512870 init_cond_val: Value,
1264612871 switch_node_offset: std.zig.Ast.Node.Offset,
12647 special: SpecialProng,
12872 special_members_only: ?SpecialProng,
12873 special_generic: SpecialProng,
12874 special_generic_is_under: bool,
1264812875 case_vals: std.ArrayListUnmanaged(Air.Inst.Ref),
1264912876 scalar_cases_len: u32,
1265012877 multi_cases_len: u32,
......@@ -12664,7 +12891,9 @@ fn resolveSwitchComptimeLoop(
1266412891 cond_val,
1266512892 cond_ty,
1266612893 switch_node_offset,
12667 special,
12894 special_members_only,
12895 special_generic,
12896 special_generic_is_under,
1266812897 case_vals,
1266912898 scalar_cases_len,
1267012899 multi_cases_len,
......@@ -12712,17 +12941,20 @@ fn resolveSwitchComptime(
1271212941 operand_val: Value,
1271312942 operand_ty: Type,
1271412943 switch_node_offset: std.zig.Ast.Node.Offset,
12715 special: SpecialProng,
12944 special_members_only: ?SpecialProng,
12945 special_generic: SpecialProng,
12946 special_generic_is_under: bool,
1271612947 case_vals: std.ArrayListUnmanaged(Air.Inst.Ref),
1271712948 scalar_cases_len: u32,
1271812949 multi_cases_len: u32,
1271912950 err_set: bool,
1272012951 empty_enum: bool,
1272112952) CompileError!Air.Inst.Ref {
12953 const zcu = sema.pt.zcu;
1272212954 const merges = &child_block.label.?.merges;
1272312955 const resolved_operand_val = try sema.resolveLazyValue(operand_val);
1272412956
12725 var extra_index: usize = special.end;
12957 var extra_index: usize = special_generic.end;
1272612958 {
1272712959 var scalar_i: usize = 0;
1272812960 while (scalar_i < scalar_cases_len) : (scalar_i += 1) {
......@@ -12823,23 +13055,45 @@ fn resolveSwitchComptime(
1282313055 extra_index += info.body_len;
1282413056 }
1282513057 }
12826 if (err_set) try sema.maybeErrorUnwrapComptime(child_block, special.body, cond_operand);
13058 if (err_set) try sema.maybeErrorUnwrapComptime(child_block, special_generic.body, cond_operand);
1282713059 if (empty_enum) {
1282813060 return .void_value;
1282913061 }
13062 if (special_members_only) |special| {
13063 assert(operand_ty.isNonexhaustiveEnum(zcu));
13064 if (operand_ty.enumTagFieldIndex(operand_val, zcu)) |_| {
13065 return spa.resolveProngComptime(
13066 child_block,
13067 .special,
13068 special.body,
13069 special.capture,
13070 child_block.src(.{ .switch_capture = .{
13071 .switch_node_offset = switch_node_offset,
13072 .case_idx = .special_else,
13073 } }),
13074 undefined, // case_vals may be undefined for special prongs
13075 if (special.is_inline) cond_operand else .none,
13076 special.has_tag_capture,
13077 merges,
13078 );
13079 }
13080 }
1283013081
1283113082 return spa.resolveProngComptime(
1283213083 child_block,
1283313084 .special,
12834 special.body,
12835 special.capture,
13085 special_generic.body,
13086 special_generic.capture,
1283613087 child_block.src(.{ .switch_capture = .{
1283713088 .switch_node_offset = switch_node_offset,
12838 .case_idx = LazySrcLoc.Offset.SwitchCaseIndex.special,
13089 .case_idx = if (special_generic_is_under)
13090 .special_under
13091 else
13092 .special_else,
1283913093 } }),
1284013094 undefined, // case_vals may be undefined for special prongs
12841 if (special.is_inline) cond_operand else .none,
12842 special.has_tag_capture,
13095 if (special_generic.is_inline) cond_operand else .none,
13096 special_generic.has_tag_capture,
1284313097 merges,
1284413098 );
1284513099}
src/Zcu.zig+66-37
......@@ -1679,20 +1679,37 @@ pub const SrcLoc = struct {
16791679 return tree.nodeToSpan(condition);
16801680 },
16811681
1682 .node_offset_switch_special_prong => |node_off| {
1682 .node_offset_switch_else_prong => |node_off| {
16831683 const tree = try src_loc.file_scope.getTree(zcu);
16841684 const switch_node = node_off.toAbsolute(src_loc.base_node);
16851685 _, const extra_index = tree.nodeData(switch_node).node_and_extra;
16861686 const case_nodes = tree.extraDataSlice(tree.extraData(extra_index, Ast.Node.SubRange), Ast.Node.Index);
16871687 for (case_nodes) |case_node| {
16881688 const case = tree.fullSwitchCase(case_node).?;
1689 const is_special = (case.ast.values.len == 0) or
1690 (case.ast.values.len == 1 and
1691 tree.nodeTag(case.ast.values[0]) == .identifier and
1692 mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(case.ast.values[0])), "_"));
1693 if (!is_special) continue;
1689 if (case.ast.values.len == 0) {
1690 return tree.nodeToSpan(case_node);
1691 }
1692 } else unreachable;
1693 },
16941694
1695 return tree.nodeToSpan(case_node);
1695 .node_offset_switch_under_prong => |node_off| {
1696 const tree = try src_loc.file_scope.getTree(zcu);
1697 const switch_node = node_off.toAbsolute(src_loc.base_node);
1698 _, const extra_index = tree.nodeData(switch_node).node_and_extra;
1699 const case_nodes = tree.extraDataSlice(tree.extraData(extra_index, Ast.Node.SubRange), Ast.Node.Index);
1700 for (case_nodes) |case_node| {
1701 const case = tree.fullSwitchCase(case_node).?;
1702 for (case.ast.values) |val| {
1703 if (tree.nodeTag(val) == .identifier and
1704 mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(val)), "_"))
1705 {
1706 return tree.tokensToSpan(
1707 tree.firstToken(case_node),
1708 tree.lastToken(case_node),
1709 tree.nodeMainToken(val),
1710 );
1711 }
1712 }
16961713 } else unreachable;
16971714 },
16981715
......@@ -1703,12 +1720,6 @@ pub const SrcLoc = struct {
17031720 const case_nodes = tree.extraDataSlice(tree.extraData(extra_index, Ast.Node.SubRange), Ast.Node.Index);
17041721 for (case_nodes) |case_node| {
17051722 const case = tree.fullSwitchCase(case_node).?;
1706 const is_special = (case.ast.values.len == 0) or
1707 (case.ast.values.len == 1 and
1708 tree.nodeTag(case.ast.values[0]) == .identifier and
1709 mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(case.ast.values[0])), "_"));
1710 if (is_special) continue;
1711
17121723 for (case.ast.values) |item_node| {
17131724 if (tree.nodeTag(item_node) == .switch_range) {
17141725 return tree.nodeToSpan(item_node);
......@@ -2113,28 +2124,35 @@ pub const SrcLoc = struct {
21132124
21142125 var multi_i: u32 = 0;
21152126 var scalar_i: u32 = 0;
2116 const case = for (case_nodes) |case_node| {
2127 var underscore_node: Ast.Node.OptionalIndex = .none;
2128 const case = case: for (case_nodes) |case_node| {
21172129 const case = tree.fullSwitchCase(case_node).?;
2118 const is_special = special: {
2119 if (case.ast.values.len == 0) break :special true;
2120 if (case.ast.values.len == 1 and tree.nodeTag(case.ast.values[0]) == .identifier) {
2121 break :special mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(case.ast.values[0])), "_");
2130 if (case.ast.values.len == 0) {
2131 if (want_case_idx == LazySrcLoc.Offset.SwitchCaseIndex.special_else) {
2132 break :case case;
21222133 }
2123 break :special false;
2124 };
2125 if (is_special) {
2126 if (want_case_idx.isSpecial()) {
2127 break case;
2128 }
2129 continue;
2134 continue :case;
21302135 }
2136 if (underscore_node == .none) for (case.ast.values) |val_node| {
2137 if (tree.nodeTag(val_node) == .identifier and
2138 mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(val_node)), "_"))
2139 {
2140 underscore_node = val_node.toOptional();
2141 if (want_case_idx == LazySrcLoc.Offset.SwitchCaseIndex.special_under) {
2142 break :case case;
2143 }
2144 continue :case;
2145 }
2146 };
21312147
21322148 const is_multi = case.ast.values.len != 1 or
21332149 tree.nodeTag(case.ast.values[0]) == .switch_range;
21342150
21352151 switch (want_case_idx.kind) {
2136 .scalar => if (!is_multi and want_case_idx.index == scalar_i) break case,
2137 .multi => if (is_multi and want_case_idx.index == multi_i) break case,
2152 .scalar => if (!is_multi and want_case_idx.index == scalar_i)
2153 break :case case,
2154 .multi => if (is_multi and want_case_idx.index == multi_i)
2155 break :case case,
21382156 }
21392157
21402158 if (is_multi) {
......@@ -2148,7 +2166,10 @@ pub const SrcLoc = struct {
21482166 .switch_case_item,
21492167 .switch_case_item_range_first,
21502168 .switch_case_item_range_last,
2151 => |x| x.item_idx,
2169 => |x| item_idx: {
2170 assert(want_case_idx != LazySrcLoc.Offset.SwitchCaseIndex.special_else);
2171 break :item_idx x.item_idx;
2172 },
21522173 .switch_capture, .switch_tag_capture => {
21532174 const start = switch (src_loc.lazy) {
21542175 .switch_capture => case.payload_token.?,
......@@ -2173,7 +2194,11 @@ pub const SrcLoc = struct {
21732194 .single => {
21742195 var item_i: u32 = 0;
21752196 for (case.ast.values) |item_node| {
2176 if (tree.nodeTag(item_node) == .switch_range) continue;
2197 if (item_node.toOptional() == underscore_node or
2198 tree.nodeTag(item_node) == .switch_range)
2199 {
2200 continue;
2201 }
21772202 if (item_i != want_item.index) {
21782203 item_i += 1;
21792204 continue;
......@@ -2184,7 +2209,9 @@ pub const SrcLoc = struct {
21842209 .range => {
21852210 var range_i: u32 = 0;
21862211 for (case.ast.values) |item_node| {
2187 if (tree.nodeTag(item_node) != .switch_range) continue;
2212 if (tree.nodeTag(item_node) != .switch_range) {
2213 continue;
2214 }
21882215 if (range_i != want_item.index) {
21892216 range_i += 1;
21902217 continue;
......@@ -2363,10 +2390,14 @@ pub const LazySrcLoc = struct {
23632390 /// by taking this AST node index offset from the containing base node,
23642391 /// which points to a switch expression AST node. Next, navigate to the operand.
23652392 node_offset_switch_operand: Ast.Node.Offset,
2366 /// The source location points to the else/`_` prong of a switch expression, found
2393 /// The source location points to the else prong of a switch expression, found
2394 /// by taking this AST node index offset from the containing base node,
2395 /// which points to a switch expression AST node. Next, navigate to the else prong.
2396 node_offset_switch_else_prong: Ast.Node.Offset,
2397 /// The source location points to the `_` prong of a switch expression, found
23672398 /// by taking this AST node index offset from the containing base node,
2368 /// which points to a switch expression AST node. Next, navigate to the else/`_` prong.
2369 node_offset_switch_special_prong: Ast.Node.Offset,
2399 /// which points to a switch expression AST node. Next, navigate to the `_` prong.
2400 node_offset_switch_under_prong: Ast.Node.Offset,
23702401 /// The source location points to all the ranges of a switch expression, found
23712402 /// by taking this AST node index offset from the containing base node,
23722403 /// which points to a switch expression AST node. Next, navigate to any of the
......@@ -2562,10 +2593,8 @@ pub const LazySrcLoc = struct {
25622593 kind: enum(u1) { scalar, multi },
25632594 index: u31,
25642595
2565 pub const special: SwitchCaseIndex = @bitCast(@as(u32, std.math.maxInt(u32)));
2566 pub fn isSpecial(idx: SwitchCaseIndex) bool {
2567 return @as(u32, @bitCast(idx)) == @as(u32, @bitCast(special));
2568 }
2596 pub const special_else: SwitchCaseIndex = @bitCast(@as(u32, std.math.maxInt(u32)));
2597 pub const special_under: SwitchCaseIndex = @bitCast(@as(u32, std.math.maxInt(u32) - 1));
25692598 };
25702599
25712600 pub const SwitchItemIndex = packed struct(u32) {
src/print_zir.zig+65-14
......@@ -2087,15 +2087,10 @@ const Writer = struct {
20872087
20882088 self.indent += 2;
20892089
2090 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 };
2090 const special_prongs = extra.data.bits.special_prongs;
20972091
2098 const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(self.code.extra[extra_index]));
2092 if (special_prongs.hasElse()) {
2093 const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(self.code.extra[extra_index]);
20992094 const capture_text = switch (info.capture) {
21002095 .none => "",
21012096 .by_val => "by_val ",
......@@ -2108,7 +2103,63 @@ const Writer = struct {
21082103
21092104 try stream.writeAll(",\n");
21102105 try stream.splatByteAll(' ', self.indent);
2111 try stream.print("{s}{s}{s} => ", .{ capture_text, inline_text, prong_name });
2106 try stream.print("{s}{s}else => ", .{ capture_text, inline_text });
2107 try self.writeBracedBody(stream, body);
2108 }
2109
2110 if (special_prongs.hasUnder()) {
2111 var single_item_ref: Zir.Inst.Ref = .none;
2112 var items_len: u32 = 0;
2113 var ranges_len: u32 = 0;
2114 if (special_prongs.hasOneAdditionalItem()) {
2115 single_item_ref = @enumFromInt(self.code.extra[extra_index]);
2116 extra_index += 1;
2117 } else if (special_prongs.hasManyAdditionalItems()) {
2118 items_len = self.code.extra[extra_index];
2119 extra_index += 1;
2120 ranges_len = self.code.extra[extra_index];
2121 extra_index += 1;
2122 }
2123 const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(self.code.extra[extra_index]);
2124 extra_index += 1;
2125 const items = self.code.refSlice(extra_index, items_len);
2126 extra_index += items_len;
2127
2128 try stream.writeAll(",\n");
2129 try stream.splatByteAll(' ', self.indent);
2130 switch (info.capture) {
2131 .none => {},
2132 .by_val => try stream.writeAll("by_val "),
2133 .by_ref => try stream.writeAll("by_ref "),
2134 }
2135 if (info.is_inline) try stream.writeAll("inline ");
2136
2137 try stream.writeAll("_");
2138 if (single_item_ref != .none) {
2139 try stream.writeAll(", ");
2140 try self.writeInstRef(stream, single_item_ref);
2141 }
2142 for (items) |item_ref| {
2143 try stream.writeAll(", ");
2144 try self.writeInstRef(stream, item_ref);
2145 }
2146
2147 var range_i: usize = 0;
2148 while (range_i < ranges_len) : (range_i += 1) {
2149 const item_first: Zir.Inst.Ref = @enumFromInt(self.code.extra[extra_index]);
2150 extra_index += 1;
2151 const item_last: Zir.Inst.Ref = @enumFromInt(self.code.extra[extra_index]);
2152 extra_index += 1;
2153
2154 try stream.writeAll(", ");
2155 try self.writeInstRef(stream, item_first);
2156 try stream.writeAll("...");
2157 try self.writeInstRef(stream, item_last);
2158 }
2159
2160 const body = self.code.bodySlice(extra_index, info.body_len);
2161 extra_index += info.body_len;
2162 try stream.writeAll(" => ");
21122163 try self.writeBracedBody(stream, body);
21132164 }
21142165
......@@ -2116,9 +2167,9 @@ const Writer = struct {
21162167 const scalar_cases_len = extra.data.bits.scalar_cases_len;
21172168 var scalar_i: usize = 0;
21182169 while (scalar_i < scalar_cases_len) : (scalar_i += 1) {
2119 const item_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
2170 const item_ref: Zir.Inst.Ref = @enumFromInt(self.code.extra[extra_index]);
21202171 extra_index += 1;
2121 const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(self.code.extra[extra_index]));
2172 const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(self.code.extra[extra_index]);
21222173 extra_index += 1;
21232174 const body = self.code.bodySlice(extra_index, info.body_len);
21242175 extra_index += info.body_len;
......@@ -2143,7 +2194,7 @@ const Writer = struct {
21432194 extra_index += 1;
21442195 const ranges_len = self.code.extra[extra_index];
21452196 extra_index += 1;
2146 const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(self.code.extra[extra_index]));
2197 const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(self.code.extra[extra_index]);
21472198 extra_index += 1;
21482199 const items = self.code.refSlice(extra_index, items_len);
21492200 extra_index += items_len;
......@@ -2164,9 +2215,9 @@ const Writer = struct {
21642215
21652216 var range_i: usize = 0;
21662217 while (range_i < ranges_len) : (range_i += 1) {
2167 const item_first = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
2218 const item_first: Zir.Inst.Ref = @enumFromInt(self.code.extra[extra_index]);
21682219 extra_index += 1;
2169 const item_last = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
2220 const item_last: Zir.Inst.Ref = @enumFromInt(self.code.extra[extra_index]);
21702221 extra_index += 1;
21712222
21722223 if (range_i != 0 or items.len != 0) {
test/behavior/switch.zig+47
......@@ -1073,3 +1073,50 @@ test "switch on 8-bit mod result" {
10731073 else => unreachable,
10741074 }
10751075}
1076
1077test "switch on non-exhaustive enum" {
1078 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; // TODO
1079
1080 const E = enum(u4) {
1081 a,
1082 b,
1083 c,
1084 _,
1085
1086 fn doTheTest(e: @This()) !void {
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 switch (e) {
1101 .a => {},
1102 .b, .c, _ => return error.TestFailed,
1103 }
1104 switch (e) {
1105 .b => return error.TestFailed,
1106 else => {},
1107 _ => return error.TestFailed,
1108 }
1109 switch (e) {
1110 else => {},
1111 _ => return error.TestFailed,
1112 }
1113 switch (e) {
1114 inline else => {},
1115 _ => return error.TestFailed,
1116 }
1117 }
1118 };
1119
1120 try E.doTheTest(.a);
1121 try comptime E.doTheTest(.a);
1122}
test/behavior/switch_loop.zig+24
......@@ -249,3 +249,27 @@ test "switch loop on larger than pointer integer" {
249249 }
250250 try expect(entry == 3);
251251}
252
253test "switch loop on non-exhaustive enum" {
254 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
255 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
256 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
257 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; // TODO
258
259 const S = struct {
260 const E = enum(u8) { a, b, c, _ };
261
262 fn doTheTest() !void {
263 var start: E = undefined;
264 start = .a;
265 const result: u32 = s: switch (start) {
266 .a => continue :s .c,
267 else => continue :s @enumFromInt(123),
268 .b, _ => |x| break :s @intFromEnum(x),
269 };
270 try expect(result == 123);
271 }
272 };
273 try S.doTheTest();
274 try comptime S.doTheTest();
275}
test/cases/compile_errors/switch_expression-non_exhaustive_absorbing.zig created+31
......@@ -0,0 +1,31 @@
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//
26// :12:5: error: switch must handle all possibilities
27// :3:5: note: unhandled enumeration value: 'b'
28// :1:11: note: enum 'tmp.E' declared here
29// :18:5: error: '_' prong only allowed when switching on non-exhaustive enums
30// :20:13: note: '_' prong here
31// :18:5: note: consider using 'else'
test/cases/compile_errors/switch_expression-non_exhaustive_inline.zig created+25
......@@ -0,0 +1,25 @@
1const E = enum(u8) {
2 a,
3 b,
4 _,
5};
6
7export fn f(e: E) void {
8 switch (e) {
9 .a => {},
10 inline _ => {},
11 }
12}
13
14export fn g(e: E) void {
15 switch (e) {
16 .a => {},
17 else => {},
18 inline _ => {},
19 }
20}
21
22// error
23//
24// :10:16: error: cannot inline '_' prong
25// :18:16: error: cannot inline '_' prong
test/cases/compile_errors/switch_expression-non_exhaustive_unreachable_else.zig created+16
......@@ -0,0 +1,16 @@
1const E = enum(u8) {
2 a,
3 b,
4 _,
5};
6
7export fn f(e: E) void {
8 switch (e) {
9 .a, .b, _ => {},
10 else => {},
11 }
12}
13
14// error
15//
16// :10:14: error: unreachable else prong; all explicit cases already handled
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+2-2
......@@ -37,7 +37,7 @@ pub export fn entry3() void {
3737// :12:5: error: switch must handle all possibilities
3838// :3:5: note: unhandled enumeration value: 'b'
3939// :1:11: note: enum 'tmp.E' declared here
40// :19:5: error: switch on non-exhaustive enum must include 'else' or '_' prong
40// :19:5: error: switch on non-exhaustive enum must include 'else' or '_' prong or both
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'