| 1 | const builtin = @import("builtin"); |
| 2 | const std = @import("std"); |
| 3 | const expect = std.testing.expect; |
| 4 | |
| 5 | test "decl literal" { |
| 6 | const S = struct { |
| 7 | x: u32, |
| 8 | const foo: @This() = .{ .x = 123 }; |
| 9 | }; |
| 10 | |
| 11 | const val: S = .foo; |
| 12 | try expect(val.x == 123); |
| 13 | } |
| 14 | |
| 15 | test "decl literal with optional" { |
| 16 | const S = struct { |
| 17 | x: u32, |
| 18 | const foo: ?@This() = .{ .x = 123 }; |
| 19 | }; |
| 20 | |
| 21 | const val: ?S = .foo; |
| 22 | try expect(val.?.x == 123); |
| 23 | } |
| 24 | |
| 25 | test "decl literal with pointer" { |
| 26 | const S = struct { |
| 27 | x: u32, |
| 28 | const foo: *const @This() = &.{ .x = 123 }; |
| 29 | }; |
| 30 | |
| 31 | const val: *const S = .foo; |
| 32 | try expect(val.x == 123); |
| 33 | } |
| 34 | |
| 35 | test "call decl literal with optional" { |
| 36 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; |
| 37 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 38 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 39 | |
| 40 | const S = struct { |
| 41 | x: u32, |
| 42 | fn init() ?@This() { |
| 43 | return .{ .x = 123 }; |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | const val: ?S = .init(); |
| 48 | try expect(val.?.x == 123); |
| 49 | } |
| 50 | |
| 51 | test "call decl literal with pointer" { |
| 52 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 53 | |
| 54 | const S = struct { |
| 55 | x: u32, |
| 56 | fn init() *const @This() { |
| 57 | return &.{ .x = 123 }; |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | const val: *const S = .init(); |
| 62 | try expect(val.x == 123); |
| 63 | } |
| 64 | |
| 65 | test "call decl literal" { |
| 66 | const S = struct { |
| 67 | x: u32, |
| 68 | fn init() @This() { |
| 69 | return .{ .x = 123 }; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | const val: S = .init(); |
| 74 | try expect(val.x == 123); |
| 75 | } |
| 76 | |
| 77 | test "call decl literal with error union" { |
| 78 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; // TODO |
| 79 | |
| 80 | const S = struct { |
| 81 | x: u32, |
| 82 | fn init(err: bool) !@This() { |
| 83 | if (err) return error.Bad; |
| 84 | return .{ .x = 123 }; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | const val: S = try .init(false); |
| 89 | try expect(val.x == 123); |
| 90 | } |