authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-08-31 00:40:59+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-01 18:31:01+01:00
log2b9af9e825fcbfd55f953e7c3bc80decb52a4b2b
treefcb27173da478ec2bdbb412b3d75cccb93bfeab6
parentb7a55cd6c3ca0c4c97f266b72f741b980416456a
signature Commit is signed but in an unrecognized format.

AstGen: error on unused switch label


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

lib/std/zig/AstGen.zig+5
...@@ -7999,6 +7999,11 @@ fn switchExpr(...@@ -7999,6 +7999,11 @@ fn switchExpr(
7999 appendBodyWithFixupsArrayList(astgen, payloads, case_slice);7999 appendBodyWithFixupsArrayList(astgen, payloads, case_slice);
8000 }8000 }
8001 }8001 }
8002
8003 if (switch_full.label_token) |label_token| if (!block_scope.label.?.used) {
8004 try astgen.appendErrorTok(label_token, "unused switch label", .{});
8005 };
8006
8002 // Now that the item expressions are generated we can add this.8007 // Now that the item expressions are generated we can add this.
8003 try parent_gz.instructions.append(gpa, switch_block);8008 try parent_gz.instructions.append(gpa, switch_block);
80048009
test/cases/compile_errors/duplicate-unused_labels.zig+6
...@@ -22,6 +22,11 @@ comptime {...@@ -22,6 +22,11 @@ comptime {
22comptime {22comptime {
23 blk: for (@as([0]void, undefined)) |_| {}23 blk: for (@as([0]void, undefined)) |_| {}
24}24}
25comptime {
26 blk: switch (true) {
27 else => {},
28 }
29}
2530
26// error31// error
27// target=native32// target=native
...@@ -35,3 +40,4 @@ comptime {...@@ -35,3 +40,4 @@ comptime {
35// :17:5: error: unused block label40// :17:5: error: unused block label
36// :20:5: error: unused while loop label41// :20:5: error: unused while loop label
37// :23:5: error: unused for loop label42// :23:5: error: unused for loop label
43// :26:5: error: unused switch label