authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-07 18:43:18+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-03-08 07:30:32-08:00
logf3227598ebe9ac7e330fea0259d4290ee31e96b9
tree17775fda93056d23999e1289ea96917d03fdfe59
parentb2427ea7d839a9e17568206c64da56865cd000f1

Sema: reset block error return trace index between cases

Resolves: #19210

2 files changed, 39 insertions(+), 0 deletions(-)

src/Sema.zig+11
......@@ -12304,6 +12304,7 @@ fn analyzeSwitchRuntimeBlock(
1230412304 extra_index += info.body_len;
1230512305
1230612306 case_block.instructions.shrinkRetainingCapacity(0);
12307 case_block.error_return_trace_index = child_block.error_return_trace_index;
1230712308
1230812309 const item = case_vals.items[scalar_i];
1230912310 // `item` is already guaranteed to be constant known.
......@@ -12361,6 +12362,7 @@ fn analyzeSwitchRuntimeBlock(
1236112362 case_val_idx += items_len;
1236212363
1236312364 case_block.instructions.shrinkRetainingCapacity(0);
12365 case_block.error_return_trace_index = child_block.error_return_trace_index;
1236412366
1236512367 // Generate all possible cases as scalar prongs.
1236612368 if (info.is_inline) {
......@@ -12392,6 +12394,7 @@ fn analyzeSwitchRuntimeBlock(
1239212394 const item_ref = Air.internedToRef(item.toIntern());
1239312395
1239412396 case_block.instructions.shrinkRetainingCapacity(0);
12397 case_block.error_return_trace_index = child_block.error_return_trace_index;
1239512398
1239612399 if (emit_bb) sema.emitBackwardBranch(block, .unneeded) catch |err| switch (err) {
1239712400 error.NeededSourceLocation => {
......@@ -12431,6 +12434,7 @@ fn analyzeSwitchRuntimeBlock(
1243112434 cases_len += 1;
1243212435
1243312436 case_block.instructions.shrinkRetainingCapacity(0);
12437 case_block.error_return_trace_index = child_block.error_return_trace_index;
1243412438
1243512439 const analyze_body = if (union_originally) blk: {
1243612440 const item_val = sema.resolveConstDefinedValue(block, .unneeded, item, undefined) catch unreachable;
......@@ -12576,6 +12580,7 @@ fn analyzeSwitchRuntimeBlock(
1257612580 defer gpa.free(cond_body);
1257712581
1257812582 case_block.instructions.shrinkRetainingCapacity(0);
12583 case_block.error_return_trace_index = child_block.error_return_trace_index;
1257912584
1258012585 const body = sema.code.bodySlice(extra_index, info.body_len);
1258112586 extra_index += info.body_len;
......@@ -12636,6 +12641,7 @@ fn analyzeSwitchRuntimeBlock(
1263612641 const item_ref = Air.internedToRef(item_val.toIntern());
1263712642
1263812643 case_block.instructions.shrinkRetainingCapacity(0);
12644 case_block.error_return_trace_index = child_block.error_return_trace_index;
1263912645
1264012646 const analyze_body = if (union_originally) blk: {
1264112647 const field_ty = maybe_union_ty.unionFieldType(item_val, mod).?;
......@@ -12686,6 +12692,7 @@ fn analyzeSwitchRuntimeBlock(
1268612692 const item_ref = Air.internedToRef(item_val);
1268712693
1268812694 case_block.instructions.shrinkRetainingCapacity(0);
12695 case_block.error_return_trace_index = child_block.error_return_trace_index;
1268912696
1269012697 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
1269112698 emit_bb = true;
......@@ -12716,6 +12723,7 @@ fn analyzeSwitchRuntimeBlock(
1271612723 const item_ref = Air.internedToRef(cur);
1271712724
1271812725 case_block.instructions.shrinkRetainingCapacity(0);
12726 case_block.error_return_trace_index = child_block.error_return_trace_index;
1271912727
1272012728 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
1272112729 emit_bb = true;
......@@ -12743,6 +12751,7 @@ fn analyzeSwitchRuntimeBlock(
1274312751 cases_len += 1;
1274412752
1274512753 case_block.instructions.shrinkRetainingCapacity(0);
12754 case_block.error_return_trace_index = child_block.error_return_trace_index;
1274612755
1274712756 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
1274812757 emit_bb = true;
......@@ -12768,6 +12777,7 @@ fn analyzeSwitchRuntimeBlock(
1276812777 cases_len += 1;
1276912778
1277012779 case_block.instructions.shrinkRetainingCapacity(0);
12780 case_block.error_return_trace_index = child_block.error_return_trace_index;
1277112781
1277212782 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
1277312783 emit_bb = true;
......@@ -12796,6 +12806,7 @@ fn analyzeSwitchRuntimeBlock(
1279612806 };
1279712807
1279812808 case_block.instructions.shrinkRetainingCapacity(0);
12809 case_block.error_return_trace_index = child_block.error_return_trace_index;
1279912810
1280012811 if (mod.backendSupportsFeature(.is_named_enum_value) and
1280112812 special.body.len != 0 and block.wantSafety() and
test/behavior/switch.zig+28
......@@ -930,3 +930,31 @@ test "prong with inline call to unreachable" {
930930 .bool => |ok| try expect(ok),
931931 }
932932}
933
934test "block error return trace index is reset between prongs" {
935 const S = struct {
936 fn returnError() error{TestFailed} {
937 return error.TestFailed;
938 }
939 };
940
941 var x: u1 = 0;
942 _ = &x;
943
944 const result = switch (x) {
945 0 => {
946 const result: anyerror!i32 = blk: {
947 break :blk 1;
948 };
949 _ = &result;
950 },
951 1 => blk: {
952 const err = switch (x) {
953 0 => {},
954 1 => S.returnError(),
955 };
956 break :blk err;
957 },
958 };
959 try result;
960}