Files
zig/test/cases/compile_errors/switching_with_non-exhaustive_enums.zig
Justus Klausecker ba549a7d67 Add support for both '_' and 'else' prongs at the same time in switch statements
If both are used, 'else' handles named members and '_' handles
unnamed members. In this case the 'else' prong will be unrolled
to an explicit case containing all remaining named values.
2025-08-07 13:58:47 +02:00

44 lines
1.0 KiB
Zig

const E = enum(u8) {
a,
b,
_,
};
const U = union(E) {
a: i32,
b: u32,
};
pub export fn entry1() void {
const e: E = .b;
switch (e) { // error: switch not handling the tag `b`
.a => {},
_ => {},
}
}
pub export fn entry2() void {
const e: E = .b;
switch (e) { // error: switch on non-exhaustive enum must include `else` or `_` prong
.a => {},
.b => {},
}
}
pub export fn entry3() void {
const u = U{ .a = 2 };
switch (u) { // error: `_` prong not allowed when switching on tagged union
.a => {},
.b => {},
_ => {},
}
}
// error
// backend=stage2
// target=native
//
// :12:5: error: switch must handle all possibilities
// :3:5: note: unhandled enumeration value: 'b'
// :1:11: note: enum 'tmp.E' declared here
// :19:5: error: switch on non-exhaustive enum must include 'else' or '_' prong or both
// :26:5: error: '_' prong only allowed when switching on non-exhaustive enums
// :29:9: note: '_' prong here
// :26:5: note: consider using 'else'