Skip to content

Wrong suggestions for typos on variables that have the same name as struct fields #97311

@TaKO8Ki

Description

@TaKO8Ki
Member

Given the following code:

1.63.0-nightly (2022-05-22)

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=bb6ee98b3c92917f9eab8607bf3f5deb

struct A {
    config: String,
}

impl A {
    fn do_something(self, cofig: String) {
        println!("{config}"); //~ Error cannot find value `config` in this scope
    }
}

fn main() {}

The current output is:

This suggestion can cause compiler errors.

Compiling playground v0.0.1 (/playground)
error[[E0425]](https://doc.rust-lang.org/nightly/error-index.html#E0425): cannot find value `config` in this scope
 --> src/main.rs:7:20
  |
7 |         println!("{config}"); //~ Error cannot find value `config` in this scope
  |                    ^^^^^^ help: you might have meant to use the available field: `self.config`

For more information about this error, try `rustc --explain E0425`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

help shouldn't be emitted.

or should look like println!("{}", self.config).

Compiling playground v0.0.1 (/playground)
error[[E0425]](https://doc.rust-lang.org/nightly/error-index.html#E0425): cannot find value `config` in this scope
 --> src/main.rs:7:20
  |
7 |         println!("{config}"); //~ Error cannot find value `config` in this scope
  |                    ^^^^^^

For more information about this error, try `rustc --explain E0425`.
error: could not compile `playground` due to previous error

Activity

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 May 23, 2022
self-assigned this
on May 23, 2022
changed the title [-]Wrong suggestions for typos on variables that has the same name as struct fields[/-] [+]Wrong suggestions for typos on variables that have the same name as struct fields[/+] on May 23, 2022
ChayimFriedman2

ChayimFriedman2 commented on May 24, 2022

@ChayimFriedman2
Contributor

Really ideally we should suggest something like println!("{}", self.config), but I'm pretty sure this is infeasible. On the other hand, maybe one day we'll have field accesses in the formatting macros.

estebank

estebank commented on Jun 2, 2022

@estebank
Contributor

I think that if we had #96999 it'd be enough. I consider it "ok" if a suggestion gives you incorrect code that will in turn give you a suggestion for working code.

added a commit that references this issue on Aug 4, 2022
d3aa757
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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

    Participants

    @estebank@ChayimFriedman2@TaKO8Ki

    Issue actions

      Wrong suggestions for typos on variables that have the same name as struct fields · Issue #97311 · rust-lang/rust