authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-01-11 13:16:25+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-01-11 14:37:27+00:00
loged1268d0e63d3af144a262a1fa41b8e69ab3073b
tree3f5e99ac159431271bc5c9029b6d8b9f8ee961b2
parent8ec4c5cb137d9a93c35df3488408c44056c586b4
signaturelock-open Commit is signed but in an unrecognized format.

Zir: simplify '_' prong of 'switch' statements


5 files changed, 188 insertions(+), 390 deletions(-)

lib/std/zig/AstGen.zig+19-69
......@@ -7267,11 +7267,7 @@ fn switchExpr(
72677267 var total_items_len: usize = 0;
72687268 var total_ranges_len: usize = 0;
72697269 var else_case_node: Ast.Node.OptionalIndex = .none;
7270 var else_src: ?Ast.TokenIndex = null;
7271 var under_case_node: Ast.Node.OptionalIndex = .none;
72727270 var underscore_node: Ast.Node.OptionalIndex = .none;
7273 var underscore_src: ?Ast.TokenIndex = null;
7274 var under_is_bare = false;
72757271 for (case_nodes) |case_node| {
72767272 const case = tree.fullSwitchCase(case_node).?;
72777273 if (case.payload_token) |payload_token| {
......@@ -7304,22 +7300,21 @@ fn switchExpr(
73047300
73057301 // Check for else prong.
73067302 if (case.ast.values.len == 0) {
7307 const case_src = case.ast.arrow_token - 1;
7308 if (else_src) |src| {
7303 if (else_case_node.unwrap()) |prev_case_node| {
7304 const prev_else_tok = tree.fullSwitchCase(prev_case_node).?.ast.arrow_token - 1;
7305 const else_tok = case.ast.arrow_token - 1;
73097306 return astgen.failTokNotes(
7310 case_src,
7307 else_tok,
73117308 "multiple else prongs in switch expression",
73127309 .{},
7313 &.{try astgen.errNoteTok(src, "previous else prong here", .{})},
7310 &.{try astgen.errNoteTok(prev_else_tok, "previous else prong here", .{})},
73147311 );
73157312 }
73167313 else_case_node = case_node.toOptional();
7317 else_src = case_src;
73187314 continue;
73197315 }
73207316
73217317 // Check for '_' prong and ranges.
7322 var case_has_underscore = false;
73237318 var case_has_ranges = false;
73247319 for (case.ast.values) |val| {
73257320 switch (tree.nodeTag(val)) {
......@@ -7329,10 +7324,10 @@ fn switchExpr(
73297324 },
73307325 .string_literal => return astgen.failNode(val, "cannot switch on strings", .{}),
73317326 else => |tag| {
7327 total_items_len += 1;
73327328 if (tag == .identifier and
73337329 mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(val)), "_"))
73347330 {
7335 const val_src = tree.nodeMainToken(val);
73367331 if (is_err_switch) {
73377332 const case_src = case.ast.arrow_token - 1;
73387333 return astgen.failTokNotes(
......@@ -7348,30 +7343,24 @@ fn switchExpr(
73487343 },
73497344 );
73507345 }
7351 if (underscore_src) |src| {
7352 return astgen.failTokNotes(
7353 val_src,
7346 if (underscore_node.unwrap()) |prev_src| {
7347 return astgen.failNodeNotes(
7348 val,
73547349 "multiple '_' prongs in switch expression",
73557350 .{},
7356 &.{try astgen.errNoteTok(src, "previous '_' prong here", .{})},
7351 &.{try astgen.errNoteNode(prev_src, "previous '_' prong here", .{})},
73577352 );
73587353 }
73597354 if (case.inline_token != null) {
7360 return astgen.failTok(val_src, "cannot inline '_' prong", .{});
7355 return astgen.failNode(val, "cannot inline '_' prong", .{});
73617356 }
7362 under_case_node = case_node.toOptional();
7363 underscore_src = val_src;
73647357 underscore_node = val.toOptional();
7365 under_is_bare = case.ast.values.len == 1;
7366 case_has_underscore = true;
7367 } else {
7368 total_items_len += 1;
73697358 }
73707359 },
73717360 }
73727361 }
73737362
7374 const case_len = case.ast.values.len - @intFromBool(case_has_underscore);
7363 const case_len = case.ast.values.len;
73757364 if (case_len == 1 and !case_has_ranges) {
73767365 scalar_cases_len += 1;
73777366 } else if (case_len >= 1) {
......@@ -7379,9 +7368,8 @@ fn switchExpr(
73797368 }
73807369 }
73817370
7382 const has_else = else_src != null;
7383 const has_under = underscore_src != null;
7384 if (under_is_bare) assert(has_under); // make sure that the former implies the latter
7371 const has_else = else_case_node != .none;
7372 const has_under = underscore_node != .none;
73857373 if (is_err_switch) assert(!has_under); // should have failed by now
73867374 const any_ranges = total_ranges_len > 0;
73877375
......@@ -7430,10 +7418,8 @@ fn switchExpr(
74307418
74317419 var non_err_prong_body_start: u32 = undefined;
74327420 var else_prong_body_start: u32 = undefined;
7433 var bare_under_prong_body_start: u32 = undefined;
74347421 var non_err_info: Zir.Inst.SwitchBlock.ProngInfo.NonErr = undefined;
74357422 var else_info: Zir.Inst.SwitchBlock.ProngInfo.Else = undefined;
7436 var under_extra: u32 = undefined;
74377423
74387424 var block_scope = parent_gz.makeSubBlock(scope);
74397425 // block_scope not used for collecting instructions
......@@ -7688,7 +7674,6 @@ fn switchExpr(
76887674 for (case_nodes) |case_node| {
76897675 const case = tree.fullSwitchCase(case_node).?;
76907676
7691 const case_has_under = case_node.toOptional() == under_case_node;
76927677 const ranges_len: u32 = if (any_ranges) blk: {
76937678 var ranges_len: u32 = 0;
76947679 for (case.ast.values) |value| {
......@@ -7696,14 +7681,13 @@ fn switchExpr(
76967681 }
76977682 break :blk ranges_len;
76987683 } else 0;
7699 const items_len: u32 = @intCast(case.ast.values.len - ranges_len - @intFromBool(case_has_under));
7684 const items_len: u32 = @intCast(case.ast.values.len - ranges_len);
77007685 const is_multi_case = items_len > 1 or ranges_len > 0;
77017686
77027687 // item/range bodies in order of occurence
77037688 var item_i: usize = 0;
77047689 var range_i: usize = 0;
77057690 for (case.ast.values) |value| {
7706 if (value.toOptional() == underscore_node) continue;
77077691 const is_range = tree.nodeTag(value) == .switch_range;
77087692 const range: [2]Ast.Node.Index = if (is_range) tree.nodeData(value).node_and_node else undefined;
77097693 const nodes: []const Ast.Node.Index = if (is_range) &range else &.{value};
......@@ -7722,16 +7706,9 @@ fn switchExpr(
77227706 const str_index = try astgen.identAsString(ident_token);
77237707 break :blk .wrap(.{ .error_value = str_index });
77247708 },
7725 .number_literal => {
7726 // We don't actually need a final result type for number
7727 // literals, they can just be turned into `comptime_int`
7728 // or `comptime_float` as usual and then be coerced to
7729 // the correct type later during semantic analysis.
7730 assert(scratch_scope.instructions_top == GenZir.unstacked_top); // important! we emit into `parent_gz` which `scratch_scope` is stacked on top of
7731 const zir_ref = try comptimeExpr(parent_gz, scope, .{ .rl = .none }, item, .switch_item);
7732 break :blk .wrap(.{ .number_literal = zir_ref });
7733 },
7734 else => {
7709 else => if (value.toOptional() == underscore_node) {
7710 break :blk .wrap(.under);
7711 } else {
77357712 scratch_scope.instructions_top = parent_gz.instructions.items.len;
77367713 defer scratch_scope.unstack();
77377714 const item_result = try fullBodyExpr(&scratch_scope, scope, item_ri, item, .normal);
......@@ -7957,25 +7934,6 @@ fn switchExpr(
79577934 break :prong_body;
79587935 }
79597936
7960 if (case_has_under) {
7961 // We're either writing under_prong_info or under_index here.
7962 if (under_is_bare) {
7963 assert(case.ast.values.len == 1); // only `_`
7964 const bare_under_info: Zir.Inst.SwitchBlock.ProngInfo.BareUnder = .{
7965 .body_len = @intCast(body_len),
7966 .capture = capture,
7967 .has_tag_capture = has_tag_capture,
7968 };
7969 under_extra = @bitCast(bare_under_info);
7970 bare_under_prong_body_start = body_start;
7971 break :prong_body;
7972 } else if (is_multi_case) {
7973 under_extra = scalar_cases_len + multi_case_index;
7974 } else {
7975 under_extra = scalar_case_index;
7976 }
7977 }
7978
79797937 // We allow prongs with error items which are not inside the error set
79807938 // being switched on if their body is `=> comptime unreachable,`.
79817939 const is_comptime_unreach = comptime_unreach: {
......@@ -8003,7 +7961,7 @@ fn switchExpr(
80037961 }
80047962 }
80057963 }
8006 assert(scalar_case_index + multi_case_index + @intFromBool(has_else) + @intFromBool(under_is_bare) == case_nodes.len);
7964 assert(scalar_case_index + multi_case_index + @intFromBool(has_else) == case_nodes.len);
80077965 assert(multi_items_infos_start + multi_item_offset == bodies_start);
80087966
80097967 if (switch_full.label_token) |label_token| if (!block_scope.label.?.used) {
......@@ -8024,7 +7982,6 @@ fn switchExpr(
80247982 @intFromBool(needs_non_err_handling) + // catch_or_if_src_node_offset
80257983 @intFromBool(needs_non_err_handling) + // non_err_info
80267984 @intFromBool(has_else) + // else_info
8027 @intFromBool(has_under) + // under_prong_info or under_index
80287985 payloads.items.len - body_table_end); // item infos and bodies
80297986
80307987 // singular pieces of data
......@@ -8035,7 +7992,6 @@ fn switchExpr(
80357992 .any_ranges = any_ranges,
80367993 .has_else = has_else,
80377994 .has_under = has_under,
8038 .under_is_bare = under_is_bare,
80397995 .has_continue = switch_full.label_token != null and block_scope.label.?.used_for_continue,
80407996 .any_maybe_runtime_capture = any_maybe_runtime_capture,
80417997 .payload_capture_inst_is_placeholder = payload_capture_inst_is_placeholder,
......@@ -8054,7 +8010,6 @@ fn switchExpr(
80548010 astgen.extra.appendAssumeCapacity(@bitCast(non_err_info));
80558011 }
80568012 if (has_else) astgen.extra.appendAssumeCapacity(@bitCast(else_info));
8057 if (has_under) astgen.extra.appendAssumeCapacity(under_extra);
80588013
80598014 const extra_payloads_start = astgen.extra.items.len;
80608015
......@@ -8070,11 +8025,6 @@ fn switchExpr(
80708025 const body = payloads.items[else_prong_body_start..][0..else_info.body_len];
80718026 astgen.extra.appendSliceAssumeCapacity(body);
80728027 }
8073 if (under_is_bare) {
8074 const under_prong_info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(under_extra);
8075 const body = payloads.items[bare_under_prong_body_start..][0..under_prong_info.body_len];
8076 astgen.extra.appendSliceAssumeCapacity(body);
8077 }
80788028 for (0..scalar_cases_len) |scalar_i| {
80798029 const item_info: Zir.Inst.SwitchBlock.ItemInfo = @bitCast(payloads.items[scalar_item_infos_start + scalar_i]);
80808030 const item_body_start = payloads.items[scalar_body_table + scalar_i];
lib/std/zig/Zir.zig+19-104
......@@ -3303,34 +3303,25 @@ pub const Inst = struct {
33033303 /// 3. catch_or_if_src_node_offset: Ast.Node.Offset, // If inst is switch_block_err_union.
33043304 /// 4. non_err_info: ProngInfo.NonErr, // If inst is switch_block_err_union.
33053305 /// 5. else_info: ProngInfo.Else, // If has_else is set.
3306 /// 6. under_info: ProngInfo.Under, // If has_under is set and
3307 /// // under_is_bare is set.
3308 /// 7. under_index: u32, // If has_under is set and
3309 /// // under_is_bare is *not* set.
3310 /// // Index into switch cases.
3311 /// 8. scalar_prong_info: ProngInfo, // for every scalar_cases_len
3312 /// 9. multi_prong_info: ProngInfo, // for every multi_cases_len
3313 /// 10. multi_case_items_len: u32, // for every multi_cases_len
3314 /// 11. multi_case_ranges_len: u32, // If has_ranges is set: for every multi_cases_len
3315 /// 12. scalar_item_info: ItemInfo, // for every scalar_cases_len
3316 /// 13. multi_items_info: { // for every multi_cases_len
3306 /// 6. scalar_prong_info: ProngInfo, // for every scalar_cases_len
3307 /// 7. multi_prong_info: ProngInfo, // for every multi_cases_len
3308 /// 8. multi_case_items_len: u32, // for every multi_cases_len
3309 /// 9. multi_case_ranges_len: u32, // If has_ranges is set: for every multi_cases_len
3310 /// 10. scalar_item_info: ItemInfo, // for every scalar_cases_len
3311 /// 11. multi_items_info: { // for every multi_cases_len
33173312 /// item_info: ItemInfo, // for each multi_case_items_len
33183313 /// range_items_info: { // for each multi_case_ranges_len
33193314 /// first_info: ItemInfo,
33203315 /// last_info: ItemInfo,
33213316 /// }
33223317 /// }
3323 /// 14. non_err_body {
3318 /// 12. non_err_body {
33243319 /// body_inst: Index // for every non_err_info.body_len
33253320 /// }
3326 /// 15. else_body: { // If has_else is set.
3321 /// 13. else_body: { // If has_else is set.
33273322 /// body_inst: Inst.Index, // for every else_info.body_len
33283323 /// }
3329 /// 16. under_body: { // If has_under is set and
3330 /// // under_is_bare is set.
3331 /// body_inst: Inst.Index, // for every under_info.body_len
3332 /// }
3333 /// 17. scalar_bodies: { // for every scalar_cases_len
3324 /// 14. scalar_bodies: { // for every scalar_cases_len
33343325 /// prong_body: { // for each body_len in scalar_prong_info
33353326 /// body_inst: Inst.Index, // for every body_len
33363327 /// }
......@@ -3338,7 +3329,7 @@ pub const Inst = struct {
33383329 /// body_inst: Inst.Index, // for every body_len
33393330 /// }
33403331 /// }
3341 /// 18. multi_bodies: { // for each multi_items_info
3332 /// 15. multi_bodies: { // for each multi_items_info
33423333 /// prong_body: {
33433334 /// body_inst: Inst.Index, // for each multi_prong_info.body_len
33443335 /// }
......@@ -3363,8 +3354,6 @@ pub const Inst = struct {
33633354 any_ranges: bool,
33643355 has_else: bool,
33653356 has_under: bool,
3366 /// Only valid if `has_under` is also set.
3367 under_is_bare: bool,
33683357 /// If true, at least one prong contains a `continue`.
33693358 /// Only valid if `has_label` is set.
33703359 has_continue: bool,
......@@ -3377,7 +3366,7 @@ pub const Inst = struct {
33773366 // NOTE maybe don't steal any more bits from poor `scalar_cases_len`
33783367 // and split `Bits` into two parts instead, `raw_operand` surely
33793368 // wouldn't mind donating a couple of bits for that purpose...
3380 pub const ScalarCasesLen = u23;
3369 pub const ScalarCasesLen = u24;
33813370 };
33823371
33833372 pub const ProngInfo = packed struct(u32) {
......@@ -3406,12 +3395,6 @@ pub const Inst = struct {
34063395 has_tag_capture: bool,
34073396 is_simple_noreturn: bool,
34083397 };
3409
3410 pub const BareUnder = packed struct(u32) {
3411 body_len: u29,
3412 capture: ProngInfo.Capture,
3413 has_tag_capture: bool,
3414 };
34153398 };
34163399
34173400 pub const ItemInfo = packed struct(u32) {
......@@ -3421,23 +3404,23 @@ pub const Inst = struct {
34213404 pub const Kind = enum(u2) {
34223405 enum_literal,
34233406 error_value,
3424 number_literal,
34253407 body_len,
3408 under,
34263409 };
34273410
34283411 pub const Unwrapped = union(ItemInfo.Kind) {
34293412 enum_literal: Zir.NullTerminatedString,
34303413 error_value: Zir.NullTerminatedString,
3431 number_literal: Inst.Ref,
34323414 body_len: u32,
3415 under,
34333416 };
34343417
34353418 pub fn wrap(unwrapped: ItemInfo.Unwrapped) ItemInfo {
34363419 const data_uncasted: u32 = switch (unwrapped) {
34373420 .enum_literal => |str_index| @intFromEnum(str_index),
34383421 .error_value => |str_index| @intFromEnum(str_index),
3439 .number_literal => |zir_ref| @intFromEnum(zir_ref),
34403422 .body_len => |body_len| body_len,
3423 .under => 0,
34413424 };
34423425 return .{ .kind = unwrapped, .data = @intCast(data_uncasted) };
34433426 }
......@@ -3446,8 +3429,8 @@ pub const Inst = struct {
34463429 return switch (item_info.kind) {
34473430 .enum_literal => .{ .enum_literal = @enumFromInt(item_info.data) },
34483431 .error_value => .{ .error_value = @enumFromInt(item_info.data) },
3449 .number_literal => .{ .number_literal = @enumFromInt(item_info.data) },
34503432 .body_len => .{ .body_len = item_info.data },
3433 .under => .under,
34513434 };
34523435 }
34533436
......@@ -4818,9 +4801,6 @@ fn findTrackableInner(
48184801 if (zir_switch.else_case) |else_case| {
48194802 try zir.findTrackableBody(gpa, contents, defers, else_case.body);
48204803 }
4821 if (zir_switch.under_case.resolve()) |under_case| {
4822 try zir.findTrackableBody(gpa, contents, defers, under_case.body);
4823 }
48244804 var extra_index = zir_switch.end;
48254805 var case_it = zir_switch.iterateCases();
48264806 while (case_it.next()) |case| {
......@@ -5268,16 +5248,6 @@ pub fn getSwitchBlock(zir: *const Zir, switch_inst: Inst.Index) UnwrappedSwitchB
52685248 extra_index += 1;
52695249 break :else_info else_info;
52705250 } else undefined;
5271 const bare_under_info: Inst.SwitchBlock.ProngInfo.BareUnder = if (bits.has_under and bits.under_is_bare) bare_under_info: {
5272 const bare_under_info: Inst.SwitchBlock.ProngInfo.BareUnder = @bitCast(zir.extra[extra_index]);
5273 extra_index += 1;
5274 break :bare_under_info bare_under_info;
5275 } else undefined;
5276 const under_index: u32 = if (bits.has_under and !bits.under_is_bare) under_index: {
5277 const under_index = zir.extra[extra_index];
5278 extra_index += 1;
5279 break :under_index under_index;
5280 } else undefined;
52815251 const scalar_cases_len: u32 = bits.scalar_cases_len;
52825252 const prong_infos: []const Inst.SwitchBlock.ProngInfo =
52835253 @ptrCast(zir.extra[extra_index..][0 .. scalar_cases_len + multi_cases_len]);
......@@ -5320,20 +5290,6 @@ pub fn getSwitchBlock(zir: *const Zir, switch_inst: Inst.Index) UnwrappedSwitchB
53205290 .is_simple_noreturn = else_info.is_simple_noreturn,
53215291 };
53225292 } else null;
5323 const under_case: UnwrappedSwitchBlock.Case.Under = if (bits.has_under) under_case: {
5324 if (bits.under_is_bare) {
5325 const body = zir.bodySlice(extra_index, bare_under_info.body_len);
5326 extra_index += body.len;
5327 break :under_case .{ .bare = .{
5328 .index = .bare_under,
5329 .body = body,
5330 .capture = bare_under_info.capture,
5331 .has_tag_capture = bare_under_info.has_tag_capture,
5332 } };
5333 } else {
5334 break :under_case .{ .index = under_index };
5335 }
5336 } else .none;
53375293 return .{
53385294 .main_operand = extra.data.raw_operand,
53395295 .switch_src_node_offset = inst_data.src_node,
......@@ -5344,7 +5300,7 @@ pub fn getSwitchBlock(zir: *const Zir, switch_inst: Inst.Index) UnwrappedSwitchB
53445300 .any_maybe_runtime_capture = bits.any_maybe_runtime_capture,
53455301 .non_err_case = non_err_case,
53465302 .else_case = else_case,
5347 .under_case = under_case,
5303 .has_under = bits.has_under,
53485304 .prong_infos = prong_infos,
53495305 .multi_case_items_lens = multi_case_items_lens,
53505306 .multi_case_ranges_lens = multi_case_ranges_lens,
......@@ -5377,7 +5333,7 @@ pub const UnwrappedSwitchBlock = struct {
53775333 any_maybe_runtime_capture: bool,
53785334 non_err_case: ?Case.NonErr,
53795335 else_case: ?Case.Else,
5380 under_case: Case.Under,
5336 has_under: bool,
53815337 // Refer to doc comment and `iterateCases` to access everything below correctly.
53825338 prong_infos: []const Inst.SwitchBlock.ProngInfo,
53835339 multi_case_items_lens: []const u32,
......@@ -5411,25 +5367,13 @@ pub const UnwrappedSwitchBlock = struct {
54115367 item_infos: []const Inst.SwitchBlock.ItemInfo,
54125368 range_infos: []const [2]Inst.SwitchBlock.ItemInfo,
54135369
5414 pub fn isUnder(case: *const Case) bool {
5415 return case.index.is_under;
5416 }
5417
54185370 pub const Index = packed struct(u32) {
54195371 kind: enum(u1) { scalar, multi },
5420 is_under: bool,
5421 value: u30,
5372 value: u31,
54225373
54235374 pub const @"else": Case.Index = .{
54245375 .kind = .scalar,
5425 .is_under = false,
5426 .value = std.math.maxInt(u30),
5427 };
5428
5429 pub const bare_under: Case.Index = .{
5430 .kind = .scalar,
5431 .is_under = true,
5432 .value = std.math.maxInt(u30),
5376 .value = std.math.maxInt(u31),
54335377 };
54345378 };
54355379
......@@ -5448,31 +5392,8 @@ pub const UnwrappedSwitchBlock = struct {
54485392 is_simple_noreturn: bool,
54495393 };
54505394
5451 pub const Under = union(enum) {
5452 none,
5453 bare: Under.Resolved,
5454 index: u32,
5455
5456 pub const Resolved = struct {
5457 index: Case.Index,
5458 body: []const Inst.Index,
5459 capture: Inst.SwitchBlock.ProngInfo.Capture,
5460 has_tag_capture: bool,
5461 };
5462
5463 /// If this returns `null` and `under` is not `.none`, you'll have to
5464 /// find the under case by iterating all cases and using `isUnder`!
5465 pub fn resolve(under: Under) ?Under.Resolved {
5466 return switch (under) {
5467 .bare => |resolved| resolved,
5468 .none, .index => null,
5469 };
5470 }
5471 };
5472
54735395 pub const Iterator = struct {
54745396 next_idx: u32,
5475 under_idx: ?u32,
54765397 prong_infos: []const Inst.SwitchBlock.ProngInfo,
54775398 multi_case_items_lens: []const u32,
54785399 multi_case_ranges_lens: ?[]const u32,
......@@ -5486,7 +5407,6 @@ pub const UnwrappedSwitchBlock = struct {
54865407 return if (idx < scalar_cases_len) .{
54875408 .index = .{
54885409 .kind = .scalar,
5489 .is_under = idx == it.under_idx,
54905410 .value = @intCast(idx),
54915411 },
54925412 .prong_info = it.prong_infos[idx],
......@@ -5495,7 +5415,6 @@ pub const UnwrappedSwitchBlock = struct {
54955415 } else .{
54965416 .index = .{
54975417 .kind = .multi,
5498 .is_under = idx == it.under_idx,
54995418 .value = @intCast(idx - scalar_cases_len),
55005419 },
55015420 .prong_info = it.prong_infos[idx],
......@@ -5516,10 +5435,6 @@ pub const UnwrappedSwitchBlock = struct {
55165435 pub fn iterateCases(unwrapped: UnwrappedSwitchBlock) Case.Iterator {
55175436 return .{
55185437 .next_idx = 0,
5519 .under_idx = switch (unwrapped.under_case) {
5520 .none, .bare => null,
5521 .index => |index| index,
5522 },
55235438 .prong_infos = unwrapped.prong_infos,
55245439 .multi_case_items_lens = unwrapped.multi_case_items_lens,
55255440 .multi_case_ranges_lens = unwrapped.multi_case_ranges_lens,
src/Sema.zig+112-129
......@@ -10753,10 +10753,9 @@ fn analyzeSwitchBlock(
1075310753 const operand_src = block.src(.{ .node_offset_switch_operand = src_node_offset });
1075410754
1075510755 const has_else = zir_switch.else_case != null;
10756 const has_under = zir_switch.under_case != .none;
10756 const has_under = zir_switch.has_under;
1075710757
1075810758 const else_case = validated_switch.else_case;
10759 const under_case = validated_switch.under_case;
1076010759
1076110760 const operand: SwitchOperand, const operand_ty: Type, const maybe_operand_opv: ?Value, const item_ty: Type = operand: {
1076210761 const val, const ref = if (operand_is_ref)
......@@ -10933,9 +10932,6 @@ fn analyzeSwitchBlock(
1093310932 // we allow simple noreturn else prongs when switching on error sets!
1093410933 break :find_prong .{ else_case.index, else_case.body, else_case.capture, else_case.has_tag_capture, else_case.is_inline, true };
1093510934 }
10936 if (has_under) {
10937 break :find_prong .{ under_case.index, under_case.body, under_case.capture, under_case.has_tag_capture, false, true };
10938 }
1093910935 unreachable; // malformed validated switch
1094010936 };
1094110937
......@@ -11083,10 +11079,9 @@ fn finishSwitchBr(
1108311079 const operand_src = block.src(.{ .node_offset_switch_operand = src_node_offset });
1108411080
1108511081 const has_else = zir_switch.else_case != null;
11086 const has_under = zir_switch.under_case != .none;
11082 const has_under = zir_switch.has_under;
1108711083
1108811084 const else_case = validated_switch.else_case;
11089 const under_case = validated_switch.under_case;
1109011085
1109111086 const scalar_cases_len = zir_switch.scalarCasesLen();
1109211087 const multi_cases_len = zir_switch.multiCasesLen();
......@@ -11138,20 +11133,15 @@ fn finishSwitchBr(
1113811133 var case_it = zir_switch.iterateCases();
1113911134 var extra_index = zir_switch.end;
1114011135
11136 var under_prong: ?struct {
11137 index: Zir.UnwrappedSwitchBlock.Case.Index,
11138 body: []const Zir.Inst.Index,
11139 capture: Zir.Inst.SwitchBlock.ProngInfo.Capture,
11140 has_tag_capture: bool,
11141 } = null;
11142
1114111143 var cases_len: u32 = 0;
1114211144 while (case_it.next()) |case| {
11143 if (case.isUnder()) { // we'll deal with this later
11144 extra_index += case.prong_info.body_len;
11145 for (case.item_infos) |item_info| {
11146 if (item_info.bodyLen()) |body_len| extra_index += body_len;
11147 }
11148 for (case.range_infos) |range_info| {
11149 if (range_info[0].bodyLen()) |body_len| extra_index += body_len;
11150 if (range_info[1].bodyLen()) |body_len| extra_index += body_len;
11151 }
11152 continue;
11153 }
11154
1115511145 const item_refs = case_vals[case_val_idx..][0..case.item_infos.len];
1115611146 case_val_idx += item_refs.len;
1115711147 const range_refs: []const [2]Air.Inst.Ref =
......@@ -11171,7 +11161,9 @@ fn finishSwitchBr(
1117111161
1117211162 var emit_bb = false;
1117311163 var any_analyze_body = false;
11164 var is_under_prong = false;
1117411165 for (case.item_infos, item_refs, 0..) |item_info, item_ref, item_i| {
11166 if (item_ref == .none) is_under_prong = true;
1117511167 if (item_info.bodyLen()) |body_len| extra_index += body_len;
1117611168
1117711169 const analyze_body = sema.wantSwitchProngBodyAnalysis(block, item_ref, operand_ty, union_originally, err_set, prong_info.is_comptime_unreach);
......@@ -11319,48 +11311,58 @@ fn finishSwitchBr(
1131911311 }
1132011312 }
1132111313
11322 if (!prong_info.is_inline) {
11323 cases_len += 1;
11324 case_block.instructions.clearRetainingCapacity();
11325 case_block.error_return_trace_index = child_block.error_return_trace_index;
11314 if (prong_info.is_inline) continue; // handled above
1132611315
11327 const prong_hint: std.builtin.BranchHint = hint: {
11328 if (any_analyze_body) break :hint try sema.analyzeSwitchProng(
11329 &case_block,
11330 operand,
11331 operand_ty,
11332 raw_operand_ty,
11333 prong_body,
11334 block.src(.{ .switch_capture = .{
11335 .switch_node_offset = src_node_offset,
11336 .case_idx = case.index,
11337 } }),
11338 prong_info.capture,
11339 prong_info.has_tag_capture,
11340 .none,
11341 .{ .item_refs = item_refs },
11342 validated_switch.else_err_ty,
11343 switch_inst,
11344 zir_switch,
11345 );
11346 _ = try case_block.addNoOp(.unreach);
11347 break :hint .cold; // unreachable branches are cold
11316 if (is_under_prong) {
11317 under_prong = .{
11318 .index = case.index,
11319 .body = prong_body,
11320 .capture = case.prong_info.capture,
11321 .has_tag_capture = case.prong_info.has_tag_capture,
1134811322 };
11349 try branch_hints.append(gpa, prong_hint);
11350
11351 try cases_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.SwitchBr.Case).@"struct".fields.len +
11352 item_refs.len +
11353 2 * range_refs.len +
11354 case_block.instructions.items.len);
11355 cases_extra.appendSliceAssumeCapacity(&payloadToExtraItems(Air.SwitchBr.Case{
11356 .items_len = @intCast(item_refs.len),
11357 .ranges_len = @intCast(range_refs.len),
11358 .body_len = @intCast(case_block.instructions.items.len),
11359 }));
11360 cases_extra.appendSliceAssumeCapacity(@ptrCast(item_refs));
11361 cases_extra.appendSliceAssumeCapacity(@ptrCast(range_refs));
11362 cases_extra.appendSliceAssumeCapacity(@ptrCast(case_block.instructions.items));
11323 continue;
1136311324 }
11325
11326 cases_len += 1;
11327 case_block.instructions.clearRetainingCapacity();
11328 case_block.error_return_trace_index = child_block.error_return_trace_index;
11329
11330 const prong_hint: std.builtin.BranchHint = hint: {
11331 if (any_analyze_body) break :hint try sema.analyzeSwitchProng(
11332 &case_block,
11333 operand,
11334 operand_ty,
11335 raw_operand_ty,
11336 prong_body,
11337 block.src(.{ .switch_capture = .{
11338 .switch_node_offset = src_node_offset,
11339 .case_idx = case.index,
11340 } }),
11341 prong_info.capture,
11342 prong_info.has_tag_capture,
11343 .none,
11344 .{ .item_refs = item_refs },
11345 validated_switch.else_err_ty,
11346 switch_inst,
11347 zir_switch,
11348 );
11349 _ = try case_block.addNoOp(.unreach);
11350 break :hint .cold; // unreachable branches are cold
11351 };
11352 try branch_hints.append(gpa, prong_hint);
11353
11354 try cases_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.SwitchBr.Case).@"struct".fields.len +
11355 item_refs.len +
11356 2 * range_refs.len +
11357 case_block.instructions.items.len);
11358 cases_extra.appendSliceAssumeCapacity(&payloadToExtraItems(Air.SwitchBr.Case{
11359 .items_len = @intCast(item_refs.len),
11360 .ranges_len = @intCast(range_refs.len),
11361 .body_len = @intCast(case_block.instructions.items.len),
11362 }));
11363 cases_extra.appendSliceAssumeCapacity(@ptrCast(item_refs));
11364 cases_extra.appendSliceAssumeCapacity(@ptrCast(range_refs));
11365 cases_extra.appendSliceAssumeCapacity(@ptrCast(case_block.instructions.items));
1136411366 }
1136511367
1136611368 const catch_all_extra: []const u32 = catch_all_extra: {
......@@ -11499,7 +11501,7 @@ fn finishSwitchBr(
1149911501 try branch_hints.append(gpa, prong_hint);
1150011502
1150111503 try cases_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.SwitchBr.Case).@"struct".fields.len +
11502 (validated_switch.seen_enum_fields.len - zir_switch.totalItemsLen()) +
11504 (validated_switch.seen_enum_fields.len + 1 - zir_switch.totalItemsLen()) + // +1 because totalItemsLen includes the _
1150311505 case_block.instructions.items.len);
1150411506 const extra_case = cases_extra.addManyAsArrayAssumeCapacity(
1150511507 @typeInfo(Air.SwitchBr.Case).@"struct".fields.len,
......@@ -11555,7 +11557,7 @@ fn finishSwitchBr(
1155511557 if (analyze_catch_all_body) {
1155611558 const index, const body, const capture, const has_tag_capture = switch (catch_all_case) {
1155711559 .@"else" => .{ else_case.index, else_case.body, else_case.capture, else_case.has_tag_capture },
11558 .under => .{ under_case.index, under_case.body, under_case.capture, under_case.has_tag_capture },
11560 .under => .{ under_prong.?.index, under_prong.?.body, under_prong.?.capture, under_prong.?.has_tag_capture },
1155911561 .none => unreachable,
1156011562 };
1156111563 break :hint try sema.analyzeSwitchProng(
......@@ -11734,7 +11736,6 @@ fn fixupSwitchContinues(
1173411736const ValidatedSwitchBlock = struct {
1173511737 seen_enum_fields: []const ?LazySrcLoc,
1173611738 seen_errors: std.AutoHashMapUnmanaged(InternPool.NullTerminatedString, LazySrcLoc),
11737 seen_sparse_values: std.AutoHashMapUnmanaged(InternPool.Index, LazySrcLoc),
1173811739 seen_ranges: []const RangeSet.Range,
1173911740 true_src: ?LazySrcLoc,
1174011741 false_src: ?LazySrcLoc,
......@@ -11742,7 +11743,6 @@ const ValidatedSwitchBlock = struct {
1174211743
1174311744 case_vals: []const Air.Inst.Ref,
1174411745 else_case: Zir.UnwrappedSwitchBlock.Case.Else,
11745 under_case: Zir.UnwrappedSwitchBlock.Case.Under.Resolved,
1174611746 else_err_ty: ?Type,
1174711747
1174811748 fn iterateUnhandledItems(
......@@ -11868,7 +11868,6 @@ fn validateSwitchBlock(
1186811868 const src = block.nodeOffset(src_node_offset);
1186911869 const operand_src = block.src(.{ .node_offset_switch_operand = src_node_offset });
1187011870 const else_prong_src = block.src(.{ .node_offset_switch_else_prong = src_node_offset });
11871 const under_prong_src = block.src(.{ .node_offset_switch_under_prong = src_node_offset });
1187211871 var extra_index = zir_switch.end;
1187311872
1187411873 // We want to map values to our placeholders later on.
......@@ -11948,33 +11947,7 @@ fn validateSwitchBlock(
1194811947 };
1194911948
1195011949 const has_else = zir_switch.else_case != null;
11951 const has_under = zir_switch.under_case != .none;
11952
11953 // Validate usage of '_' prongs.
11954 if (has_under and !operand_ty.isNonexhaustiveEnum(zcu)) {
11955 const msg = msg: {
11956 const msg = try sema.errMsg(
11957 src,
11958 "'_' prong only allowed when switching on non-exhaustive enums",
11959 .{},
11960 );
11961 errdefer msg.destroy(gpa);
11962 try sema.errNote(
11963 under_prong_src,
11964 msg,
11965 "'_' prong here",
11966 .{},
11967 );
11968 try sema.errNote(
11969 src,
11970 msg,
11971 "consider using 'else'",
11972 .{},
11973 );
11974 break :msg msg;
11975 };
11976 return sema.failWithOwnedErrorMsg(block, msg);
11977 }
11950 const has_under = zir_switch.has_under;
1197811951
1197911952 var case_vals: std.ArrayList(Air.Inst.Ref) = .empty;
1198011953 try case_vals.ensureUnusedCapacity(arena, zir_switch.item_infos.len);
......@@ -11991,7 +11964,6 @@ fn validateSwitchBlock(
1199111964 var else_err_ty: ?Type = null;
1199211965
1199311966 const else_case = zir_switch.else_case orelse undefined;
11994 var under_case = zir_switch.under_case.resolve() orelse undefined;
1199511967
1199611968 switch (item_ty.zigTypeTag(zcu)) {
1199711969 .@"union" => unreachable,
......@@ -12020,16 +11992,6 @@ fn validateSwitchBlock(
1202011992 var case_it = zir_switch.iterateCases();
1202111993 while (case_it.next()) |case| {
1202211994 const prong_info = case.prong_info;
12023 const is_under = case.isUnder();
12024 if (is_under) {
12025 assert(!prong_info.is_inline);
12026 under_case = .{
12027 .index = case.index,
12028 .body = sema.code.bodySlice(extra_index, prong_info.body_len),
12029 .capture = prong_info.capture,
12030 .has_tag_capture = prong_info.has_tag_capture,
12031 };
12032 }
1203311995 extra_index += prong_info.body_len;
1203411996 for (case.item_infos, 0..) |item_info, item_i| {
1203511997 const item_src = block.src(.{ .switch_case_item = .{
......@@ -12037,9 +11999,34 @@ fn validateSwitchBlock(
1203711999 .case_idx = case.index,
1203812000 .item_idx = .{ .kind = .single, .value = @intCast(item_i) },
1203912001 } });
12040 const item, extra_index = try sema.resolveSwitchItem(block, item_src, item_ty, item_info, extra_index, switch_inst, prong_info.is_comptime_unreach, prong_info.is_inline);
12041 try sema.validateSwitchItemOrRange(block, item_src, item.val, null, item_ty, seen_enum_fields, &seen_errors, &seen_sparse_values, &range_set, &true_src, &false_src, &void_src);
12042 if (!is_under) case_vals.appendAssumeCapacity(item.ref);
12002 if (item_info.unwrap() == .under) {
12003 if (!operand_ty.isNonexhaustiveEnum(zcu)) return sema.failWithOwnedErrorMsg(block, msg: {
12004 const msg = try sema.errMsg(
12005 src,
12006 "'_' prong only allowed when switching on non-exhaustive enums",
12007 .{},
12008 );
12009 errdefer msg.destroy(gpa);
12010 try sema.errNote(
12011 item_src,
12012 msg,
12013 "'_' prong here",
12014 .{},
12015 );
12016 try sema.errNote(
12017 src,
12018 msg,
12019 "consider using 'else'",
12020 .{},
12021 );
12022 break :msg msg;
12023 });
12024 case_vals.appendAssumeCapacity(.none);
12025 } else {
12026 const item, extra_index = try sema.resolveSwitchItem(block, item_src, item_ty, item_info, extra_index, switch_inst, prong_info.is_comptime_unreach);
12027 try sema.validateSwitchItemOrRange(block, item_src, item.val, null, item_ty, seen_enum_fields, &seen_errors, &seen_sparse_values, &range_set, &true_src, &false_src, &void_src);
12028 case_vals.appendAssumeCapacity(item.ref);
12029 }
1204312030 }
1204412031 for (case.range_infos, 0..) |range_info, range_i| {
1204512032 const range_offset: LazySrcLoc.Offset.SwitchItem = .{
......@@ -12050,10 +12037,10 @@ fn validateSwitchBlock(
1205012037 const range_src = block.src(.{ .switch_case_item = range_offset });
1205112038 const first_src = block.src(.{ .switch_case_item_range_first = range_offset });
1205212039 const last_src = block.src(.{ .switch_case_item_range_last = range_offset });
12053 const first_item, extra_index = try sema.resolveSwitchItem(block, first_src, item_ty, range_info[0], extra_index, switch_inst, prong_info.is_comptime_unreach, prong_info.is_inline);
12054 const last_item, extra_index = try sema.resolveSwitchItem(block, last_src, item_ty, range_info[1], extra_index, switch_inst, prong_info.is_comptime_unreach, prong_info.is_inline);
12040 const first_item, extra_index = try sema.resolveSwitchItem(block, first_src, item_ty, range_info[0], extra_index, switch_inst, prong_info.is_comptime_unreach);
12041 const last_item, extra_index = try sema.resolveSwitchItem(block, last_src, item_ty, range_info[1], extra_index, switch_inst, prong_info.is_comptime_unreach);
1205512042 try sema.validateSwitchItemOrRange(block, range_src, first_item.val, last_item.val, item_ty, seen_enum_fields, &seen_errors, &seen_sparse_values, &range_set, &true_src, &false_src, &void_src);
12056 if (!is_under) case_vals.appendSliceAssumeCapacity(&.{ first_item.ref, last_item.ref });
12043 case_vals.appendSliceAssumeCapacity(&.{ first_item.ref, last_item.ref });
1205712044 }
1205812045 }
1205912046
......@@ -12285,7 +12272,6 @@ fn validateSwitchBlock(
1228512272 return .{
1228612273 .seen_enum_fields = seen_enum_fields,
1228712274 .seen_errors = seen_errors,
12288 .seen_sparse_values = seen_sparse_values,
1228912275 .seen_ranges = range_set.ranges.items,
1229012276 .true_src = true_src,
1229112277 .false_src = false_src,
......@@ -12293,7 +12279,6 @@ fn validateSwitchBlock(
1229312279
1229412280 .case_vals = case_vals.items,
1229512281 .else_case = else_case,
12296 .under_case = under_case,
1229712282 .else_err_ty = else_err_ty,
1229812283 };
1229912284}
......@@ -12334,19 +12319,13 @@ fn resolveSwitchBlock(
1233412319 var case_val_idx: usize = 0;
1233512320 var extra_index = zir_switch.end;
1233612321 var case_it = zir_switch.iterateCases();
12322 var under_prong: ?struct {
12323 index: Zir.UnwrappedSwitchBlock.Case.Index,
12324 body: []const Zir.Inst.Index,
12325 capture: Zir.Inst.SwitchBlock.ProngInfo.Capture,
12326 has_tag_capture: bool,
12327 } = null;
1233712328 while (case_it.next()) |case| {
12338 if (case.isUnder()) { // we'll deal with this later
12339 extra_index += case.prong_info.body_len;
12340 for (case.item_infos) |item_info| {
12341 if (item_info.bodyLen()) |body_len| extra_index += body_len;
12342 }
12343 for (case.range_infos) |range_info| {
12344 if (range_info[0].bodyLen()) |body_len| extra_index += body_len;
12345 if (range_info[1].bodyLen()) |body_len| extra_index += body_len;
12346 }
12347 continue;
12348 }
12349
1235012329 const prong_info = case.prong_info;
1235112330 const prong_body = sema.code.bodySlice(extra_index, prong_info.body_len);
1235212331 extra_index += prong_body.len;
......@@ -12363,6 +12342,15 @@ fn resolveSwitchBlock(
1236312342 const range_refs: []const [2]Air.Inst.Ref = @ptrCast(case_vals[case_val_idx..][0 .. 2 * case.range_infos.len]);
1236412343 case_val_idx += 2 * range_refs.len;
1236512344 for (item_refs) |item_ref| {
12345 if (item_ref == .none) {
12346 under_prong = .{
12347 .index = case.index,
12348 .body = prong_body,
12349 .capture = case.prong_info.capture,
12350 .has_tag_capture = case.prong_info.has_tag_capture,
12351 };
12352 continue;
12353 }
1236612354 const item_val = sema.resolveConstDefinedValue(child_block, .unneeded, item_ref, undefined) catch unreachable;
1236712355 if (cond_val.eql(item_val, item_ty, zcu)) {
1236812356 if (err_set) try sema.maybeErrorUnwrapComptime(child_block, prong_body, cond_ref);
......@@ -12421,7 +12409,6 @@ fn resolveSwitchBlock(
1242112409 }
1242212410
1242312411 const else_case = validated_switch.else_case;
12424 const under_case = validated_switch.under_case;
1242512412
1242612413 // named-only prong
1242712414
......@@ -12452,7 +12439,7 @@ fn resolveSwitchBlock(
1245212439
1245312440 const index, const body, const capture, const has_tag_capture, const is_inline = switch (catch_all_case) {
1245412441 .@"else" => .{ else_case.index, else_case.body, else_case.capture, else_case.has_tag_capture, else_case.is_inline },
12455 .under => .{ under_case.index, under_case.body, under_case.capture, under_case.has_tag_capture, false },
12442 .under => .{ under_prong.?.index, under_prong.?.body, under_prong.?.capture, under_prong.?.has_tag_capture, false },
1245612443 .none => unreachable,
1245712444 };
1245812445 if (err_set) try sema.maybeErrorUnwrapComptime(child_block, body, cond_ref);
......@@ -13169,7 +13156,6 @@ fn resolveSwitchItem(
1316913156 extra_index: usize,
1317013157 switch_inst: Zir.Inst.Index,
1317113158 prong_is_comptime_unreach: bool,
13172 prong_is_inline: bool,
1317313159) CompileError!ResolvedSwitchItemAndExtraIndex {
1317413160 const pt = sema.pt;
1317513161 const zcu = pt.zcu;
......@@ -13180,6 +13166,7 @@ fn resolveSwitchItem(
1318013166
1318113167 var end = extra_index;
1318213168 const uncoerced: Air.Inst.Ref, const uncoerced_ty: Type = uncoerced: switch (item_info.unwrap()) {
13169 .under => unreachable, // caller must check this before calling us
1318313170 .enum_literal => |str_index| {
1318413171 const zir_str = sema.code.nullTerminatedString(str_index);
1318513172 const name = try ip.getOrPutString(gpa, io, pt.tid, zir_str, .no_embedded_nulls);
......@@ -13198,10 +13185,6 @@ fn resolveSwitchItem(
1319813185 } }));
1319913186 break :uncoerced .{ uncoerced, err_set_ty };
1320013187 },
13201 .number_literal => |zir_ref| {
13202 const uncoerced = try sema.resolveInst(zir_ref);
13203 break :uncoerced .{ uncoerced, sema.typeOf(uncoerced) };
13204 },
1320513188 .body_len => |body_len| {
1320613189 const body = sema.code.bodySlice(extra_index, body_len);
1320713190 end += body.len;
......@@ -13231,7 +13214,7 @@ fn resolveSwitchItem(
1323113214 .ok => if (try sema.resolveValue(uncoerced)) |uncoerced_val| {
1323213215 break :item_ref try sema.coerceInMemory(uncoerced_val, item_ty);
1323313216 },
13234 .missing_error => if (prong_is_comptime_unreach and !prong_is_inline) {
13217 .missing_error => if (prong_is_comptime_unreach) {
1323513218 break :item_ref uncoerced;
1323613219 },
1323713220 .from_anyerror => {},
src/Zcu.zig+1-41
......@@ -1742,27 +1742,6 @@ pub const SrcLoc = struct {
17421742 } else unreachable;
17431743 },
17441744
1745 .node_offset_switch_under_prong => |node_off| {
1746 const tree = try src_loc.file_scope.getTree(zcu);
1747 const switch_node = node_off.toAbsolute(src_loc.base_node);
1748 _, const extra_index = tree.nodeData(switch_node).node_and_extra;
1749 const case_nodes = tree.extraDataSlice(tree.extraData(extra_index, Ast.Node.SubRange), Ast.Node.Index);
1750 for (case_nodes) |case_node| {
1751 const case = tree.fullSwitchCase(case_node).?;
1752 for (case.ast.values) |val| {
1753 if (tree.nodeTag(val) == .identifier and
1754 mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(val)), "_"))
1755 {
1756 return tree.tokensToSpan(
1757 tree.firstToken(case_node),
1758 tree.lastToken(case_node),
1759 tree.nodeMainToken(val),
1760 );
1761 }
1762 }
1763 } else unreachable;
1764 },
1765
17661745 .node_offset_switch_range => |node_off| {
17671746 const tree = try src_loc.file_scope.getTree(zcu);
17681747 const switch_node = node_off.toAbsolute(src_loc.base_node);
......@@ -2176,7 +2155,6 @@ pub const SrcLoc = struct {
21762155
21772156 var multi_i: u32 = 0;
21782157 var scalar_i: u32 = 0;
2179 var underscore_node: Ast.Node.OptionalIndex = .none;
21802158 const case: Ast.full.SwitchCase = case: for (case_nodes) |case_node| {
21812159 const case = tree.fullSwitchCase(case_node).?;
21822160 if (case.ast.values.len == 0) {
......@@ -2185,17 +2163,6 @@ pub const SrcLoc = struct {
21852163 }
21862164 continue :case;
21872165 }
2188 if (underscore_node == .none) {
2189 for (case.ast.values) |value| {
2190 if (tree.nodeTag(value) == .identifier and
2191 mem.eql(u8, tree.tokenSlice(tree.nodeMainToken(value)), "_"))
2192 {
2193 underscore_node = value.toOptional();
2194 if (want_case_idx.is_under) break :case case;
2195 if (case.ast.values.len == 1) continue :case;
2196 }
2197 }
2198 }
21992166
22002167 const is_multi = case.ast.values.len != 1 or
22012168 tree.nodeTag(case.ast.values[0]) == .switch_range;
......@@ -2220,7 +2187,6 @@ pub const SrcLoc = struct {
22202187 .switch_case_item_range_last,
22212188 => |x| item_idx: {
22222189 assert(want_case_idx != Zir.UnwrappedSwitchBlock.Case.Index.@"else");
2223 assert(want_case_idx != Zir.UnwrappedSwitchBlock.Case.Index.bare_under);
22242190 break :item_idx x.item_idx;
22252191 },
22262192 .switch_capture, .switch_tag_capture => {
......@@ -2247,9 +2213,7 @@ pub const SrcLoc = struct {
22472213 .single => {
22482214 var item_i: u32 = 0;
22492215 for (case.ast.values) |item_node| {
2250 if (item_node.toOptional() == underscore_node or
2251 tree.nodeTag(item_node) == .switch_range)
2252 {
2216 if (tree.nodeTag(item_node) == .switch_range) {
22532217 continue;
22542218 }
22552219 if (item_i != want_item_idx.value) {
......@@ -2456,10 +2420,6 @@ pub const LazySrcLoc = struct {
24562420 /// by taking this AST node index offset from the containing base node,
24572421 /// which points to a switch expression AST node. Next, navigate to the else prong.
24582422 node_offset_switch_else_prong: Ast.Node.Offset,
2459 /// The source location points to the `_` prong of a switch expression, found
2460 /// by taking this AST node index offset from the containing base node,
2461 /// which points to a switch expression AST node. Next, navigate to the `_` prong.
2462 node_offset_switch_under_prong: Ast.Node.Offset,
24632423 /// The source location points to all the ranges of a switch expression, found
24642424 /// by taking this AST node index offset from the containing base node,
24652425 /// which points to a switch expression AST node. Next, navigate to any of the
src/print_zir.zig+37-47
......@@ -2021,15 +2021,6 @@ const Writer = struct {
20212021 try stream.writeAll("else => ");
20222022 try self.writeBracedBody(stream, else_case.body);
20232023 }
2024 if (zir_switch.under_case.resolve()) |under_case| {
2025 try stream.writeAll(",\n");
2026 try stream.splatByteAll(' ', self.indent);
2027
2028 try self.writeSwitchCaptures(stream, under_case.capture, under_case.has_tag_capture, inst, &zir_switch);
2029
2030 try stream.writeAll("_ => ");
2031 try self.writeBracedBody(stream, under_case.body);
2032 }
20332024
20342025 var case_it = zir_switch.iterateCases();
20352026 while (case_it.next()) |case| {
......@@ -2043,14 +2034,8 @@ const Writer = struct {
20432034 const prong_body = self.code.bodySlice(extra_index, prong_info.body_len);
20442035 extra_index += prong_body.len;
20452036
2046 var first_item: bool = true;
2047 if (case.isUnder()) {
2048 try stream.writeAll("_");
2049 first_item = false;
2050 }
2051 for (case.item_infos) |item_info| {
2052 if (!first_item) try stream.writeAll(", ");
2053 first_item = false;
2037 for (case.item_infos, 0..) |item_info, i| {
2038 if (i > 0) try stream.writeAll(", ");
20542039
20552040 switch (item_info.unwrap()) {
20562041 .enum_literal => |str_index| {
......@@ -2061,9 +2046,7 @@ const Writer = struct {
20612046 const str = self.code.nullTerminatedString(str_index);
20622047 try stream.print("\"error.{f}\"", .{std.zig.fmtString(str)});
20632048 },
2064 .number_literal => |zir_ref| {
2065 try self.writeInstRef(stream, zir_ref);
2066 },
2049 .under => try stream.writeByte('_'),
20672050 .body_len => |body_len| {
20682051 const item_body = self.code.bodySlice(extra_index, body_len);
20692052 extra_index += item_body.len;
......@@ -2071,33 +2054,40 @@ const Writer = struct {
20712054 },
20722055 }
20732056 }
2074 for (case.range_infos) |range_info| {
2075 if (!first_item) try stream.writeAll(", ");
2076 first_item = false;
2077
2078 var first_range_item = true;
2079 for (&range_info) |item_info| {
2080 if (!first_range_item) try stream.writeAll("...");
2081 first_range_item = false;
2082
2083 switch (item_info.unwrap()) {
2084 .enum_literal => |str_index| {
2085 const str = self.code.nullTerminatedString(str_index);
2086 try stream.print("\".{f}\"", .{std.zig.fmtString(str)});
2087 },
2088 .error_value => |str_index| {
2089 const str = self.code.nullTerminatedString(str_index);
2090 try stream.print("\"error.{f}\"", .{std.zig.fmtString(str)});
2091 },
2092 .number_literal => |zir_ref| {
2093 try self.writeInstRef(stream, zir_ref);
2094 },
2095 .body_len => |body_len| {
2096 const item_body = self.code.bodySlice(extra_index, body_len);
2097 extra_index += item_body.len;
2098 try self.writeBracedDecl(stream, item_body);
2099 },
2100 }
2057 for (case.range_infos, 0..) |range_info, i| {
2058 if (i > 0 and case.item_infos.len == 0) try stream.writeAll(", ");
2059 switch (range_info[0].unwrap()) {
2060 .enum_literal => |str_index| {
2061 const str = self.code.nullTerminatedString(str_index);
2062 try stream.print("\".{f}\"", .{std.zig.fmtString(str)});
2063 },
2064 .error_value => |str_index| {
2065 const str = self.code.nullTerminatedString(str_index);
2066 try stream.print("\"error.{f}\"", .{std.zig.fmtString(str)});
2067 },
2068 .under => unreachable, // '_..._' is not allowed
2069 .body_len => |body_len| {
2070 const item_body = self.code.bodySlice(extra_index, body_len);
2071 extra_index += item_body.len;
2072 try self.writeBracedDecl(stream, item_body);
2073 },
2074 }
2075 try stream.writeAll("...");
2076 switch (range_info[1].unwrap()) {
2077 .enum_literal => |str_index| {
2078 const str = self.code.nullTerminatedString(str_index);
2079 try stream.print("\".{f}\"", .{std.zig.fmtString(str)});
2080 },
2081 .error_value => |str_index| {
2082 const str = self.code.nullTerminatedString(str_index);
2083 try stream.print("\"error.{f}\"", .{std.zig.fmtString(str)});
2084 },
2085 .under => unreachable, // '_..._' is not allowed
2086 .body_len => |body_len| {
2087 const item_body = self.code.bodySlice(extra_index, body_len);
2088 extra_index += item_body.len;
2089 try self.writeBracedDecl(stream, item_body);
2090 },
21012091 }
21022092 }
21032093 try stream.writeAll(" => ");