authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-11-29 11:55:36+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-11-29 11:55:36+00:00
log8f5db19791dde73e8e969c6ebf821767f1c3e50b
treedc1337c0ff763087963f524e4f8d6e0c18fb887a
parente52232cd570935d7880cbfe52215dbdf0f2d2243
signaturelock-open Commit is signed but in an unrecognized format.

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

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

src/Sema.zig+3-1
......@@ -175,9 +175,11 @@ const ComptimeAlloc = struct {
175175
176176/// `src` may be `null` if `is_const` will be set.
177177fn newComptimeAlloc(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type, alignment: Alignment) !ComptimeAllocIndex {
178 const pt = sema.pt;
179 const init_val = try sema.typeHasOnePossibleValue(ty) orelse try pt.undefValue(ty);
178180 const idx = sema.comptime_allocs.items.len;
179181 try sema.comptime_allocs.append(sema.gpa, .{
180 .val = .{ .interned = try sema.pt.intern(.{ .undef = ty.toIntern() }) },
182 .val = .{ .interned = init_val.toIntern() },
181183 .is_const = false,
182184 .src = src,
183185 .alignment = alignment,
test/behavior/type.zig+9
......@@ -427,3 +427,12 @@ test "undefined type value" {
427427 };
428428 comptime assert(@TypeOf(S.undef_type) == type);
429429}
430
431test "reify struct with zero fields through const arrays" {
432 const names: [0][]const u8 = .{};
433 const types: [0]type = .{};
434 const attrs: [0]std.builtin.Type.StructField.Attributes = .{};
435 const S = @Struct(.auto, null, &names, &types, &attrs);
436 comptime assert(@typeInfo(S) == .@"struct");
437 comptime assert(@typeInfo(S).@"struct".fields.len == 0);
438}