aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-02-21 14:18:17 -0500
committerGitHub <noreply@github.com>2022-02-21 14:18:17 -0500
commit74303a3d9591f188fb4dda96bf689c00ebbd24ca (patch)
tree936ea6f3afe985b9cc3110eca1d4b1750a4fca2f /test
parent628e9e6d040979bd0a2cba05e854014dee5a7d55 (diff)
parenta5ac06268972bd7279a1bb928a40d70cc7d515ed (diff)
downloadzig-74303a3d9591f188fb4dda96bf689c00ebbd24ca.tar.gz
zig-74303a3d9591f188fb4dda96bf689c00ebbd24ca.zip
Merge pull request #10925 from Vexu/stage2
stage2: support anon init through error unions and optionals
Diffstat (limited to 'test')
-rw-r--r--test/behavior.zig2
-rw-r--r--test/behavior/struct.zig61
2 files changed, 62 insertions, 1 deletions
diff --git a/test/behavior.zig b/test/behavior.zig
index b94b0b631a..1cd2833c4d 100644
--- a/test/behavior.zig
+++ b/test/behavior.zig
@@ -119,6 +119,7 @@ test {
_ = @import("behavior/sizeof_and_typeof.zig");
_ = @import("behavior/switch.zig");
_ = @import("behavior/widening.zig");
+ _ = @import("behavior/bugs/1442.zig");
if (builtin.zig_backend == .stage1) {
// Tests that only pass for the stage1 backend.
@@ -135,7 +136,6 @@ test {
_ = @import("behavior/bugs/920.zig");
_ = @import("behavior/bugs/1120.zig");
_ = @import("behavior/bugs/1421.zig");
- _ = @import("behavior/bugs/1442.zig");
_ = @import("behavior/bugs/1607.zig");
_ = @import("behavior/bugs/1851.zig");
_ = @import("behavior/bugs/2114.zig");
diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig
index 3e92b4374b..d059cccb60 100644
--- a/test/behavior/struct.zig
+++ b/test/behavior/struct.zig
@@ -1199,3 +1199,64 @@ test "for loop over pointers to struct, getting field from struct pointer" {
};
try S.doTheTest();
}
+
+test "anon init through error unions and optionals" {
+ if (builtin.zig_backend == .stage1) return error.SkipZigTest;
+ if (builtin.zig_backend != .stage2_llvm) return error.SkipZigTest; // TODO
+
+ const S = struct {
+ a: u32,
+
+ fn foo() anyerror!?anyerror!@This() {
+ return .{ .a = 1 };
+ }
+ fn bar() ?anyerror![2]u8 {
+ return .{ 1, 2 };
+ }
+
+ fn doTheTest() !void {
+ var a = try (try foo()).?;
+ var b = try bar().?;
+ try expect(a.a + b[1] == 3);
+ }
+ };
+
+ try S.doTheTest();
+ comptime try S.doTheTest();
+}
+
+test "anon init through optional" {
+ if (builtin.zig_backend == .stage1) return error.SkipZigTest;
+ if (builtin.zig_backend != .stage2_llvm) return error.SkipZigTest; // TODO
+
+ const S = struct {
+ a: u32,
+
+ fn doTheTest() !void {
+ var s: ?@This() = null;
+ s = .{ .a = 1 };
+ try expect(s.?.a == 1);
+ }
+ };
+
+ try S.doTheTest();
+ comptime try S.doTheTest();
+}
+
+test "anon init through error union" {
+ if (builtin.zig_backend == .stage1) return error.SkipZigTest;
+ if (builtin.zig_backend != .stage2_llvm) return error.SkipZigTest; // TODO
+
+ const S = struct {
+ a: u32,
+
+ fn doTheTest() !void {
+ var s: anyerror!@This() = error.Foo;
+ s = .{ .a = 1 };
+ try expect((try s).a == 1);
+ }
+ };
+
+ try S.doTheTest();
+ comptime try S.doTheTest();
+}