-
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 interacting with Enum layout optimization #36394
Comments
Some specific examples: union A {
x: Box<i32>,
y: Box<i64>,
}
union B {
x: Box<i32>,
y: Box<i64>,
z: (),
} The question here is essentially, what is the size of |
Previous discussion (11 April - 28 May) rust-lang/rfcs#1444 (comment) |
|
Layout of non- |
This is another open question for the tracking issue: #32836 |
@nagisa While repr(C) fixes the representation of a type Foo, it does not say anything (that I know to be documented) about the representation of |
@Amanieu unions don't have a discriminant, though, so all three unions should have the size of a pointer. Adding a I don't think the enum layout optimization needs to apply here at all; the size of a union should always match the maximum size of any field type. |
Why do you care about representation of |
I think that this discussion has come to the conclusion that unions interact "as all other things" with enum layout optimization, and I'm going to close. |
I think this issue should be re-opened. It is originally about the nodrop-union crate, which is pretty much the same as
They need not only a way to inhibit automatic drop glue (which Concretely, Some(ManuallyDrop::new(uninitialized::<[String; 100]>())).is_some() More generally, we need to have principles for what to do to use |
Hm, okay. I'll reopen; this was (or is? Was it merged?) an unresolved question in the RFC. |
Could you point to a specific part of the RFC? I can’t find it in rust-lang/rfcs#1897. |
I don't think the original union RFC mentioned it (RFC 1444), but there was some discussion in the thread itself: rust-lang/rfcs#1444 (comment) and rust-lang/rfcs#1444 (comment) (but also just search from "enum layout") |
Ah, found rust-lang/rfcs#1444 (comment) which concludes that adding a |
Just an update: at present assert!(Some(MaybeUninit::<&u8>::zeroed()).is_some()); |
Also see rust-lang/unsafe-code-guidelines#73: discussion of the invariant that a union has to satisfy at all times. This directly informs which layout optimizations are possible. |
…r=nikomatsakis,Centril Test interaction of unions with non-zero/niche-filling optimization Notably this nails down part of the behavior that MaybeUninit assumes, e.g. that a Option<MaybeUninit<&u8>> does not take advantage of non-zero optimization, and thus is a safe construct. It also verifies the status quo: that even unions that could theoretically take advantage of niches don't. (relevant: rust-lang#36394)
IRC (#rust) short discussion log, since I don't actually know much about this:
cc @Amanieu @bluss
The text was updated successfully, but these errors were encountered: