From 8470b6ea37a82330be2c5ab41460fe58b8cfd4f6 Mon Sep 17 00:00:00 2001 From: mlugg Date: Wed, 22 Jan 2025 02:22:39 +0000 Subject: [PATCH] Zcu: fix switch prong source location resolution Resolves: #22343 --- src/Zcu.zig | 5 +- .../compile_errors/invalid_switch_item.zig | 46 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 test/cases/compile_errors/invalid_switch_item.zig diff --git a/src/Zcu.zig b/src/Zcu.zig index 8f35253509..c75cd5d40c 100644 --- a/src/Zcu.zig +++ b/src/Zcu.zig @@ -1864,15 +1864,16 @@ pub const SrcLoc = struct { if (want_case_idx.isSpecial()) { break case; } + continue; } const is_multi = case.ast.values.len != 1 or node_tags[case.ast.values[0]] == .switch_range; - if (!want_case_idx.isSpecial()) switch (want_case_idx.kind) { + switch (want_case_idx.kind) { .scalar => if (!is_multi and want_case_idx.index == scalar_i) break case, .multi => if (is_multi and want_case_idx.index == multi_i) break case, - }; + } if (is_multi) { multi_i += 1; diff --git a/test/cases/compile_errors/invalid_switch_item.zig b/test/cases/compile_errors/invalid_switch_item.zig new file mode 100644 index 0000000000..ee8c2a8b36 --- /dev/null +++ b/test/cases/compile_errors/invalid_switch_item.zig @@ -0,0 +1,46 @@ +const E = enum { a, b, c }; +var my_e: E = .a; + +export fn f0() void { + switch (my_e) { + .a => {}, + .b => {}, + .x => {}, + .c => {}, + } +} + +export fn f1() void { + switch (my_e) { + else => {}, + .x, .y => {}, + } +} + +export fn f2() void { + switch (my_e) { + else => {}, + .a => {}, + .x, .y => {}, + .b => {}, + } +} + +export fn f3() void { + switch (my_e) { + .a, .b => {}, + .x, .y => {}, + else => {}, + } +} + +// error +// +// :8:10: error: no field named 'x' in enum 'tmp.E' +// :1:11: note: enum declared here +// :16:10: error: no field named 'x' in enum 'tmp.E' +// :1:11: note: enum declared here +// :24:10: error: no field named 'x' in enum 'tmp.E' +// :1:11: note: enum declared here +// :32:10: error: no field named 'x' in enum 'tmp.E' +// :1:11: note: enum declared here