make decl literals work with single item pointers

This commit is contained in:
xdBronch 2024-09-12 11:25:40 -04:00
parent 55250a9370
commit 0329b8387c
2 changed files with 50 additions and 0 deletions

View File

@ -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 }));

View File

@ -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,