Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
matthiaskrgr opened this issue Jun 4, 2023 · 0 comments · Fixed by #112272
Closed

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

matthiaskrgr opened this issue Jun 4, 2023 · 0 comments · Fixed by #112272
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Jun 4, 2023

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.


@matthiaskrgr matthiaskrgr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 4, 2023
@bors bors closed this as completed in e410606 Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
1 participant