-
Notifications
You must be signed in to change notification settings - Fork 13.4k
WIP: Suggest similar type or module on resolve failure #68850
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a3ec43f
Suggest similar type or module on resolve failure
phansch 242dab6
Move suggestion building to librustc_resolve/diagnostics.rs
phansch 8e0cf40
Remove tests testing Levenshtein distance
phansch 8a468d2
Re-use typo suggestion wording
phansch 9863a9b
Add error annotations for existing tests
phansch 3c6a203
Fix suggestion inside module
phansch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -797,6 +797,14 @@ impl<'a> Resolver<'a> { | |
} | ||
} | ||
|
||
fn typo_suggestion_text(&self, suggestion: &TypoSuggestion) -> String { | ||
format!( | ||
"{} {} with a similar name exists", | ||
suggestion.res.article(), | ||
suggestion.res.descr() | ||
) | ||
} | ||
|
||
crate fn add_typo_suggestion( | ||
&self, | ||
err: &mut DiagnosticBuilder<'_>, | ||
|
@@ -809,14 +817,9 @@ impl<'a> Resolver<'a> { | |
return false; | ||
} | ||
|
||
let msg = format!( | ||
"{} {} with a similar name exists", | ||
suggestion.res.article(), | ||
suggestion.res.descr() | ||
); | ||
err.span_suggestion( | ||
span, | ||
&msg, | ||
&self.typo_suggestion_text(&suggestion), | ||
suggestion.candidate.to_string(), | ||
Applicability::MaybeIncorrect, | ||
); | ||
|
@@ -999,6 +1002,32 @@ impl<'a> Resolver<'a> { | |
|
||
err.emit(); | ||
} | ||
|
||
crate fn make_undeclared_type_suggestion( | ||
&mut self, | ||
ident: Ident, | ||
parent_scope: &ParentScope<'a>, | ||
ns: Namespace, | ||
) -> (String, Option<Suggestion>) { | ||
let typo_suggestion = self.early_lookup_typo_candidate( | ||
ScopeSet::All(ns, false), | ||
parent_scope, | ||
ident, | ||
&|_| true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use |
||
); | ||
if let Some(typo_suggestion) = typo_suggestion { | ||
petrochenkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
( | ||
format!("use of undeclared type or module `{}`", ident), | ||
Some(( | ||
vec![(ident.span, format!("{}", typo_suggestion.candidate.as_str()))], | ||
self.typo_suggestion_text(&typo_suggestion), | ||
Applicability::MaybeIncorrect, | ||
)), | ||
) | ||
} else { | ||
(format!("use of undeclared type or module `{}`", ident), None) | ||
} | ||
} | ||
} | ||
|
||
impl<'a, 'b> ImportResolver<'a, 'b> { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::ffi::CString; | ||
|
||
mod foo { | ||
use std::collections::HashMap; | ||
|
||
fn bar() { | ||
let _ = HashNap::new(); | ||
//~^ ERROR failed to resolve: use of undeclared type or module `HashNap` | ||
//~| HELP a struct with a similar name exists | ||
//~| SUGGESTION HashMap | ||
} | ||
} | ||
|
||
fn main() { | ||
let _ = Cstring::new("hello").unwrap(); | ||
//~^ ERROR failed to resolve: use of undeclared type or module `Cstring` | ||
//~| HELP a struct with a similar name exists | ||
//~| SUGGESTION CString | ||
|
||
let _ = foO::bar(); | ||
//~^ ERROR failed to resolve: use of undeclared type or module `foO` | ||
//~| HELP a module with a similar name exists | ||
//~| SUGGESTION foo | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
error[E0433]: failed to resolve: use of undeclared type or module `HashNap` | ||
--> $DIR/suggest-type.rs:7:17 | ||
| | ||
LL | let _ = HashNap::new(); | ||
| ^^^^^^^ | ||
| | | ||
| use of undeclared type or module `HashNap` | ||
| help: a struct with a similar name exists: `HashMap` | ||
|
||
error[E0433]: failed to resolve: use of undeclared type or module `Cstring` | ||
--> $DIR/suggest-type.rs:15:13 | ||
| | ||
LL | let _ = Cstring::new("hello").unwrap(); | ||
| ^^^^^^^ | ||
| | | ||
| use of undeclared type or module `Cstring` | ||
| help: a struct with a similar name exists (notice the capitalization): `CString` | ||
|
||
error[E0433]: failed to resolve: use of undeclared type or module `foO` | ||
--> $DIR/suggest-type.rs:20:13 | ||
| | ||
LL | let _ = foO::bar(); | ||
| ^^^ | ||
| | | ||
| use of undeclared type or module `foO` | ||
| help: a module with a similar name exists (notice the capitalization): `foo` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0433`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.