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

Typo suggestion for a variable with a name similar to struct fields #97240

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
);
}
}
// Try Levenshtein algorithm.
let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected);
if path.len() == 1 && self.self_type_is_available() {
if let Some(candidate) = self.lookup_assoc_candidate(ident, ns, is_expected) {
let self_is_available = self.self_value_is_available(path[0].ident.span);
Expand All @@ -452,18 +454,19 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.span_suggestion(
span,
"you might have meant to use the available field",
format!("self.{}", path_str),
format!("self.{path_str}"),
Applicability::MachineApplicable,
);
} else {
err.span_label(span, "a field by this name exists in `Self`");
}
self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
}
AssocSuggestion::MethodWithSelf if self_is_available => {
err.span_suggestion(
span,
"you might have meant to call the method",
format!("self.{}", path_str),
format!("self.{path_str}"),
Applicability::MachineApplicable,
);
}
Expand All @@ -474,7 +477,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.span_suggestion(
span,
&format!("you might have meant to {}", candidate.action()),
format!("Self::{}", path_str),
format!("Self::{path_str}"),
Applicability::MachineApplicable,
);
}
Expand All @@ -493,16 +496,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {

err.span_suggestion(
call_span,
&format!("try calling `{}` as a method", ident),
format!("self.{}({})", path_str, args_snippet),
&format!("try calling `{ident}` as a method"),
format!("self.{path_str}({args_snippet})"),
Applicability::MachineApplicable,
);
return (err, candidates);
}
}

// Try Levenshtein algorithm.
let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected);
// Try context-dependent help if relaxed lookup didn't work.
if let Some(res) = res {
if self.smart_resolve_context_dependent_help(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
struct Foo {
config: String,
}

impl Foo {
fn new(cofig: String) -> Self {
Self { config } //~ Error cannot find value `config` in this scope
}

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

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

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
error[E0425]: cannot find value `config` in this scope
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:7:16
|
LL | Self { config }
| ^^^^^^
| |
| a field by this name exists in `Self`
| help: a local variable with a similar name exists: `cofig`

error[E0425]: cannot find value `config` in this scope
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:11:20
|
LL | println!("{config}");
| ^^^^^^
| |
| a field by this name exists in `Self`
| help: a local variable with a similar name exists: `cofig`

error[E0425]: cannot find value `config` in this scope
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:15:20
|
LL | println!("{config}");
| ^^^^^^
|
help: you might have meant to use the available field
|
LL | println!("{self.config}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by comment: this probably shouldn't be emitted, as it won't compile.

Copy link
Member

@compiler-errors compiler-errors May 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that's a bit orthogonal to this diff, though. It might be possible to detect this case from HIR, but not related to the suggestion added here.

@TaKO8Ki, I would appreciate if you find some time to investigate suppressing this. Don't block this PR on that if it's more difficult than it seems -- if not immediately easy to fix, then could you file an issue and downgrade the suggestion from MachineApplicable to MaybeIncorrect? 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would appreciate if you find some time to investigate suppressing this. Don't block this PR on that if it's more difficult than it seems -- if not immediately easy to fix, then could you file an issue and downgrade the suggestion from MachineApplicable to MaybeIncorrect? 😅

Ok. I will investigate it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #97311. Can I work on this problem in a new PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sounds good.

| ~~~~~~~~~~~
help: a local variable with a similar name exists
|
LL | println!("{cofig}");
| ~~~~~

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0425`.