From 15cc4514e0566430ed1d95e85aaa462642b70364 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sun, 28 Aug 2022 14:07:01 +0300 Subject: [PATCH] Sema: add missing calls to resolveStructLayout Closes #12645 --- src/Sema.zig | 5 ++++- test/behavior/packed-struct.zig | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Sema.zig b/src/Sema.zig index 1030fe0473..ff32dc4b33 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -3779,6 +3779,7 @@ fn validateStructInit( if ((is_comptime or block.is_comptime) and (try sema.resolveDefinedValue(block, init_src, struct_ptr)) != null) { + try sema.resolveStructLayout(block, init_src, struct_ty); // In this case the only thing we need to do is evaluate the implicit // store instructions for default field values, and report any missing fields. // Avoid the cost of the extra machinery for detecting a comptime struct init value. @@ -3973,6 +3974,7 @@ fn validateStructInit( try sema.storePtr2(block, init_src, struct_ptr, init_src, struct_init, init_src, .store); return; } + try sema.resolveStructLayout(block, init_src, struct_ty); // Our task is to insert `store` instructions for all the default field values. for (found_fields) |field_ptr, i| { @@ -15843,6 +15845,7 @@ fn finishStructInit( } if (is_ref) { + try sema.resolveStructLayout(block, dest_src, struct_ty); const target = sema.mod.getTarget(); const alloc_ty = try Type.ptr(sema.arena, sema.mod, .{ .pointee_type = struct_ty, @@ -28850,7 +28853,7 @@ pub fn typeHasOnePossibleValue( for (tuple.values) |val, i| { const is_comptime = val.tag() != .unreachable_value; if (is_comptime) continue; - if ((try sema.typeHasOnePossibleValue(block, src, tuple.types[i])) != null) continue; + if ((try sema.typeHasOnePossibleValue(block, src, tuple.types[i])) != null) continue; return null; } return Value.initTag(.empty_struct_value); diff --git a/test/behavior/packed-struct.zig b/test/behavior/packed-struct.zig index 9bc1c1f824..bd312e9cda 100644 --- a/test/behavior/packed-struct.zig +++ b/test/behavior/packed-struct.zig @@ -564,3 +564,18 @@ test "nested packed struct field access test" { try std.testing.expect(arg.g.h == 6); try std.testing.expect(arg.g.i == 8); } + +test "runtime init of unnamed packed struct type" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; + + var z: u8 = 123; + try (packed struct { + x: u8, + pub fn m(s: @This()) !void { + try expect(s.x == 123); + } + }{ .x = z }).m(); +}