diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-02-21 14:18:17 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-21 14:18:17 -0500 |
| commit | 74303a3d9591f188fb4dda96bf689c00ebbd24ca (patch) | |
| tree | 936ea6f3afe985b9cc3110eca1d4b1750a4fca2f /test/behavior | |
| parent | 628e9e6d040979bd0a2cba05e854014dee5a7d55 (diff) | |
| parent | a5ac06268972bd7279a1bb928a40d70cc7d515ed (diff) | |
| download | zig-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/behavior')
| -rw-r--r-- | test/behavior/struct.zig | 61 |
1 files changed, 61 insertions, 0 deletions
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(); +} |
