Skip to content

Account for shadowed bindings in E0599 method not found errors #123558

Open

Description

Code

fn main() {
    let x = Some(3);
    let y = vec![1, 2];
    if let Some(y) = x {
        y.push(y);
    }
}

Current output

error[E0599]: no method named `push` found for type `{integer}` in the current scope
 --> src/main.rs:5:11
  |
5 |         y.push(y);
  |           ^^^^ method not found in `{integer}`

Desired output

error[E0599]: no method named `push` found for type `{integer}` in the current scope
 --> src/main.rs:5:11
  |
5 |         y.push(y);
  |           ^^^^ method not found in `{integer}`
note: there's an earlier shadowed binding `y` of type `Vec<{integer}>` that *has* method `push` available
  |
3 |     let y = vec![1, 2];
  |         ^ `y` of type `Vec<{integer}>` that has method `push` defined earlier here
4 |     if let Some(y) = x {
  |                 ^ earlier `y` shadowed here with type `{integer}`

Rationale and extra context

Shadowed bindings can sometimes confuse the user as to why they don't have the expected type. Pointing out shadowed bindings in type and trait errors can help avoid that.

Other cases

E0308 would present similar situations.

Rust Version

rustc 1.78.0-nightly (3b1717c05 2024-03-10)

Anything else?

No response

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

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-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