authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-14 06:24:24+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-09-14 06:24:24+01:00
log4d81e8ee915c3e012131cf90ed87cc8c6a01a934
treefbd395f71db31e55c5fa4094d095b919b10c053e
parent8ddce90e62e52244b7f6d1104bb39a55350f0a83
parentbc161430b0006386ae19c51cfc574b5d8f8fef6e
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21390 from xdBronch/push-tvovpsxztrqn

make decl literals work with single item pointers

7 files changed, 80 insertions(+), 7 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 }));
src/arch/riscv64/CodeGen.zig+31-1
......@@ -3364,8 +3364,38 @@ fn airOptionalPayloadPtr(func: *Func, inst: Air.Inst.Index) !void {
33643364}
33653365
33663366fn airOptionalPayloadPtrSet(func: *Func, inst: Air.Inst.Index) !void {
3367 const zcu = func.pt.zcu;
3368
33673369 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3368 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement .optional_payload_ptr_set for {}", .{func.target.cpu.arch});
3370 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
3371 const dst_ty = func.typeOfIndex(inst);
3372 const src_ty = func.typeOf(ty_op.operand);
3373 const opt_ty = src_ty.childType(zcu);
3374 const src_mcv = try func.resolveInst(ty_op.operand);
3375
3376 if (opt_ty.optionalReprIsPayload(zcu)) {
3377 break :result if (func.reuseOperand(inst, ty_op.operand, 0, src_mcv))
3378 src_mcv
3379 else
3380 try func.copyToNewRegister(inst, src_mcv);
3381 }
3382
3383 const dst_mcv: MCValue = if (src_mcv.isRegister() and
3384 func.reuseOperand(inst, ty_op.operand, 0, src_mcv))
3385 src_mcv
3386 else
3387 try func.copyToNewRegister(inst, src_mcv);
3388
3389 const pl_ty = dst_ty.childType(zcu);
3390 const pl_abi_size: i32 = @intCast(pl_ty.abiSize(zcu));
3391 try func.genSetMem(
3392 .{ .reg = dst_mcv.getReg().? },
3393 pl_abi_size,
3394 Type.bool,
3395 .{ .immediate = 1 },
3396 );
3397 break :result dst_mcv;
3398 };
33693399 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
33703400}
33713401
test/behavior/cast.zig-1
......@@ -1875,7 +1875,6 @@ test "peer type resolution: vector and optional vector" {
18751875 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
18761876 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
18771877 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
1878 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
18791878
18801879 var a: ?@Vector(3, u32) = .{ 0, 1, 2 };
18811880 var b: @Vector(3, u32) = .{ 3, 4, 5 };
test/behavior/decl_literals.zig+48
......@@ -12,6 +12,54 @@ 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_sparc64 or
37 builtin.zig_backend == .stage2_arm or
38 builtin.zig_backend == .stage2_aarch64 or
39 builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
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
1563test "call decl literal" {
1664 const S = struct {
1765 x: u32,
test/behavior/optional.zig-1
......@@ -320,7 +320,6 @@ test "coerce an anon struct literal to optional struct" {
320320 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
321321 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
322322 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
323 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
324323
325324 const S = struct {
326325 const Struct = struct {
test/behavior/struct.zig-3
......@@ -1165,7 +1165,6 @@ test "anon init through error unions and optionals" {
11651165 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
11661166 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
11671167 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1168 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
11691168
11701169 const S = struct {
11711170 a: u32,
......@@ -1193,7 +1192,6 @@ test "anon init through optional" {
11931192 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
11941193 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
11951194 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1196 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
11971195
11981196 const S = struct {
11991197 a: u32,
......@@ -1503,7 +1501,6 @@ test "no dependency loop on pointer to optional struct" {
15031501 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
15041502 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
15051503 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1506 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
15071504
15081505 const S = struct {
15091506 const A = struct { b: B };
test/behavior/union.zig-1
......@@ -1268,7 +1268,6 @@ test "extern union most-aligned field is smaller" {
12681268 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
12691269 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
12701270 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1271 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
12721271
12731272 const U = extern union {
12741273 in6: extern struct {