aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/switch.zig
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2023-10-28 01:22:30 +0100
committerMatthew Lugg <mlugg@mlugg.co.uk>2023-10-28 06:30:28 +0100
commitc1c9bc0c41eb8956b2fe0b21e2443c6504e73234 (patch)
treeaa7e221d54438cb290c902f272bb4fc6d660478f /test/behavior/switch.zig
parent5257643d3ddd35b0fb40b82988a9ccf9f859a779 (diff)
downloadzig-c1c9bc0c41eb8956b2fe0b21e2443c6504e73234.tar.gz
zig-c1c9bc0c41eb8956b2fe0b21e2443c6504e73234.zip
Sema: do not assume switch item indices align with union field indices
Resolves: #17754
Diffstat (limited to 'test/behavior/switch.zig')
-rw-r--r--test/behavior/switch.zig23
1 files changed, 23 insertions, 0 deletions
diff --git a/test/behavior/switch.zig b/test/behavior/switch.zig
index d7d2f8f7b6..37cf8b7c6f 100644
--- a/test/behavior/switch.zig
+++ b/test/behavior/switch.zig
@@ -800,3 +800,26 @@ test "nested break ignores switch conditions and breaks instead" {
// Originally reported at https://github.com/ziglang/zig/issues/10196
try expect(0x01 == try S.register_to_address("a0"));
}
+
+test "peer type resolution on switch captures ignores unused payload bits" {
+ if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
+
+ const Foo = union(enum) {
+ a: u32,
+ b: u64,
+ };
+
+ var val: Foo = undefined;
+ @memset(std.mem.asBytes(&val), 0xFF);
+
+ // This is runtime-known so the following store isn't comptime-known.
+ var rt: u32 = 123;
+ val = .{ .a = rt }; // will not necessarily zero remaning payload memory
+
+ // Fields intentionally backwards here
+ const x = switch (val) {
+ .b, .a => |x| x,
+ };
+
+ try expect(x == 123);
+}