master
1const builtin = @import("builtin");
2const std = @import("std");
3const expect = std.testing.expect;
4
5test "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
15test "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
25test "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
35test "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
51test "call decl literal with pointer" {
52 const S = struct {
53 x: u32,
54 fn init() *const @This() {
55 return &.{ .x = 123 };
56 }
57 };
58
59 const val: *const S = .init();
60 try expect(val.x == 123);
61}
62
63test "call decl literal" {
64 const S = struct {
65 x: u32,
66 fn init() @This() {
67 return .{ .x = 123 };
68 }
69 };
70
71 const val: S = .init();
72 try expect(val.x == 123);
73}
74
75test "call decl literal with error union" {
76 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; // TODO
77
78 const S = struct {
79 x: u32,
80 fn init(err: bool) !@This() {
81 if (err) return error.Bad;
82 return .{ .x = 123 };
83 }
84 };
85
86 const val: S = try .init(false);
87 try expect(val.x == 123);
88}