Skip to content
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

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

Closed
TaKO8Ki opened this issue May 23, 2022 · 2 comments · Fixed by #100058
Closed

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

TaKO8Ki opened this issue May 23, 2022 · 2 comments · Fixed by #100058
Assignees
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

@TaKO8Ki
Copy link
Member

TaKO8Ki commented May 23, 2022

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
@TaKO8Ki TaKO8Ki 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 May 23, 2022
@TaKO8Ki TaKO8Ki self-assigned this May 23, 2022
@TaKO8Ki TaKO8Ki 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 May 23, 2022
@ChayimFriedman2
Copy link
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
Copy link
Contributor

estebank commented Jun 2, 2022

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.

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
Development

Successfully merging a pull request may close this issue.

3 participants