Skip to content

very weird warning when using same varname for const and let binding #112269

Closed

Description

@matthiaskrgr

Code

pub fn main() {
    const y: i32 = 4;
    let y: i32 = 3;
}

This will cause rustc to interpret the y as a if let binding for some reason which seems quite surprising.

error[E0005]: refutable pattern in local binding
 --> src/main.rs:3:9
  |
3 |     let y: i32 = 3;
  |         ^ patterns `i32::MIN..=3_i32` and `5_i32..=i32::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/ch18-02-refutability.html
  = note: the matched value is of type `i32`
help: you might want to use `if let` to ignore the variants that aren't matched
  |
3 |     if let y: i32 = 3 { todo!() };
  |     ++                +++++++++++

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

When I replace let by static, I get a warning that seems to be much closer to reality:

pub fn main() {
    const y: i32 = 4;
    static y: i32 = 3;
}
error[E0428]: the name `y` is defined multiple times
 --> src/main.rs:3:5
  |
2 |     const y: i32 = 4;
  |     ----------------- previous definition of the value `y` here
3 |     static y: i32 = 3;
  |     ^^^^^^^^^^^^^^^^^^ `y` redefined here
  |
  = note: `y` must be defined only once in the value namespace of this block

For more information about this error, try `rustc --explain E0428`.
error: could not compile `playground` (bin "playground") due to previous error

IMO it would make sense to issue such warning for the first case as well, instead of the if-let/range diagnostic.


Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 4, 2023
  2. added a commit that references this issue on Jun 5, 2023
    e410606
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