aboutsummaryrefslogtreecommitdiff
path: root/test/behavior
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-10-25 15:06:47 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-10-25 15:11:21 -0700
commit8509e7111d80a07e778aa2a57d58d2bea6945014 (patch)
treee7eedaf980ada713a6c3d2da3e6153751d03bed6 /test/behavior
parenta132190cad80669306705b72276e9641401426fb (diff)
downloadzig-8509e7111d80a07e778aa2a57d58d2bea6945014.tar.gz
zig-8509e7111d80a07e778aa2a57d58d2bea6945014.zip
stage2: fix switch on tagged union capture-by-pointer
* AstGen: always use `typeof` and never `typeof_elem` on the `switch_cond`/`switch_cond_ref` instruction because both variants return a value and not a pointer. - Delete the `typeof_elem` ZIR instruction since it is no longer needed. * Sema: validateUnionInit now recognizes a comptime mutable value and no longer emits a compile error saying "cannot evaluate constant expression" - Still to-do is detecting comptime union values in a function that is not being executed at compile-time. - This is still to-do for structs too. * Sema: when emitting a call AIR instruction, call resolveTypeLayout on all the parameter types as well as the return type. * `Type.structFieldOffset` now works for unions in addition to structs.
Diffstat (limited to 'test/behavior')
-rw-r--r--test/behavior/switch.zig43
-rw-r--r--test/behavior/switch_stage1.zig42
2 files changed, 43 insertions, 42 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,
+ }
+}
diff --git a/test/behavior/switch_stage1.zig b/test/behavior/switch_stage1.zig
index 5847acb7d2..c42a9b7894 100644
--- a/test/behavior/switch_stage1.zig
+++ b/test/behavior/switch_stage1.zig
@@ -3,48 +3,6 @@ const expect = std.testing.expect;
const expectError = std.testing.expectError;
const expectEqual = std.testing.expectEqual;
-test "switch prong with variable" {
- try switchProngWithVarFn(SwitchProngWithVarEnum{ .One = 13 });
- try switchProngWithVarFn(SwitchProngWithVarEnum{ .Two = 13.0 });
- try switchProngWithVarFn(SwitchProngWithVarEnum{ .Meh = {} });
-}
-const SwitchProngWithVarEnum = union(enum) {
- One: i32,
- Two: f32,
- Meh: void,
-};
-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,
- }
-}
-
test "switch handles all cases of number" {
try testSwitchHandleAllCases();
comptime try testSwitchHandleAllCases();