diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-02-19 11:35:49 +0200 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2022-02-19 20:21:19 +0200 |
| commit | e02749224357a33cffcaf9970d14c199adc3d892 (patch) | |
| tree | 90970730e6d00583eeb8789e035d1f1dd8edc466 /test/behavior/struct.zig | |
| parent | 2f0204aca303daf899a97c740719a62398adc206 (diff) | |
| download | zig-e02749224357a33cffcaf9970d14c199adc3d892.tar.gz zig-e02749224357a33cffcaf9970d14c199adc3d892.zip | |
stage2: support anon init through error unions and optionals
Diffstat (limited to 'test/behavior/struct.zig')
| -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..7428aed62a 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 (true) return error.SkipZigTest; // TODO + + const S = struct { + a: u32, + + fn foo() anyerror!?anyerror!@This() { + return @This(){ .a = 1 }; + } + fn bar() ?anyerror![2]u8 { + return [2]u8{ 1, 2 }; + } + + fn doTheTest() !void { + var a = ((foo() catch unreachable).?) catch unreachable; + var b = (bar().?) catch unreachable; + 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; + // not sure why this is needed, we only do the test at comptime + if (builtin.zig_backend != .stage2_llvm) return error.SkipZigTest; + + const S = struct { + a: u32, + + fn doTheTest() !void { + var s: ?@This() = null; + s = .{ .a = 1 }; + try expect(s.?.a == 1); + } + }; + // try S.doTheTest(); // TODO + comptime try S.doTheTest(); +} + +test "anon init through error union" { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + // not sure why this is needed, we only do the test at comptime + if (builtin.zig_backend != .stage2_llvm) return error.SkipZigTest; + + 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(); // TODO + comptime try S.doTheTest(); +} |
