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(...@@ -12304,6 +12304,7 @@ fn analyzeSwitchRuntimeBlock(
12304 extra_index += info.body_len;12304 extra_index += info.body_len;
1230512305
12306 case_block.instructions.shrinkRetainingCapacity(0);12306 case_block.instructions.shrinkRetainingCapacity(0);
12307 case_block.error_return_trace_index = child_block.error_return_trace_index;
1230712308
12308 const item = case_vals.items[scalar_i];12309 const item = case_vals.items[scalar_i];
12309 // `item` is already guaranteed to be constant known.12310 // `item` is already guaranteed to be constant known.
...@@ -12361,6 +12362,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12361,6 +12362,7 @@ fn analyzeSwitchRuntimeBlock(
12361 case_val_idx += items_len;12362 case_val_idx += items_len;
1236212363
12363 case_block.instructions.shrinkRetainingCapacity(0);12364 case_block.instructions.shrinkRetainingCapacity(0);
12365 case_block.error_return_trace_index = child_block.error_return_trace_index;
1236412366
12365 // Generate all possible cases as scalar prongs.12367 // Generate all possible cases as scalar prongs.
12366 if (info.is_inline) {12368 if (info.is_inline) {
...@@ -12392,6 +12394,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12392,6 +12394,7 @@ fn analyzeSwitchRuntimeBlock(
12392 const item_ref = Air.internedToRef(item.toIntern());12394 const item_ref = Air.internedToRef(item.toIntern());
1239312395
12394 case_block.instructions.shrinkRetainingCapacity(0);12396 case_block.instructions.shrinkRetainingCapacity(0);
12397 case_block.error_return_trace_index = child_block.error_return_trace_index;
1239512398
12396 if (emit_bb) sema.emitBackwardBranch(block, .unneeded) catch |err| switch (err) {12399 if (emit_bb) sema.emitBackwardBranch(block, .unneeded) catch |err| switch (err) {
12397 error.NeededSourceLocation => {12400 error.NeededSourceLocation => {
...@@ -12431,6 +12434,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12431,6 +12434,7 @@ fn analyzeSwitchRuntimeBlock(
12431 cases_len += 1;12434 cases_len += 1;
1243212435
12433 case_block.instructions.shrinkRetainingCapacity(0);12436 case_block.instructions.shrinkRetainingCapacity(0);
12437 case_block.error_return_trace_index = child_block.error_return_trace_index;
1243412438
12435 const analyze_body = if (union_originally) blk: {12439 const analyze_body = if (union_originally) blk: {
12436 const item_val = sema.resolveConstDefinedValue(block, .unneeded, item, undefined) catch unreachable;12440 const item_val = sema.resolveConstDefinedValue(block, .unneeded, item, undefined) catch unreachable;
...@@ -12576,6 +12580,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12576,6 +12580,7 @@ fn analyzeSwitchRuntimeBlock(
12576 defer gpa.free(cond_body);12580 defer gpa.free(cond_body);
1257712581
12578 case_block.instructions.shrinkRetainingCapacity(0);12582 case_block.instructions.shrinkRetainingCapacity(0);
12583 case_block.error_return_trace_index = child_block.error_return_trace_index;
1257912584
12580 const body = sema.code.bodySlice(extra_index, info.body_len);12585 const body = sema.code.bodySlice(extra_index, info.body_len);
12581 extra_index += info.body_len;12586 extra_index += info.body_len;
...@@ -12636,6 +12641,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12636,6 +12641,7 @@ fn analyzeSwitchRuntimeBlock(
12636 const item_ref = Air.internedToRef(item_val.toIntern());12641 const item_ref = Air.internedToRef(item_val.toIntern());
1263712642
12638 case_block.instructions.shrinkRetainingCapacity(0);12643 case_block.instructions.shrinkRetainingCapacity(0);
12644 case_block.error_return_trace_index = child_block.error_return_trace_index;
1263912645
12640 const analyze_body = if (union_originally) blk: {12646 const analyze_body = if (union_originally) blk: {
12641 const field_ty = maybe_union_ty.unionFieldType(item_val, mod).?;12647 const field_ty = maybe_union_ty.unionFieldType(item_val, mod).?;
...@@ -12686,6 +12692,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12686,6 +12692,7 @@ fn analyzeSwitchRuntimeBlock(
12686 const item_ref = Air.internedToRef(item_val);12692 const item_ref = Air.internedToRef(item_val);
1268712693
12688 case_block.instructions.shrinkRetainingCapacity(0);12694 case_block.instructions.shrinkRetainingCapacity(0);
12695 case_block.error_return_trace_index = child_block.error_return_trace_index;
1268912696
12690 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);12697 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
12691 emit_bb = true;12698 emit_bb = true;
...@@ -12716,6 +12723,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12716,6 +12723,7 @@ fn analyzeSwitchRuntimeBlock(
12716 const item_ref = Air.internedToRef(cur);12723 const item_ref = Air.internedToRef(cur);
1271712724
12718 case_block.instructions.shrinkRetainingCapacity(0);12725 case_block.instructions.shrinkRetainingCapacity(0);
12726 case_block.error_return_trace_index = child_block.error_return_trace_index;
1271912727
12720 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);12728 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
12721 emit_bb = true;12729 emit_bb = true;
...@@ -12743,6 +12751,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12743,6 +12751,7 @@ fn analyzeSwitchRuntimeBlock(
12743 cases_len += 1;12751 cases_len += 1;
1274412752
12745 case_block.instructions.shrinkRetainingCapacity(0);12753 case_block.instructions.shrinkRetainingCapacity(0);
12754 case_block.error_return_trace_index = child_block.error_return_trace_index;
1274612755
12747 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);12756 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
12748 emit_bb = true;12757 emit_bb = true;
...@@ -12768,6 +12777,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12768,6 +12777,7 @@ fn analyzeSwitchRuntimeBlock(
12768 cases_len += 1;12777 cases_len += 1;
1276912778
12770 case_block.instructions.shrinkRetainingCapacity(0);12779 case_block.instructions.shrinkRetainingCapacity(0);
12780 case_block.error_return_trace_index = child_block.error_return_trace_index;
1277112781
12772 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);12782 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
12773 emit_bb = true;12783 emit_bb = true;
...@@ -12796,6 +12806,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12796,6 +12806,7 @@ fn analyzeSwitchRuntimeBlock(
12796 };12806 };
1279712807
12798 case_block.instructions.shrinkRetainingCapacity(0);12808 case_block.instructions.shrinkRetainingCapacity(0);
12809 case_block.error_return_trace_index = child_block.error_return_trace_index;
1279912810
12800 if (mod.backendSupportsFeature(.is_named_enum_value) and12811 if (mod.backendSupportsFeature(.is_named_enum_value) and
12801 special.body.len != 0 and block.wantSafety() and12812 special.body.len != 0 and block.wantSafety() and
test/behavior/switch.zig+28
...@@ -930,3 +930,31 @@ test "prong with inline call to unreachable" {...@@ -930,3 +930,31 @@ test "prong with inline call to unreachable" {
930 .bool => |ok| try expect(ok),930 .bool => |ok| try expect(ok),
931 }931 }
932}932}
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}