authorgravatar for 51252236+xdBronch@users.noreply.github.comxdBronch <51252236+xdBronch@users.noreply.github.com> 2024-09-12 11:25:40-04:00
committergravatar for 51252236+xdBronch@users.noreply.github.comxdBronch <51252236+xdBronch@users.noreply.github.com> 2024-09-12 20:29:10-04:00
log0329b8387ce53574c42565322c191e7cc30eb3f7
tree418fa148b963cce215591acaadf34af0a45b3f3b
parent55250a9370ae247d52d9d78033880451cb1e9add

make decl literals work with single item pointers


2 files changed, 50 insertions(+), 0 deletions(-)

src/Sema.zig+1
......@@ -8996,6 +8996,7 @@ fn zirDeclLiteral(sema: *Sema, block: *Block, inst: Zir.Inst.Index, do_coerce: b
89968996 while (true) switch (ty.zigTypeTag(zcu)) {
89978997 .error_union => ty = ty.errorUnionPayload(zcu),
89988998 .optional => ty = ty.optionalChild(zcu),
8999 .pointer => ty = if (ty.isSinglePointer(zcu)) ty.childType(zcu) else break,
89999000 .enum_literal, .error_set => {
90009001 // Treat this as a normal enum literal.
90019002 break :res Air.internedToRef(try pt.intern(.{ .enum_literal = name }));
test/behavior/decl_literals.zig+49
......@@ -12,6 +12,55 @@ test "decl literal" {
1212 try expect(val.x == 123);
1313}
1414
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_riscv64 or
37 builtin.zig_backend == .stage2_sparc64 or
38 builtin.zig_backend == .stage2_arm or
39 builtin.zig_backend == .stage2_aarch64 or
40 builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
41 const S = struct {
42 x: u32,
43 fn init() ?@This() {
44 return .{ .x = 123 };
45 }
46 };
47
48 const val: ?S = .init();
49 try expect(val.?.x == 123);
50}
51
52test "call decl literal with pointer" {
53 const S = struct {
54 x: u32,
55 fn init() *const @This() {
56 return &.{ .x = 123 };
57 }
58 };
59
60 const val: *const S = .init();
61 try expect(val.x == 123);
62}
63
1564test "call decl literal" {
1665 const S = struct {
1766 x: u32,