-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Enum size depends on ordering of members #117238
Comments
@rustbot label A-layout |
I suspect an inclusive/exclusive range confusion or an off-by-one error here. @rustbot claim |
@Veykril was the change in 2d73d30 meant to fix an observed bug or was it just based on the assumption that the Still trying to understand whether this is a limitation by the way we currently encode/decode niche values or an oversight in how the ranges are calculated. rust/compiler/rustc_abi/src/lib.rs Lines 1392 to 1406 in 11cd1f0
rust/compiler/rustc_abi/src/layout.rs Lines 338 to 344 in 11cd1f0
rust/compiler/rustc_codegen_ssa/src/mir/place.rs Lines 351 to 371 in 11cd1f0
|
That line was supposed to be an inlining of |
Ah, that was just a fix for another commit in the same PR. Nevermind then. I only saw the commit in isolation. |
Ok I think the issue here stems from how the variant index range of niche-encoded variants is calculated. A bool occupies values 0 and 1, which leaves 2..=255 (254 values) as possible niches. In principle that's enough to represent the remaining variants. But to keep things simple codegen only generates a branch to check if it's the non-niche variant and otherwise shifts the range of tag values down (with a subtraction) to match the variant indices. To do this properly we have to detect that the variant index range ends up being more compact if we rely on wraparound and have it start at E8 instead. Should be feasible but this is more than fixing an off-by-one error. I'm not sure if I have time to get to this soon. @rustbot release-assignment |
@rustbot claim |
Since you're working on this: I have a commit for the "// FIXME(eddyb)" in a local branch. I can make a PR for that. I don't think it's actually relevant to this issue, but it should be one less thing to worry about. |
That's great. I haven't looked into that part yet. |
I tried this code:
Then I ran this in gdb and examined the size of the enums:
I expected these to have the same size. The two enums differ only in the placement of the
(bool)
-carrying member.Instead, the two enums have different sizes.
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: