diff options
Diffstat (limited to 'test/behavior/switch.zig')
| -rw-r--r-- | test/behavior/switch.zig | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/test/behavior/switch.zig b/test/behavior/switch.zig index 67cda60958..d5e4a40500 100644 --- a/test/behavior/switch.zig +++ b/test/behavior/switch.zig @@ -219,3 +219,46 @@ test "switch on global mutable var isn't constant-folded" { poll(); } } + +const SwitchProngWithVarEnum = union(enum) { + One: i32, + Two: f32, + Meh: void, +}; + +test "switch prong with variable" { + try switchProngWithVarFn(SwitchProngWithVarEnum{ .One = 13 }); + try switchProngWithVarFn(SwitchProngWithVarEnum{ .Two = 13.0 }); + try switchProngWithVarFn(SwitchProngWithVarEnum{ .Meh = {} }); +} +fn switchProngWithVarFn(a: SwitchProngWithVarEnum) !void { + switch (a) { + SwitchProngWithVarEnum.One => |x| { + try expect(x == 13); + }, + SwitchProngWithVarEnum.Two => |x| { + try expect(x == 13.0); + }, + SwitchProngWithVarEnum.Meh => |x| { + const v: void = x; + _ = v; + }, + } +} + +test "switch on enum using pointer capture" { + try testSwitchEnumPtrCapture(); + comptime try testSwitchEnumPtrCapture(); +} + +fn testSwitchEnumPtrCapture() !void { + var value = SwitchProngWithVarEnum{ .One = 1234 }; + switch (value) { + SwitchProngWithVarEnum.One => |*x| x.* += 1, + else => unreachable, + } + switch (value) { + SwitchProngWithVarEnum.One => |x| try expect(x == 1235), + else => unreachable, + } +} |
