authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-01-22 02:22:39+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-01-22 04:11:02+00:00
log8470b6ea37a82330be2c5ab41460fe58b8cfd4f6
tree5d491879869d4f97f78b0e7b62ea0ad09dee24fd
parent6e7ae66871575a2bf6b15c0f0280b13a57fc59eb
signaturelock-open Commit is signed but in an unrecognized format.

Zcu: fix switch prong source location resolution

Resolves: #22343

2 files changed, 49 insertions(+), 2 deletions(-)

src/Zcu.zig+3-2
......@@ -1864,15 +1864,16 @@ pub const SrcLoc = struct {
18641864 if (want_case_idx.isSpecial()) {
18651865 break case;
18661866 }
1867 continue;
18671868 }
18681869
18691870 const is_multi = case.ast.values.len != 1 or
18701871 node_tags[case.ast.values[0]] == .switch_range;
18711872
1872 if (!want_case_idx.isSpecial()) switch (want_case_idx.kind) {
1873 switch (want_case_idx.kind) {
18731874 .scalar => if (!is_multi and want_case_idx.index == scalar_i) break case,
18741875 .multi => if (is_multi and want_case_idx.index == multi_i) break case,
1875 };
1876 }
18761877
18771878 if (is_multi) {
18781879 multi_i += 1;
test/cases/compile_errors/invalid_switch_item.zig created+46
......@@ -0,0 +1,46 @@
1const E = enum { a, b, c };
2var my_e: E = .a;
3
4export fn f0() void {
5 switch (my_e) {
6 .a => {},
7 .b => {},
8 .x => {},
9 .c => {},
10 }
11}
12
13export fn f1() void {
14 switch (my_e) {
15 else => {},
16 .x, .y => {},
17 }
18}
19
20export fn f2() void {
21 switch (my_e) {
22 else => {},
23 .a => {},
24 .x, .y => {},
25 .b => {},
26 }
27}
28
29export fn f3() void {
30 switch (my_e) {
31 .a, .b => {},
32 .x, .y => {},
33 else => {},
34 }
35}
36
37// error
38//
39// :8:10: error: no field named 'x' in enum 'tmp.E'
40// :1:11: note: enum declared here
41// :16:10: error: no field named 'x' in enum 'tmp.E'
42// :1:11: note: enum declared here
43// :24:10: error: no field named 'x' in enum 'tmp.E'
44// :1:11: note: enum declared here
45// :32:10: error: no field named 'x' in enum 'tmp.E'
46// :1:11: note: enum declared here