as-casts for enums with tuple-only variants are not prohibited #113904
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Code
Current output
Desired output
No response
Rationale and extra context
Hello,
According to https://doc.rust-lang.org/reference/items/enumerations.html#casting as-casting enum variants to their discriminants should be possible if
they do not have explicit discriminants, or where only unit variants are explicit
(there's also case for unit-only enums but it's irrelevant to the case).Despite all that I can write code as seen in the example that clearly brakes
only unit variants are explicit
rule.Moreover, it compiles without any warnings or errors and produces counter-intuitive results in the runtime, e.g.:
I've confirmed (using recommended pointer casting method) that created enum variant holds the correct value i.e.
Enum::V1(0).discriminant()
is indeed0
.If I correctly understand the intentions of the authors, the expressions like
Enum::V1 as u8
should not be allowed at this time.As for the future, I think that it would be nice if there was a way to fetch the discriminant value of the variant without creating it in the first place, i.e. discussed constructions would be allowed and returned discriminant of that variant.
Few related discussions I've came about while researching this:
#60553 (comment)
#88621
Other cases
No response
Anything else?
No response
The text was updated successfully, but these errors were encountered: