Commit 8f5db19791

Matthew Lugg <mlugg@mlugg.co.uk>
2025-11-29 12:55:36
Sema: initialize OPV comptime allocs correctly
This was caused a `[0]std.builtin.Type.StructField.Attributes` to be considered `undefined`, even though that type is OPV so should prefer its OPV `.{}` over `undefined`. Resolves: #30039
1 parent e52232c
Changed files (2)
src
test
behavior
src/Sema.zig
@@ -175,9 +175,11 @@ const ComptimeAlloc = struct {
 
 /// `src` may be `null` if `is_const` will be set.
 fn newComptimeAlloc(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type, alignment: Alignment) !ComptimeAllocIndex {
+    const pt = sema.pt;
+    const init_val = try sema.typeHasOnePossibleValue(ty) orelse try pt.undefValue(ty);
     const idx = sema.comptime_allocs.items.len;
     try sema.comptime_allocs.append(sema.gpa, .{
-        .val = .{ .interned = try sema.pt.intern(.{ .undef = ty.toIntern() }) },
+        .val = .{ .interned = init_val.toIntern() },
         .is_const = false,
         .src = src,
         .alignment = alignment,
test/behavior/type.zig
@@ -427,3 +427,12 @@ test "undefined type value" {
     };
     comptime assert(@TypeOf(S.undef_type) == type);
 }
+
+test "reify struct with zero fields through const arrays" {
+    const names: [0][]const u8 = .{};
+    const types: [0]type = .{};
+    const attrs: [0]std.builtin.Type.StructField.Attributes = .{};
+    const S = @Struct(.auto, null, &names, &types, &attrs);
+    comptime assert(@typeInfo(S) == .@"struct");
+    comptime assert(@typeInfo(S).@"struct".fields.len == 0);
+}