Skip to content
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

Closed

Conversation

fee1-dead
Copy link
Member

@fee1-dead fee1-dead commented Aug 21, 2021

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.

@rust-highfive
Copy link
Collaborator

r? @matthewjasper

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 21, 2021
@rust-log-analyzer

This comment has been minimized.

@inquisitivecrystal inquisitivecrystal added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Aug 24, 2021
@joshtriplett
Copy link
Member

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 as-cast to their discriminant, but that seems like it'd break backwards compatibility.)

Personally, I'm hoping that #81642 helps improve this substantially, so that using as becomes less necessary.

@ehuss
Copy link
Contributor

ehuss commented Sep 13, 2021

@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.

@fee1-dead
Copy link
Member Author

I'm busy with other stuff. You can do it if you'd like.

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 3, 2021
@bors
Copy link
Contributor

bors commented Oct 15, 2021

☔ The latest upstream changes (presumably #89884) made this pull request unmergeable. Please resolve the merge conflicts.

@JohnCSimon
Copy link
Member

merge conflicts

@fee1-dead fee1-dead added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 31, 2021
@pnkfelix pnkfelix added the I-lang-nominated Nominated for discussion during a lang team meeting. label Nov 4, 2021
@pnkfelix
Copy link
Member

pnkfelix commented Nov 4, 2021

discussed at T-compiler meeting; we think this is waiting on lang team decision regarding e.g. #88621 and PR #89234

@Lokathor
Copy link
Contributor

Hello. This was discussed at the lang team meeting today.

  • The main open question was if this change affects Stable or not. Currently, having a discriminant on a non-unit enum variant requires the arbitrary_enum_discriminant feature. This change should likely be "no more stable" than that feature, and it's unclear.
  • Also, would as casting work for enums declared like this? In the meeting it was assumed "no".

@fee1-dead
Copy link
Member Author

would as casting work for enums declared like this? In the meeting it was assumed "no".

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.