authorgravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2026-03-13 14:11:00+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-03-17 02:05:31+01:00
log097ca369d57a816512b726423ca97e07875a20c8
tree3cd50d0370bfe1658e44fa052dc2929fb0691d8d
parent524345b6353c5aa4a848f4e579ab81f8ac126319

detect when switch on error shadows it's own switch case capture


2 files changed, 25 insertions(+), 1 deletions(-)

lib/std/zig/AstGen.zig+1-1
......@@ -7693,7 +7693,7 @@ fn switchExpr(
76937693 payload_sub_scope = switch_scope;
76947694 } else {
76957695 const capture_name = try astgen.identAsString(ident);
7696 try astgen.detectLocalShadowing(&scratch_scope.base, capture_name, ident, ident_slice, .capture);
7696 try astgen.detectLocalShadowing(switch_scope, capture_name, ident, ident_slice, .capture);
76977697 payload_capture_scope = .{
76987698 .parent = switch_scope,
76997699 .gen_zir = &scratch_scope,
test/cases/compile_errors/switch_on_error_shadows_case_capture.zig created+24
......@@ -0,0 +1,24 @@
1export fn entry1() void {
2 const e: error{Foo}!u32 = error.Foo;
3 e catch |err| switch (err) {
4 error.Foo => |err| {
5 _ = err catch {};
6 },
7 };
8}
9
10export fn entry2() void {
11 const e: error{Foo}!u32 = error.Foo;
12 if (e) {} else |err| switch (err) {
13 error.Foo => |err| {
14 _ = err catch {};
15 },
16 }
17}
18
19// error
20//
21// :4:23: error: redeclaration of capture 'err'
22// :3:14: note: previous declaration here
23// :13:23: error: redeclaration of capture 'err'
24// :12:21: note: previous declaration here