| author | |
| committer | |
| log | 5382c84ed22519a600642f5cf3b1d8c2769050d6 |
| tree | 2d11defb265047506e0c8a941d81a982809941fa |
| parent | 1bb9a9dafa1f7961e6bf7cf66bcf1a425eedc07e |
| signature | Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM |
Resolves: https://codeberg.org/ziglang/zig/issues/373254 files changed, 23 insertions(+), 4 deletions(-)
lib/std/zig/AstGen.zig+6| ... | ... | @@ -7731,6 +7731,12 @@ fn switchExpr( |
| 7731 | 7731 | const tag_name = try astgen.identAsString(tag_token); |
| 7732 | 7732 | try astgen.detectLocalShadowing(payload_sub_scope, tag_name, tag_token, tag_slice, .@"switch tag capture"); |
| 7733 | 7733 | |
| 7734 | if (case_node.toOptional() == else_case_node and case.inline_token == null) { | |
| 7735 | return astgen.failTokNotes(tag_token, "cannot capture tag of 'else' prong", .{}, &.{ | |
| 7736 | try astgen.errNoteTok(ident, "first capture in 'else' prong always captures 'switch' operand", .{}), | |
| 7737 | }); | |
| 7738 | } | |
| 7739 | ||
| 7734 | 7740 | assert(any_has_tag_capture); |
| 7735 | 7741 | has_tag_capture = true; |
| 7736 | 7742 |
test/behavior/switch.zig+1-2| ... | ... | @@ -1186,8 +1186,7 @@ test "switch with tag capture" { |
| 1186 | 1186 | _ = &tag; |
| 1187 | 1187 | comptime unreachable; |
| 1188 | 1188 | }, |
| 1189 | else => |un, tag| { | |
| 1190 | try expect(tag == .c); | |
| 1189 | else => |un| { | |
| 1191 | 1190 | try expect(un == .c); |
| 1192 | 1191 | try expect(un.c == 0xFF); |
| 1193 | 1192 | }, |
test/behavior/switch_loop.zig+1-2| ... | ... | @@ -416,8 +416,7 @@ test "switch loop with tag capture" { |
| 416 | 416 | _ = &tag; |
| 417 | 417 | return error.AnalyzedNoreturnProng; |
| 418 | 418 | }, |
| 419 | else => |un, tag| { | |
| 420 | try expect(tag == .c); | |
| 419 | else => |un| { | |
| 421 | 420 | try expect(un == .c); |
| 422 | 421 | if (un.c == 0xFF) continue :label .a; |
| 423 | 422 | if (un.c == 0x00) break :label false; |
test/cases/compile_errors/tag_capture_in_else_prong.zig created+15| ... | ... | @@ -0,0 +1,15 @@ |
| 1 | const Foo = union(enum) { a: u32, b: u32 }; | |
| 2 | export fn entry() void { | |
| 3 | const foo: Foo = .{ .a = 123 }; | |
| 4 | switch (foo) { | |
| 5 | else => |payload, tag| { | |
| 6 | _ = payload; | |
| 7 | _ = tag; | |
| 8 | }, | |
| 9 | } | |
| 10 | } | |
| 11 | ||
| 12 | // error | |
| 13 | // | |
| 14 | // :5:27: error: cannot capture tag of 'else' prong | |
| 15 | // :5:18: note: first capture in 'else' prong always captures 'switch' operand |