fix: more descriptive error message for enum to integer#151122
fix: more descriptive error message for enum to integer#151122Jaidenmagnan wants to merge 5 commits intorust-lang:mainfrom
Conversation
|
r? @chenyukang rustbot has assigned @chenyukang. Use |
|
|
||
| fn main() { | ||
| let priority = &Priority::Normal; | ||
| let priority = priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid |
There was a problem hiding this comment.
It might be a good idea to check the HELP message as well.
like
| 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 |
There was a problem hiding this comment.
It might be a good idea to check the HELP message as well. like
Ty! will add this.
|
you can use 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, |
There was a problem hiding this comment.
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|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
|
I will fix this up next week, thanks! Didn't see this. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This comment has been minimized.
This comment has been minimized.
|
@rustbot ready |
| err.span_suggestion_verbose( | ||
| self.expr_span.shrink_to_lo(), | ||
| "dereference the expression", | ||
| "*", | ||
| Applicability::MaybeIncorrect, | ||
| ); |
There was a problem hiding this comment.
The current version does not imply the suggestion is not correct.
maybe this is more verbose and also give tip for type without Copy derive:
| err.span_suggestion_verbose( | |
| self.expr_span.shrink_to_lo(), | |
| "dereference the expression", | |
| "*", | |
| Applicability::MaybeIncorrect, | |
| ); | |
| let help = format!( | |
| "try dereferencing before the cast{}", | |
| if !fcx.type_is_copy_modulo_regions(fcx.param_env, inner_ty) { | |
| format!( | |
| ", you also need to add `#[derive(Copy, Clone)]` to the enum definition", | |
| ) | |
| } else { | |
| String::new() | |
| } | |
| ); | |
| err.span_suggestion_verbose( | |
| self.expr_span.shrink_to_lo(), | |
| help, | |
| "*", | |
| Applicability::MaybeIncorrect, | |
| ); |
There was a problem hiding this comment.
or maybe this is better:
err.span_suggestion_verbose(
self.expr_span.shrink_to_lo(),
"try dereferencing before the cast",
"*",
Applicability::MaybeIncorrect,
);
if !fcx.type_is_copy_modulo_regions(fcx.param_env, inner_ty) {
err.span_suggestion_verbose(
fcx.tcx.def_span(adt_def.did()).shrink_to_lo(),
"add `#[derive(Copy, Clone)]` to the enum definition",
"#[derive(Copy, Clone)]\n",
Applicability::MaybeIncorrect,
);
}| #[repr(u8)] | ||
| enum Priority { | ||
| High = 255, | ||
| Normal = 127, |
There was a problem hiding this comment.
and we add a new type:
#[derive(Copy, Clone)]
#[repr(u8)]
enum CopyPriority {
High = 255,
Normal = 127,
Low = 1,
}|
@rustbot author |
|
@rustbot ready |
|
Thanks! |
fix: more descriptive error message for enum to integer Fixes rust-lang#151116 A more descriptive error message when casting an enum to an Integer. Please review issue linked above.
Rollup of 15 pull requests Successful merges: - #151122 (fix: more descriptive error message for enum to integer) - #155341 (generic_const_args: allow paths to non type consts) - #156062 (Added command-line argument support for `wasm32-wali-linux-musl`) - #156159 ([AIX] add -bdbg:namedsects:ss link arg) - #156174 (Wasm: remove implicit `__heap_base`/`__data_end` exports) - #156186 (fix: remap ci-llvm debug paths via `-ffile-prefix-map`) - #156193 (port `rustc_ast*` crates from `box_` to `deref_patterns`) - #156201 (Don't run ui-fulldeps tests twice in stage 1) - #155808 (Always use `ConstFn` context for `const` closures) - #156105 (interpret: correctly deal with repr(transparent) enums) - #156148 (Use `all_impls` instead of handrolling it) - #156156 (Adjust getMCSubtargetInfo signature for LLVM 23+) - #156170 (add known-bug test for coroutine 'static-yields-non-'static unsoundness (#144442)) - #156195 (Move tests codegen) - #156205 (move generalization test)
View all comments
Fixes #151116
A more descriptive error message when casting an enum to an Integer. Please review issue linked above.