Skip to content

Conversation

@Jaidenmagnan
Copy link

@Jaidenmagnan Jaidenmagnan commented Jan 14, 2026

Fixes #151116
A more descriptive error message when casting an enum to an Integer. Please review issue linked above.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 14, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 14, 2026

r? @chenyukang

rustbot has assigned @chenyukang.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer


fn main() {
let priority = &Priority::Normal;
let priority = priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to check the HELP message as well.
like

Suggested change
let priority = priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid
let priority = priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid
//~| HELP: dereference the expression

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to check the HELP message as well. like

Ty! will add this.

@chenyukang
Copy link
Member

chenyukang commented Jan 17, 2026

you can use Fixes #151116 in PR description to link origin issue, so that if the PR is merged the issue will be closed automatically.

there is no need to paste a screenshot of CI testing, since if any tests in CI failed, we will get it in Github action .

@Jaidenmagnan
Copy link
Author

you can use Fixes #151116 in PR description to link origin issue, so that if the PR is merged the issue will be closed automatically.

there is no need to paste a screenshot of CI testing, since if any tests in CI failed, we will get it in Github action .

ty, just fixed this.

self.expr_span.shrink_to_lo(),
"dereference the expression",
"*",
Applicability::MachineApplicable,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer to use MaybeIncorrect?

since if we apply the suggestion there maybe still some errors:

error[E0507]: cannot move out of `*priority` which is behind a shared reference
  --> tests/ui/cast/cast-enum-to-int-issue-151116.rs:10:20
   |
10 |     let priority = *priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid
   |                    ^^^^^^^^^ move occurs because `*priority` has type `Priority`, which does not implement the `Copy` trait
   |
note: if `Priority` implemented `Clone`, you could clone the value
  --> tests/ui/cast/cast-enum-to-int-issue-151116.rs:2:1
   |
 2 | enum Priority {
   | ^^^^^^^^^^^^^ consider implementing `Clone` for this type
...
10 |     let priority = *priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid
   |                    --------- you could clone this value


fn main() {
let priority = &Priority::Normal;
let priority = priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could add a test to make sure it does not suggest for code:

let priority = &Priority::Normal as u8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error message of casting a reference of #[repr(Int)] enum to Int is lacking details

4 participants