rust-lang / rust-lang/rust

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

Open
#162,949 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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: https://github.com/rust-lang/rust/issues/112269 which was fixed by https://github.com/rust-lang/rust/pull/112272 adding the note

https://github.com/rust-lang/rust/issues/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 https://github.com/rust-lang/rust/issues/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`.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the diagnostic with the Rust snippets in the issue using the listed rustc version, then compare the raw-identifier behavior with the related issue #113989 and pull request #112272. Done means the E0005 output includes the constant-pattern explanation and an appropriate variable suggestion, with the r# spelling decision made consistently with other diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.