Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type: resolve union tag type before checking for runtime bits #22669

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/Type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -597,12 +597,21 @@ pub fn hasRuntimeBitsInner(
// and then later if our guess was incorrect, we emit a compile error.
if (union_type.assumeRuntimeBitsIfFieldTypesWip(ip)) return true;
},
.safety, .tagged => {},
}
switch (strat) {
.sema => try ty.resolveFields(strat.pt(zcu, tid)),
.eager => assert(union_flags.status.haveFieldTypes()),
.lazy => if (!union_flags.status.haveFieldTypes())
return error.NeedLazy,
}
switch (union_flags.runtime_tag) {
.none => {},
.safety, .tagged => {
const tag_ty = union_type.tagTypeUnordered(ip);
// tag_ty will be `none` if this union's tag type is not resolved yet,
// in which case we want control flow to continue down below.
if (tag_ty != .none and
try Type.fromInterned(tag_ty).hasRuntimeBitsInner(
// tag_ty should have been resolved above
assert(tag_ty != .none);
if (try Type.fromInterned(tag_ty).hasRuntimeBitsInner(
ignore_comptime_only,
strat,
zcu,
Expand All @@ -612,12 +621,6 @@ pub fn hasRuntimeBitsInner(
}
},
}
switch (strat) {
.sema => try ty.resolveFields(strat.pt(zcu, tid)),
.eager => assert(union_flags.status.haveFieldTypes()),
.lazy => if (!union_flags.status.haveFieldTypes())
return error.NeedLazy,
}
for (0..union_type.field_types.len) |field_index| {
const field_ty = Type.fromInterned(union_type.field_types.get(ip)[field_index]);
if (try field_ty.hasRuntimeBitsInner(ignore_comptime_only, strat, zcu, tid))
Expand Down
8 changes: 8 additions & 0 deletions test/behavior/eval.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,14 @@ test "lazy sizeof is resolved in division" {
try expect(@sizeOf(A) - a == 2);
}

test "lazy sizeof union tag size in compare" {
const A = union(enum) {
a: void,
b: void,
};
try expect(@sizeOf(A) == 1);
}

test "lazy value is resolved as slice operand" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
Expand Down
Loading