Enum with non-primitive fields can be errorneously cast to primitive type #136508
Labels
A-coercions
Area: implicit and explicit `expr as Type` coercions
C-bug
Category: This is a bug.
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.
I tried this code:
I expected to see this happen: The code should fail to compile, as casting to u8 for this enum is not allowed.
Instead, this happened: code compiles fine, and prints garbage. The garbage printed appears to be part of the address of the function that would instantiate the respective variant of the enum. As there are no warnings issued, this can be very nasty. Clippy does detect this and correctly warns.
Playground for easy reproduction on any compiler of choice.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3e3b23e01f545caf4f4fc88ab45edd61
Note
The text was updated successfully, but these errors were encountered: