Commit 0329b8387c
Changed files (2)
src
test
behavior
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 }));
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,