-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Unions with uninhabited variants are considered uninhabited #46845
Comments
This becomes a problem when making an array of unions: #![allow(unused)]
use std::mem;
#[derive(Copy, Clone)]
enum Never {}
union Foo {
a: u64,
b: Never
}
fn main() {
println!("{}", mem::size_of::<Foo>());
let f = [Foo { a: 42 }, Foo { a: 10 }];
println!("{:?}", unsafe { f[0].a });
} The above code represents a regression (prints 42 on stable, garbage on beta/nightly). |
I didn't realize it was a regression to beta - in that case I'll put up an independent fix so that it can be backported if desired. |
Although I can't trigger it on the playground, you code is actually triggering an ICE for me:
EDIT: I bisected the ICE to somewhere between "rust 1.23.0-nightly (e97ba83 2017-11-25)" and "rust 1.23.0-nightly (827cb0d 2017-11-26)". |
cc @eddyb |
I put up a PR to fix this, but I'm still confused about the ICE (which I moved to #46855). |
Bounce out the layout refactor from beta @eddyb's #45225 was supposed to get into get into 1.24, but due to an ordering mistake, it had snuck into 1.23. That wide-effect translation-changing PR had poked LLVM's weak corners and caused many regressions (3 of them have fixes I include here, but also #46897, #46845, #46449, #46371). I don't think it is a good idea to land it in the beta (1.23) because there are bound to be some regressions we didn't patch. Therefore, I am reverting it in time for stable, along with its related regression fixes. r? @michaelwoerister (I think)
Only mark unions as uninhabited if all of their fields are uninhabited Fixes #46845.
Uninhabited variants should ideally be irrelevant to the representation, just like in enums.
As a test, this prints out 0 instead of 8:
This happens because rustc considers unions more like structs than enums, and the test for uninhabitedness sees a single variant with two fields, one of which is uninhabited.
I have a fix for this, but it's slightly easier to bundle it with some other changes I'm working on.
The text was updated successfully, but these errors were encountered: