Skip to content

Missing "interpreted as a constant pattern" note for raw identifiers in local bindings #162949

Description

@ZenPZero

Code

#[allow(non_upper_case_globals)]
const x: u8 = 0;

fn main() {
  let r#x = 0;
}

Current output

error[E0005]: refutable pattern in local binding
 --> src/main.rs:5:7
  |
5 |   let r#x = 0;
  |       ^^^ pattern `1_u8..=u8::MAX` not covered
  |
  = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
  = note: for more information, visit https://doc.rust-lang.org/book/ch19-02-refutability.html
  = note: the matched value is of type `u8`
help: you might want to use `if let` to ignore the variant that isn't matched
  |
5 |   if let r#x = 0 { todo!() };
  |   ++             +++++++++++

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

Desired output

error[E0005]: refutable pattern in local binding
 --> src/main.rs:5:7
  |
2 | const x: u8 = 0;
  | ----------- missing patterns are not covered because `x` is interpreted as a constant pattern, not a new variable
...
5 |   let r#x = 0;
  |         ^ pattern `1_u8..=u8::MAX` not covered
  |
  = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
  = note: for more information, visit https://doc.rust-lang.org/book/ch19-02-refutability.html
  = note: the matched value is of type `u8`
help: introduce a variable instead
  |
5 |   let r#x_var = 0;
  |          ++++

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

Rationale and extra context

First related issue I could find: #112269 which was fixed by #112272 adding the note

#113989 was made after and perhaps the fix was not stable at the time

  • Whether the suggestion to "introduce a variable instead" should keep the the r# or not should be decided to be consistent with other diagnostics

Other cases

Rust Version

rustc 1.98.1 (48a229cea 2026-09-01)
binary: rustc
commit-hash: 48a229ceaefd4985c50990b14116b6d856af0985
commit-date: 2026-09-01
host: x86_64-unknown-linux-gnu
release: 1.98.1
LLVM version: 22.1.8

Anything else?

I am not familiar with compiler internals but I assume that there may be other things that behave differently when using raw identifiers. For example I was able to come up with this discrepancy based on the observation the author of #112269 made about statics:

#[allow(non_upper_case_globals)]
const y: i32 = 4;
fn main() {
  static y: i32 = 3;
  let y = 0;
}
error[E0530]: let bindings cannot shadow statics
 --> src/main.rs:5:7
  |
4 |   static y: i32 = 3;
  |   ------------------ the static `y` is defined here
5 |   let y = 0;
  |       ^ cannot be named the same as a static

warning: unused variable: `y`
 --> src/main.rs:5:7
  |
5 |   let y = 0;
  |       ^
  |
  = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default
help: if this is intentional, prefix it with an underscore
  |
5 |   let _y = 0;
  |       +

For more information about this error, try `rustc --explain E0530`.
#[allow(non_upper_case_globals)]
const y: i32 = 4;
fn main() {
  static y: i32 = 3;
  let r#y = 0;
}
error[E0530]: let bindings cannot shadow statics
 --> src/main.rs:5:7
  |
4 |   static y: i32 = 3;
  |   ------------------ the static `y` is defined here
5 |   let r#y = 0;
  |       ^^^ cannot be named the same as a static

warning: unused variable: `y`
 --> src/main.rs:5:7
  |
5 |   let r#y = 0;
  |       ^^^
  |
  = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default
help: you might have meant to pattern match on the similarly named constant `y`
  |
5 -   let r#y = 0;
5 +   let y = 0;
  |
help: if this is intentional, prefix it with an underscore
  |
5 -   let r#y = 0;
5 +   let _y = 0;
  |

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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

      No branches or pull requests

      Issue actions