diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-06-13 08:45:12 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-13 08:45:12 -0700 |
| commit | df6319418a08b611bae23307ace6688f95bedea2 (patch) | |
| tree | 6cdf873c6800aebccdd8c03a443f9594acb84729 /test/behavior | |
| parent | 387f9568ad0dabd426d382efb45b9c52a4ccc5bb (diff) | |
| parent | 42dc7539c5b0a39e9b64c5ad92757945b0ca05ad (diff) | |
| download | zig-df6319418a08b611bae23307ace6688f95bedea2.tar.gz zig-df6319418a08b611bae23307ace6688f95bedea2.zip | |
Merge pull request #15880 from mlugg/feat/better-switch-zir-2
Simplify and compact switch ZIR, and resolve union payload captures with PTR
Diffstat (limited to 'test/behavior')
| -rw-r--r-- | test/behavior/switch.zig | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/test/behavior/switch.zig b/test/behavior/switch.zig index 3f6cd37298..72a36c9883 100644 --- a/test/behavior/switch.zig +++ b/test/behavior/switch.zig @@ -1,5 +1,6 @@ const builtin = @import("builtin"); const std = @import("std"); +const assert = std.debug.assert; const expect = std.testing.expect; const expectError = std.testing.expectError; const expectEqual = std.testing.expectEqual; @@ -717,3 +718,70 @@ test "comptime inline switch" { try expectEqual(u32, value); } + +test "switch capture peer type resolution" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const U = union(enum) { + a: u32, + b: u64, + fn innerVal(u: @This()) u64 { + switch (u) { + .a, .b => |x| return x, + } + } + }; + + try expectEqual(@as(u64, 100), U.innerVal(.{ .a = 100 })); + try expectEqual(@as(u64, 200), U.innerVal(.{ .b = 200 })); +} + +test "switch capture peer type resolution for in-memory coercible payloads" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const T1 = c_int; + const T2 = @Type(@typeInfo(T1)); + + comptime assert(T1 != T2); + + const U = union(enum) { + a: T1, + b: T2, + fn innerVal(u: @This()) c_int { + switch (u) { + .a, .b => |x| return x, + } + } + }; + + try expectEqual(@as(c_int, 100), U.innerVal(.{ .a = 100 })); + try expectEqual(@as(c_int, 200), U.innerVal(.{ .b = 200 })); +} + +test "switch pointer capture peer type resolution" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const T1 = c_int; + const T2 = @Type(@typeInfo(T1)); + + comptime assert(T1 != T2); + + const U = union(enum) { + a: T1, + b: T2, + fn innerVal(u: *@This()) *c_int { + switch (u.*) { + .a, .b => |*ptr| return ptr, + } + } + }; + + var ua: U = .{ .a = 100 }; + var ub: U = .{ .b = 200 }; + + ua.innerVal().* = 111; + ub.innerVal().* = 222; + + try expectEqual(U{ .a = 111 }, ua); + try expectEqual(U{ .b = 222 }, ub); +} |
