-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Only mark unions as uninhabited if all of their fields are uninhabited #46859
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @petrochenkov (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
What should ( |
I would expect That's not to say that there isn't a use for a union union MaybeInitialized<T> {
init: T,
uninit: ()
} That should be used whenever cleaned up data sticks around, that is when "uninitialized" is a valid state to be in. All that said, I think The safe way to write |
@gereeter Part of the problem is that people have conflated |
@eddyb Oh, absolutely. I guess my point is that people have gone further and collapsed some |
@gereeter I still prefer not having unions able to be uninhabited but maybe there's a point to it? |
Hmm, what's an empty union? In the "a union is an enum without an explicit discriminant" model it's uninhabited; in the "a union is a struct where all the fields overlap" model it's a ZST. Though in both of those models, a union with one uninhabited field is uninhabited... Edit: To answer my own question, 0-field unions are an error. |
I admit that the biggest motivation for me is that it feels more consistent. We call enums tagged unions, but an enum with all uninhabited variants is uninhabited. Therefore, a tag with a union with all uninhabited variants should be uninhabited. We have safe functions like |
@scottmcm Is the following a valid implementation of union Transmuter<T, U> {
start: T,
end: U
}
unsafe fn transmute<T, U>(value: T) -> U {
Transmuter { start: value }.end
} In the "overlapping struct" model, I'd say definitely yes. In the "tagless enum" model, I'm not sure. |
This isn't really about implementation details, which don't really matter here either way. |
@eddyb Sorry, I meant an implementation detail of a data structure library. If I have a data structure like |
@gereeter What does a sound usecase look like? If you can optimize it out, how do you use |
Uninhabitedness is the first thing I've seen where the "a union is an enum" model is clearly better than the"a union is a struct" model, since a struct is uninhabited if any field is uninhabited (aka the issue at hand) whereas an enum is uninhabited only if all variants are uninhabited. @gereeter That's a question for the smantics-of-unsafe-code folks, I think -- it seems open as of https://github.com/petrochenkov/rfcs/blob/8f1a960844b6ae37ec19fd89d39355fa98b1bb2b/text/1444-union.md#delayed-and-unresolved-questions |
I think that we want unions (or, at least, |
…is certainly sound, and the general consensus seems to value not having footguns over some sort of aesthetic consistency.
@eddyb I'm thinking of some sort of wrapper like struct PermutedDrop<A: Array> {
data: [ManuallyDrop<A::Item>; A::LENGTH],
order: [usize; A::LENGTH]
}
impl<A: Array> Drop for PermutedDrop<A> {
fn drop(&mut self) {
for idx in self.order {
unsafe {
ManuallyDrop::drop(&mut self.data[idx]);
}
}
}
} that rearranges the drop order of the things it contains. If @arielb1 I'd posit that almost all code that assumes that unions cannot be uninhabited is broken in some other way. In that case, this being a footgun is not a problem - it just makes it more likely that the unsoundness will be noticed. That said, I may be wrong about this, and I think the situation is a bit different with |
I don't even think we treat |
@eddyb Oh. Well, that also seems wrong to me (for Since it seems to be the most accepted opinion, doesn't break code, and isn't setting a precedent that hasn't already been set, I switched this PR to always making unions inhabited. |
src/librustc/ty/layout.rs
Outdated
if def.is_union() { | ||
let packed = def.repr.packed(); | ||
if packed && def.repr.align > 0 { | ||
bug!("Union cannot be packed and aligned"); | ||
} | ||
if variants.len() != 1 { | ||
bug!("Union must be represented as a single variant"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove this too? (unions are single-variant by construction)
@bors r+ |
📌 Commit da97917 has been approved by |
⌛ Testing commit da97917 with merge 34c65e22a3c9b0610bfd98e9cf2244df4656ccc3... |
💔 Test failed - status-travis |
@bors retry
( |
Only mark unions as uninhabited if all of their fields are uninhabited Fixes #46845.
☀️ Test successful - status-appveyor, status-travis |
Looks like we forgot to backport this to 1.23.0 (sorry about that!) so removing the beta tags |
@alexcrichton #45225 was backed out from that beta (IIUC) so there wasn't anything to backport. |
Oh yay! |
Fixes #46845.