Skip to content

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

madsmtm
Copy link
Contributor

@madsmtm madsmtm commented Apr 21, 2025

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.

@rustbot rustbot added the T-lang Relevant to the language team, which will review and decide on the RFC. label Apr 21, 2025
Comment on lines 474 to 499
## 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.
Copy link
Contributor Author

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.

madsmtm added a commit to madsmtm/wgpu that referenced this pull request Apr 25, 2025
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.
cwfitzgerald pushed a commit to gfx-rs/wgpu that referenced this pull request Apr 25, 2025
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.
@madsmtm
Copy link
Contributor Author

madsmtm commented May 5, 2025

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.