From 0329b8387ce53574c42565322c191e7cc30eb3f7 Mon Sep 17 00:00:00 2001 From: xdBronch <51252236+xdBronch@users.noreply.github.com> Date: Thu, 12 Sep 2024 11:25:40 -0400 Subject: [PATCH] make decl literals work with single item pointers --- src/Sema.zig | 1 + test/behavior/decl_literals.zig | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/Sema.zig b/src/Sema.zig index 0a609ff0f4..61501fa455 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -8996,6 +8996,7 @@ fn zirDeclLiteral(sema: *Sema, block: *Block, inst: Zir.Inst.Index, do_coerce: b while (true) switch (ty.zigTypeTag(zcu)) { .error_union => ty = ty.errorUnionPayload(zcu), .optional => ty = ty.optionalChild(zcu), + .pointer => ty = if (ty.isSinglePointer(zcu)) ty.childType(zcu) else break, .enum_literal, .error_set => { // Treat this as a normal enum literal. break :res Air.internedToRef(try pt.intern(.{ .enum_literal = name })); diff --git a/test/behavior/decl_literals.zig b/test/behavior/decl_literals.zig index 7956689122..b5961c2c13 100644 --- a/test/behavior/decl_literals.zig +++ b/test/behavior/decl_literals.zig @@ -12,6 +12,55 @@ test "decl literal" { try expect(val.x == 123); } +test "decl literal with optional" { + const S = struct { + x: u32, + const foo: ?@This() = .{ .x = 123 }; + }; + + const val: ?S = .foo; + try expect(val.?.x == 123); +} + +test "decl literal with pointer" { + const S = struct { + x: u32, + const foo: *const @This() = &.{ .x = 123 }; + }; + + const val: *const S = .foo; + try expect(val.x == 123); +} + +test "call decl literal with optional" { + if (builtin.zig_backend == .stage2_riscv64 or + builtin.zig_backend == .stage2_sparc64 or + builtin.zig_backend == .stage2_arm or + builtin.zig_backend == .stage2_aarch64 or + builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO + const S = struct { + x: u32, + fn init() ?@This() { + return .{ .x = 123 }; + } + }; + + const val: ?S = .init(); + try expect(val.?.x == 123); +} + +test "call decl literal with pointer" { + const S = struct { + x: u32, + fn init() *const @This() { + return &.{ .x = 123 }; + } + }; + + const val: *const S = .init(); + try expect(val.x == 123); +} + test "call decl literal" { const S = struct { x: u32,