aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/inline_switch.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-10-03 23:43:09 -0400
committerGitHub <noreply@github.com>2022-10-03 23:43:09 -0400
commitff534d22676b8a934acf1931f91d70c554a4bdca (patch)
treed04b360f7831f6428d13a85d9d9c0d7c04fc1079 /test/behavior/inline_switch.zig
parent9d5462dcb5b4b4601bdf2e628b9d80fb74000cb2 (diff)
parent17eea918aee98ca29c3762a7ecd568d2f14f66ef (diff)
downloadzig-ff534d22676b8a934acf1931f91d70c554a4bdca.tar.gz
zig-ff534d22676b8a934acf1931f91d70c554a4bdca.zip
Merge pull request #12979 from Vexu/inline-switch
Implement inline switch cases
Diffstat (limited to 'test/behavior/inline_switch.zig')
-rw-r--r--test/behavior/inline_switch.zig131
1 files changed, 131 insertions, 0 deletions
diff --git a/test/behavior/inline_switch.zig b/test/behavior/inline_switch.zig
new file mode 100644
index 0000000000..ecc7bba280
--- /dev/null
+++ b/test/behavior/inline_switch.zig
@@ -0,0 +1,131 @@
+const std = @import("std");
+const expect = std.testing.expect;
+const builtin = @import("builtin");
+
+test "inline scalar prongs" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+
+ var x: usize = 0;
+ switch (x) {
+ 10 => |*item| try expect(@TypeOf(item) == *usize),
+ inline 11 => |*item| {
+ try expect(@TypeOf(item) == *const usize);
+ try expect(item.* == 11);
+ },
+ else => {},
+ }
+}
+
+test "inline prong ranges" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+
+ var x: usize = 0;
+ switch (x) {
+ inline 0...20, 24 => |item| {
+ if (item > 25) @compileError("bad");
+ },
+ else => {},
+ }
+}
+
+const E = enum { a, b, c, d };
+test "inline switch enums" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+
+ var x: E = .a;
+ switch (x) {
+ inline .a, .b => |aorb| if (aorb != .a and aorb != .b) @compileError("bad"),
+ inline .c, .d => |cord| if (cord != .c and cord != .d) @compileError("bad"),
+ }
+}
+
+const U = union(E) { a: void, b: u2, c: u3, d: u4 };
+test "inline switch unions" {
+ 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_x86_64) return error.SkipZigTest; // TODO
+
+ var x: U = .a;
+ switch (x) {
+ inline .a, .b => |aorb, tag| {
+ if (tag == .a) {
+ try expect(@TypeOf(aorb) == void);
+ } else {
+ try expect(tag == .b);
+ try expect(@TypeOf(aorb) == u2);
+ }
+ },
+ inline .c, .d => |cord, tag| {
+ if (tag == .c) {
+ try expect(@TypeOf(cord) == u3);
+ } else {
+ try expect(tag == .d);
+ try expect(@TypeOf(cord) == u4);
+ }
+ },
+ }
+}
+
+test "inline else bool" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+
+ var a = true;
+ switch (a) {
+ true => {},
+ inline else => |val| if (val != false) @compileError("bad"),
+ }
+}
+
+test "inline else error" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+
+ const Err = error{ a, b, c };
+ var a = Err.a;
+ switch (a) {
+ error.a => {},
+ inline else => |val| comptime if (val == error.a) @compileError("bad"),
+ }
+}
+
+test "inline else enum" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
+
+ const E2 = enum(u8) { a = 2, b = 3, c = 4, d = 5 };
+ var a: E2 = .a;
+ switch (a) {
+ .a, .b => {},
+ inline else => |val| comptime if (@enumToInt(val) < 4) @compileError("bad"),
+ }
+}
+
+test "inline else int with gaps" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+
+ var a: u8 = 0;
+ switch (a) {
+ 1...125, 128...254 => {},
+ inline else => |val| {
+ if (val != 0 and
+ val != 126 and
+ val != 127 and
+ val != 255)
+ @compileError("bad");
+ },
+ }
+}
+
+test "inline else int all values" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+
+ var a: u2 = 0;
+ switch (a) {
+ inline else => |val| {
+ if (val != 0 and
+ val != 1 and
+ val != 2 and
+ val != 3)
+ @compileError("bad");
+ },
+ }
+}