Skip to content

Conversation

madsmtm
Copy link
Contributor

@madsmtm madsmtm commented Apr 21, 2025

Add #[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.

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
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!

@madsmtm madsmtm changed the title RFC: Disable niche layout optimization on enum discriminants RFC: Allow making enums usable in FFI May 12, 2025
sinjs pushed a commit to sinjs/wgpu that referenced this pull request Jul 22, 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.
@dhardy
Copy link
Contributor

dhardy commented Aug 31, 2025

Nit: the summary could do with a short example. Currently the first few code snippets are about alternatives.

I really like the idea. I'm a little less sure about the syntax because in this case an attribute is changing the type, not just its implementation.

Ideally we'd have a new keyword, e.g. open_enum Thing { A, B, C }.

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.