-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
fix: more descriptive error message for enum to integer #151122
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
Changes from all commits
da13353
53f0ce8
9f43e6d
61743bf
0f5d89f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||||
| #[repr(u8)] | ||||||||
| enum Priority { //~ HELP: add `#[derive(Copy, Clone)]` to the enum definition | ||||||||
| High = 255, | ||||||||
| Normal = 127, | ||||||||
| Low = 1, | ||||||||
| } | ||||||||
|
|
||||||||
| #[repr(u8)] | ||||||||
| #[derive(Copy, Clone)] | ||||||||
| enum CopyPriority { | ||||||||
| High = 255, | ||||||||
| Normal = 127, | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and we add a new type: #[derive(Copy, Clone)]
#[repr(u8)]
enum CopyPriority {
High = 255,
Normal = 127,
Low = 1,
}for https://github.com/rust-lang/rust/pull/151122/changes#r3144432881 |
||||||||
| Low = 1, | ||||||||
| } | ||||||||
|
|
||||||||
| fn main() { | ||||||||
| let priority = &Priority::Normal; | ||||||||
| let priority = priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ty! will add this.
Jaidenmagnan marked this conversation as resolved.
|
||||||||
| //~| HELP: try dereferencing before the cast | ||||||||
|
|
||||||||
| let priority = &Priority::Normal as u8; //~ ERROR casting `&Priority` as `u8` is invalid | ||||||||
| //~| HELP: cast through a raw pointer first | ||||||||
|
|
||||||||
| let priority = &CopyPriority::Normal; | ||||||||
| let priority = priority as u8; //~ ERROR casting `&CopyPriority` as `u8` is invalid | ||||||||
| //~| HELP: try dereferencing before the cast | ||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| error[E0606]: casting `&Priority` as `u8` is invalid | ||
| --> $DIR/cast-enum-to-int-issue-151116.rs:18:20 | ||
| | | ||
| LL | let priority = priority as u8; | ||
| | ^^^^^^^^^^^^^^ | ||
| | | ||
| help: try dereferencing before the cast | ||
| | | ||
| LL | let priority = *priority as u8; | ||
| | + | ||
| help: add `#[derive(Copy, Clone)]` to the enum definition | ||
| | | ||
| LL + #[derive(Copy, Clone)] | ||
| LL | enum Priority { | ||
| | | ||
|
|
||
| error[E0606]: casting `&Priority` as `u8` is invalid | ||
| --> $DIR/cast-enum-to-int-issue-151116.rs:21:20 | ||
| | | ||
| LL | let priority = &Priority::Normal as u8; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: cast through a raw pointer first | ||
|
|
||
| error[E0606]: casting `&CopyPriority` as `u8` is invalid | ||
| --> $DIR/cast-enum-to-int-issue-151116.rs:25:20 | ||
| | | ||
| LL | let priority = priority as u8; | ||
| | ^^^^^^^^^^^^^^ | ||
| | | ||
| help: try dereferencing before the cast | ||
| | | ||
| LL | let priority = *priority as u8; | ||
| | + | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0606`. |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current version does not imply the suggestion is not correct.
maybe this is more verbose and also give tip for type without
Copyderive:View changes since the review
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or maybe this is better: