Skip to content

Add subdiagnostic suggestion to unresolved-reference diagnostic if it's a method scope and the variable exists on self #502

@AlexWaygood

Description

@AlexWaygood

If you do something like this in Rust:

struct Foo {
    x: u8
}

impl Foo {
    fn method(&self) {
        let y = x;
    }
}

Then rustc gives a really nice diagnostic that tells you you probably meant to access self.x:

error[E0425]: cannot find value `x` in this scope
 --> src/main.rs:7:17
  |
7 |         let y = x;
  |                 ^
  |
help: you might have meant to use the available field
  |
7 |         let y = self.x;
  |                 +++++

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=00fc5f337e983eeedfdcaab2d70df558

We should add a similar suggestion to our unresolved-reference diagnostic in situations like this:

class Foo:
    x: int

    def method(self):
        y = x

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsRelated to reporting of diagnostics.help wantedContributions especially welcome

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions