-
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
Make specifying repr optional for fieldless enums #88203
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
28ce92e
to
4b24701
Compare
This comment has been minimized.
This comment has been minimized.
4b24701
to
9f5c9fd
Compare
I don't believe we should allow enums with struct or tuple variants (even if they don't have fields) to omit the repr. (Ideally, I'd also prefer if such enums couldn't be Personally, I'm hoping that #81642 helps improve this substantially, so that using |
@fee1-dead Do you think you'd be able to prepare a PR that rejected casting enums with 0-field tuple/struct variants? (Per the discussion at https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/arbitrary.20enum.20discriminants/near/251899236) I can maybe give it a try myself if you'd like. |
I'm busy with other stuff. You can do it if you'd like. |
☔ The latest upstream changes (presumably #89884) made this pull request unmergeable. Please resolve the merge conflicts. |
merge conflicts |
Hello. This was discussed at the lang team meeting today.
|
Actually, the answer is "yes". https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f38e9d525cd1ca67ea8836aa65d6f7b9 #[repr(u8)]
pub enum Foo {
A,
B(),
C{},
}
pub fn a(foo: Foo) -> u8 {
foo as u8
} |
This makes it consistent for another behavior, which is casting an enum to its discriminant.
We can currently cast "fieldless" enums to its discriminants, but only "c-like" (only unit variants) enums are allowed to leave the repr unspecified. See rust-lang/reference#1055 (comment) for previous discussion.