Files
zig/test/cases/compile_errors/switch_packed_exhaustion.zig
Justus Klausecker 28886ca9ec Sema: implement switch for packed structs/unions
Since packed containers are now represented by `bitpack` and can't include
pointers anymore this has become a very easy change to make. This commit
largely just reuses the logic already in place for integers.

Also fixes a small bug where captures-by-ref of errors wouldn't cause a
compile error for regular switch statements. There was already an astgen
error in place for error handling switch statements (`switch_block_err_union`)
capturing their error by reference.
2026-03-11 21:04:04 +01:00

42 lines
1.0 KiB
Zig

const S = packed struct(u2) {
a: u2,
};
export fn entry1(x: u8) void {
const s: S = .{ .a = @intCast(x) };
switch (s) {
.{ .a = 0b00 }, .{ .a = 0b01 }, .{ .a = 0b10 }, .{ .a = 0b11 } => {},
else => {},
}
}
export fn entry2(x: u8) void {
const s: S = .{ .a = @intCast(x) };
switch (s) {
.{ .a = 0b00 }, .{ .a = 0b01 }, .{ .a = 0b11 } => {},
}
}
const U = packed union(u2) {
a: u2,
b: i2,
};
export fn entry3(x: u8) void {
const u: U = .{ .a = @intCast(x) };
switch (u) {
.{ .a = 0b00 }, .{ .a = 0b01 }, .{ .a = 0b10 }, .{ .a = 0b11 } => {},
else => {},
}
}
export fn entry4(x: u8) void {
const u: U = .{ .a = @intCast(x) };
switch (u) {
.{ .a = 0b00 }, .{ .a = 0b01 }, .{ .a = 0b11 } => {},
}
}
// error
//
// :8:14: error: unreachable else prong; all cases already handled
// :13:5: error: switch must handle all possibilities
// :26:14: error: unreachable else prong; all cases already handled
// :31:5: error: switch must handle all possibilities