-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RFC: Disable niche layout optimization on enum discriminants #3803
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
base: master
Are you sure you want to change the base?
Conversation
text/3803-enum-repr-no-niches.md
Outdated
## Syntax | ||
|
||
Current options and sub-options: | ||
1. Use `#[repr(_, something_here)]`. | ||
Advantage: The feature semantically modifies the layout/representation. | ||
1. `#[repr(_, no_niches)]`. | ||
Advantage: Very precise in what it does. | ||
Disadvantage: Uses "niche" which is a niche term. | ||
2. `#[repr(_, non_exhaustive)]`. | ||
Advantage: Clear that it implies `#[non_exhaustive]`. | ||
Disadvantage: Might be confusing for users which kind of `non_exhaustive` they should use. | ||
3. `#[repr(_, abi_stable)]`. | ||
3. `#[repr(_, all_bits_valid)]`. | ||
4. `#[repr(_, discriminant = no_niches)]` to mirror [RFC 3659](https://github.com/rust-lang/rfcs/pull/3659). | ||
2. Use `#[non_exhaustive(something_here)]`. | ||
Advantage: Might be easier to explain the effect to users ("this works just like `#[non_exhaustive]`, except stronger"). | ||
Advantage: Might align better with future additions to `#[non_exhaustive]`, such as [`#[non_exhaustive(pub)]`](https://internals.rust-lang.org/t/pre-rfc-relaxed-non-exhaustive-structs/11977). | ||
1. `#[non_exhaustive(no_niches)]`. | ||
2. `#[non_exhaustive(abi)]`. | ||
3. `#[non_exhaustive(repr)]`. | ||
4. `#[non_exhaustive(layout)]`. | ||
3. New attribute `#[really_non_exhaustive]` or similar. | ||
|
||
We have a kind of decision tree here, where some unresolved questions depend on the syntax. The exact syntax does not need to be decided before accepting the RFC, though we should choose one of the main "branches". | ||
|
||
The RFC author himself is undecided on the syntax, but decided to go with `#[repr(_, no_niches)]` for the body of the RFC, to make the desired semantics clearer to the RFC reader. |
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.
Might be T-opsem
purview in terms of "niche" vs. "ABI" vs. "non_exhaustive" terminology? See also rust-lang/unsafe-code-guidelines#122.
The `metal` crate is currently unsound regarding unknown/future enum variants, see gfx-rs/metal-rs#209 and rust-lang/rfcs#3803. `objc2-metal` fixes this by emitting C enums as a newtype + constants for each variant, but that prevents us from importing the variants/constants. So this commit converts to a pattern that works with that in preparation for the migration.
The `metal` crate is currently unsound regarding unknown/future enum variants, see gfx-rs/metal-rs#209 and rust-lang/rfcs#3803. `objc2-metal` fixes this by emitting C enums as a newtype + constants for each variant, but that prevents us from importing the variants/constants. So this commit converts to a pattern that works with that in preparation for the migration.
More straightforward framing.
Thanks for the comments all, and sorry for the delay. I believe I've incorporated your suggestions into the RFC text, but feel free to open a new comment if you disagree! |
Allow
#[repr(_, open)]
on field-less (C-style) enums to allow the enum to contain unknown variants, which enables using the enum directly in FFI.Enums with the
open
modifier cannot be matched on exhaustively without the use of a wildcard arm. This feature is similar but distinct from the#[non_exhaustive]
attribute, since it works on an ABI level and applies in the defining module as well.Based on @thomcc's initial work on a similar RFC. This kind of functionality has also been discussed in the past on the internals forum, more recently on Zulip and possibly many more times before that.
Pretty sure this is
T-lang
purview:@rustbot label T-lang
Please try to create inline comments for discussions to keep this RFC manageable for me.
Rendered.