authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-28 14:07:01+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-28 15:41:21+03:00
log15cc4514e0566430ed1d95e85aaa462642b70364
treeb4522e711412cc2a53924ae2303cd29631ffc28c
parente2dc77ab62edf5446af830356eceef8eefb67cfa

Sema: add missing calls to resolveStructLayout

Closes #12645

2 files changed, 19 insertions(+), 1 deletions(-)

src/Sema.zig+4-1
...@@ -3779,6 +3779,7 @@ fn validateStructInit(...@@ -3779,6 +3779,7 @@ fn validateStructInit(
3779 if ((is_comptime or block.is_comptime) and3779 if ((is_comptime or block.is_comptime) and
3780 (try sema.resolveDefinedValue(block, init_src, struct_ptr)) != null)3780 (try sema.resolveDefinedValue(block, init_src, struct_ptr)) != null)
3781 {3781 {
3782 try sema.resolveStructLayout(block, init_src, struct_ty);
3782 // In this case the only thing we need to do is evaluate the implicit3783 // In this case the only thing we need to do is evaluate the implicit
3783 // store instructions for default field values, and report any missing fields.3784 // store instructions for default field values, and report any missing fields.
3784 // Avoid the cost of the extra machinery for detecting a comptime struct init value.3785 // Avoid the cost of the extra machinery for detecting a comptime struct init value.
...@@ -3973,6 +3974,7 @@ fn validateStructInit(...@@ -3973,6 +3974,7 @@ fn validateStructInit(
3973 try sema.storePtr2(block, init_src, struct_ptr, init_src, struct_init, init_src, .store);3974 try sema.storePtr2(block, init_src, struct_ptr, init_src, struct_init, init_src, .store);
3974 return;3975 return;
3975 }3976 }
3977 try sema.resolveStructLayout(block, init_src, struct_ty);
39763978
3977 // Our task is to insert `store` instructions for all the default field values.3979 // Our task is to insert `store` instructions for all the default field values.
3978 for (found_fields) |field_ptr, i| {3980 for (found_fields) |field_ptr, i| {
...@@ -15843,6 +15845,7 @@ fn finishStructInit(...@@ -15843,6 +15845,7 @@ fn finishStructInit(
15843 }15845 }
1584415846
15845 if (is_ref) {15847 if (is_ref) {
15848 try sema.resolveStructLayout(block, dest_src, struct_ty);
15846 const target = sema.mod.getTarget();15849 const target = sema.mod.getTarget();
15847 const alloc_ty = try Type.ptr(sema.arena, sema.mod, .{15850 const alloc_ty = try Type.ptr(sema.arena, sema.mod, .{
15848 .pointee_type = struct_ty,15851 .pointee_type = struct_ty,
...@@ -28850,7 +28853,7 @@ pub fn typeHasOnePossibleValue(...@@ -28850,7 +28853,7 @@ pub fn typeHasOnePossibleValue(
28850 for (tuple.values) |val, i| {28853 for (tuple.values) |val, i| {
28851 const is_comptime = val.tag() != .unreachable_value;28854 const is_comptime = val.tag() != .unreachable_value;
28852 if (is_comptime) continue;28855 if (is_comptime) continue;
28853 if ((try sema.typeHasOnePossibleValue(block, src, tuple.types[i])) != null) continue; 28856 if ((try sema.typeHasOnePossibleValue(block, src, tuple.types[i])) != null) continue;
28854 return null;28857 return null;
28855 }28858 }
28856 return Value.initTag(.empty_struct_value);28859 return Value.initTag(.empty_struct_value);
test/behavior/packed-struct.zig+15
...@@ -564,3 +564,18 @@ test "nested packed struct field access test" {...@@ -564,3 +564,18 @@ test "nested packed struct field access test" {
564 try std.testing.expect(arg.g.h == 6);564 try std.testing.expect(arg.g.h == 6);
565 try std.testing.expect(arg.g.i == 8);565 try std.testing.expect(arg.g.i == 8);
566}566}
567
568test "runtime init of unnamed packed struct type" {
569 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
570 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
571 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
572 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
573
574 var z: u8 = 123;
575 try (packed struct {
576 x: u8,
577 pub fn m(s: @This()) !void {
578 try expect(s.x == 123);
579 }
580 }{ .x = z }).m();
581}