authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2025-01-29 18:17:27-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-22 22:22:55-05:00
log84aac8b6c7721d53a93e636e803e867933ae6e99
tree7eb8314778e6c9c5a844cc0feebd6545c9c5e291
parenta502301b5eb84329c8c3ecb4b68bc52048f99cdc

Type: resolve union tag type before checking for runtime bits


2 files changed, 24 insertions(+), 15 deletions(-)

src/Type.zig+16-15
......@@ -604,21 +604,7 @@ pub fn hasRuntimeBitsInner(
604604 // and then later if our guess was incorrect, we emit a compile error.
605605 if (union_type.assumeRuntimeBitsIfFieldTypesWip(ip)) return true;
606606 },
607 .safety, .tagged => {
608 const tag_ty = union_type.tagTypeUnordered(ip);
609 // tag_ty will be `none` if this union's tag type is not resolved yet,
610 // in which case we want control flow to continue down below.
611 if (tag_ty != .none and
612 try Type.fromInterned(tag_ty).hasRuntimeBitsInner(
613 ignore_comptime_only,
614 strat,
615 zcu,
616 tid,
617 ))
618 {
619 return true;
620 }
621 },
607 .safety, .tagged => {},
622608 }
623609 switch (strat) {
624610 .sema => try ty.resolveFields(strat.pt(zcu, tid)),
......@@ -626,6 +612,21 @@ pub fn hasRuntimeBitsInner(
626612 .lazy => if (!union_flags.status.haveFieldTypes())
627613 return error.NeedLazy,
628614 }
615 switch (union_flags.runtime_tag) {
616 .none => {},
617 .safety, .tagged => {
618 const tag_ty = union_type.tagTypeUnordered(ip);
619 assert(tag_ty != .none); // tag_ty should have been resolved above
620 if (try Type.fromInterned(tag_ty).hasRuntimeBitsInner(
621 ignore_comptime_only,
622 strat,
623 zcu,
624 tid,
625 )) {
626 return true;
627 }
628 },
629 }
629630 for (0..union_type.field_types.len) |field_index| {
630631 const field_ty = Type.fromInterned(union_type.field_types.get(ip)[field_index]);
631632 if (try field_ty.hasRuntimeBitsInner(ignore_comptime_only, strat, zcu, tid))
test/behavior/eval.zig+8
......@@ -1360,6 +1360,14 @@ test "lazy sizeof is resolved in division" {
13601360 try expect(@sizeOf(A) - a == 2);
13611361}
13621362
1363test "lazy sizeof union tag size in compare" {
1364 const A = union(enum) {
1365 a: void,
1366 b: void,
1367 };
1368 try expect(@sizeOf(A) == 1);
1369}
1370
13631371test "lazy value is resolved as slice operand" {
13641372 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
13651373 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO