Commit 15cc4514e0

Veikka Tuominen <git@vexu.eu>
2022-08-28 13:07:01
Sema: add missing calls to resolveStructLayout
Closes #12645
1 parent e2dc77a
Changed files (2)
src
test
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);
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();
+}