Skip to content

fix: more descriptive error message for enum to integer#151122

Open
Jaidenmagnan wants to merge 5 commits intorust-lang:mainfrom
Jaidenmagnan:main
Open

fix: more descriptive error message for enum to integer#151122
Jaidenmagnan wants to merge 5 commits intorust-lang:mainfrom
Jaidenmagnan:main

Conversation

@Jaidenmagnan
Copy link
Copy Markdown

@Jaidenmagnan Jaidenmagnan commented Jan 14, 2026

View all comments

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread compiler/rustc_hir_typeck/src/cast.rs Outdated
self.expr_span.shrink_to_lo(),
"dereference the expression",
"*",
Applicability::MachineApplicable,
Copy link
Copy Markdown
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

Comment thread tests/ui/cast/cast-enum-to-int-issue-151116.rs
@chenyukang
Copy link
Copy Markdown
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 10, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 10, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@Jaidenmagnan
Copy link
Copy Markdown
Author

Jaidenmagnan commented Feb 10, 2026

I will fix this up next week, thanks! Didn't see this.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Apr 13, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 26, 2026

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.

@rustbot

This comment has been minimized.

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 26, 2026
@Jaidenmagnan
Copy link
Copy Markdown
Author

@rustbot ready

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 26, 2026
Comment on lines +302 to +307
err.span_suggestion_verbose(
self.expr_span.shrink_to_lo(),
"dereference the expression",
"*",
Applicability::MaybeIncorrect,
);
Copy link
Copy Markdown
Member

@chenyukang chenyukang Apr 27, 2026

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 Copy derive:

Suggested change
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,
);

View changes since the review

Copy link
Copy Markdown
Member

@chenyukang chenyukang Apr 27, 2026

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:

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,
Copy link
Copy Markdown
Member

@chenyukang chenyukang Apr 27, 2026

Choose a reason for hiding this comment

The 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

View changes since the review

@chenyukang
Copy link
Copy Markdown
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 5, 2026
@Jaidenmagnan Jaidenmagnan requested a review from chenyukang May 5, 2026 20:34
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 5, 2026
@Jaidenmagnan
Copy link
Copy Markdown
Author

@rustbot ready

@chenyukang
Copy link
Copy Markdown
Member

Thanks!
@bors r=chenyukang

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 5, 2026

📌 Commit 0f5d89f has been approved by chenyukang

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 5, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request May 6, 2026
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.
rust-bors Bot pushed a commit that referenced this pull request May 6, 2026
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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