aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/switch_loop.zig
diff options
context:
space:
mode:
authorJustus Klausecker <justus@klausecker.de>2025-07-10 01:58:02 +0200
committerJustus Klausecker <justus@klausecker.de>2025-08-07 13:58:47 +0200
commitba549a7d67d0268cdbd4b23a5b7371de4f2d8d33 (patch)
tree2ee5ed3a9546923fa41abeac193634f88f944d82 /test/behavior/switch_loop.zig
parent1d9b1c021273037efea6623e669800ad538c8075 (diff)
downloadzig-ba549a7d67d0268cdbd4b23a5b7371de4f2d8d33.tar.gz
zig-ba549a7d67d0268cdbd4b23a5b7371de4f2d8d33.zip
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.
Diffstat (limited to 'test/behavior/switch_loop.zig')
-rw-r--r--test/behavior/switch_loop.zig24
1 files changed, 24 insertions, 0 deletions
diff --git a/test/behavior/switch_loop.zig b/test/behavior/switch_loop.zig
index d2e967e4c7..1d82957d39 100644
--- a/test/behavior/switch_loop.zig
+++ b/test/behavior/switch_loop.zig
@@ -249,3 +249,27 @@ test "switch loop on larger than pointer integer" {
}
try expect(entry == 3);
}
+
+test "switch loop on non-exhaustive enum" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; // TODO
+
+ const S = struct {
+ const E = enum(u8) { a, b, c, _ };
+
+ fn doTheTest() !void {
+ var start: E = undefined;
+ start = .a;
+ const result: u32 = s: switch (start) {
+ .a => continue :s .c,
+ else => continue :s @enumFromInt(123),
+ .b, _ => |x| break :s @intFromEnum(x),
+ };
+ try expect(result == 123);
+ }
+ };
+ try S.doTheTest();
+ try comptime S.doTheTest();
+}