-
Notifications
You must be signed in to change notification settings - Fork 60
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
Dead variant removal in #[repr(C)]
enums
#500
Comments
It's interesting what happens when you remove the
That's more what I expected: single-variant enums can have a zero-sized discriminant. Also interestingly, zero-variant enums with My first reaction is that this is a bug in rustc, we shouldn't do dead variant removal on |
I agree with that. Even if the variant is uninhabited, we guarantee that there's a target-dependant discriminant field in the layout of a |
Assuming that’s a rustc bug, I sent a patch that fixes it. |
…s, r=oli-obk Disable dead variant removal for `#[repr(C)]` enums. This prevents removing dead branches from a `#[repr(C)]` enum (they now get discriminants allocated as if they were inhabited). Implementation notes: ABI of something like ```rust #[repr(C)] enum Foo { Foo(!), } ``` is still `Uninhabited`, but its layout is now computed as if all the branches were inhabited. This seemed to me like a proper way to do it, especially given that ABI sanity check explicitly asserts that type-level uninhabitedness implies ABI uninhabitedness. This probably needs some sort of FCP (given that it changes `#[repr(C)]` layout, which is a stable guarantee), but I’m not sure how to call for one or which team is the most relevant. See rust-lang/unsafe-code-guidelines#500.
Rollup merge of rust-lang#123043 - GoldsteinE:fix/repr-c-dead-branches, r=oli-obk Disable dead variant removal for `#[repr(C)]` enums. This prevents removing dead branches from a `#[repr(C)]` enum (they now get discriminants allocated as if they were inhabited). Implementation notes: ABI of something like ```rust #[repr(C)] enum Foo { Foo(!), } ``` is still `Uninhabited`, but its layout is now computed as if all the branches were inhabited. This seemed to me like a proper way to do it, especially given that ABI sanity check explicitly asserts that type-level uninhabitedness implies ABI uninhabitedness. This probably needs some sort of FCP (given that it changes `#[repr(C)]` layout, which is a stable guarantee), but I’m not sure how to call for one or which team is the most relevant. See rust-lang/unsafe-code-guidelines#500.
The fix is stable now, so I think this could be closed. |
Consider this enum:
For unsafe code it could be useful to assume that
Fun<T>
is layout-compatible withFun<MaybeUninit<T>>
for any choice ofT
. That’s not currently the case: ifT
is uninhabited,Fun<T>
would be zero-sized (or, more generally, the::Fun
variant will not affect enum size), butFun<MaybeUninit<T>>
wouldn’t.Reference currently documents the above enum as being rough equivalent to something like
That doesn’t hold for uninhabited types: this “desugaring” preserves the invariant above.
I think we should do one of the following:
#[repr(C)]
enums;Related: #443
Playground demonstrating the issue: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d2995ff99cea9c2e60d28cbf0f970176
The text was updated successfully, but these errors were encountered: