Skip to content

Unhelpful diagnostic when casting a tuple-like enum to an int #120756

@achan1989

Description

@achan1989
Contributor

Code

# [derive (Debug)]
pub enum Good {
    A,
    B,
}

# [derive (Debug)]
pub enum Bad {
    A,
    B(u8),
}

fn main() {
    let good = Good::A;
    let bad = Bad::A;
    
    dbg!(good as u32);
    dbg!(bad as u32);
}

Current output

error[E0605]: non-primitive cast: `Bad` as `u32`
  --> src/main.rs:18:10
   |
18 |     dbg!(bad as u32);
   |          ^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

For more information about this error, try `rustc --explain E0605`.

Desired output

an 'as' expression can only cast an enum to an int if all of its variants are unit-like

Possibly link to the Rust Reference where this is explained in detail. https://doc.rust-lang.org/reference/items/enumerations.html#casting

Rationale and extra context

Casting an enum to an int is common for C developers. When I searched the internet for guides on how to do this in Rust, they all essentially said it just works like foo as i32. E.g. stackoverflow, rust by example.

These guides don't explicitly say that this only works if all of the enum variants are unit-like. The compiler message in this situation did not help me to understand what had gone wrong, and googling for it and "enum" returned the same sort of results as mentioned above.

Other cases

No response

Rust Version

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6

Anything else?

No response

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Feb 7, 2024
achan1989

achan1989 commented on Feb 7, 2024

@achan1989
ContributorAuthor

@rustbot label D-newcomer-roadblock D-confusing

added
D-confusingDiagnostics: Confusing error or lint that should be reworked.
D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
on Feb 7, 2024
added
D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
on Feb 7, 2024
gurry

gurry commented on Feb 8, 2024

@gurry
Contributor

@rustbot claim

added a commit that references this issue on Feb 9, 2024
d9a957b
added a commit that references this issue on Feb 9, 2024
759993a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @gurry@achan1989@fmease@rustbot

    Issue actions

      Unhelpful diagnostic when casting a tuple-like enum to an int · Issue #120756 · rust-lang/rust