-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Suggest similar names for types #96839
Changes from all commits
be0501f
8788df5
9e75730
63c4d1e
85acc8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -28,7 +28,7 @@ use rustc_span::{BytePos, Span}; | |||
use tracing::debug; | ||||
|
||||
use crate::imports::{Import, ImportKind, ImportResolver}; | ||||
use crate::late::{PatternSource, Rib}; | ||||
use crate::late::{PatternSource, Rib, RibKind}; | ||||
use crate::path_names_to_string; | ||||
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, Finalize}; | ||||
use crate::{HasGenericParams, MacroRulesScope, Module, ModuleKind, ModuleOrUniformRoot}; | ||||
|
@@ -1825,6 +1825,76 @@ impl<'a> Resolver<'a> { | |||
} | ||||
} | ||||
|
||||
fn find_similarly_named_type( | ||||
&mut self, | ||||
ident: Symbol, | ||||
ribs: Option<&PerNS<Vec<Rib<'a>>>>, | ||||
) -> Option<TypoSuggestion> { | ||||
fn is_type_candidate(res: Res) -> bool { | ||||
matches!( | ||||
res, | ||||
Res::Def( | ||||
DefKind::Struct | ||||
| DefKind::Union | ||||
| DefKind::Enum | ||||
| DefKind::Trait | ||||
| DefKind::TraitAlias | ||||
| DefKind::TyAlias | ||||
| DefKind::AssocTy | ||||
| DefKind::TyParam | ||||
| DefKind::OpaqueTy | ||||
| DefKind::ForeignTy, | ||||
_ | ||||
) | Res::PrimTy(..) | ||||
| Res::SelfTy { .. } | ||||
) | ||||
} | ||||
|
||||
let mut names = Vec::new(); | ||||
if let Some(ribs) = ribs { | ||||
// Search in lexical scope. | ||||
// Walk backwards up the ribs in scope and collect candidates. | ||||
for rib in ribs[TypeNS].iter().rev() { | ||||
Comment on lines
+1853
to
+1857
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. I implemented the logics after this line with reference to the following function:
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. Any way these could be merged or share logic? Might be worth leaving a comment at the very least. |
||||
for (ident, res) in &rib.bindings { | ||||
if is_type_candidate(*res) { | ||||
names.push(TypoSuggestion::typo_from_res(ident.name, *res)); | ||||
} | ||||
} | ||||
// Items in scope | ||||
if let RibKind::ModuleRibKind(module) = rib.kind { | ||||
// Items from this module | ||||
self.add_module_candidates(module, &mut names, &is_type_candidate); | ||||
|
||||
if let ModuleKind::Block(..) = module.kind { | ||||
// We can through blocks | ||||
} else { | ||||
// Items from the prelude | ||||
if !module.no_implicit_prelude { | ||||
if let Some(prelude) = self.prelude { | ||||
self.add_module_candidates(prelude, &mut names, &is_type_candidate); | ||||
} | ||||
} | ||||
break; | ||||
} | ||||
} | ||||
} | ||||
// Add primitive types | ||||
names.extend(PrimTy::ALL.iter().map(|prim_ty| { | ||||
TypoSuggestion::typo_from_res(prim_ty.name(), Res::PrimTy(*prim_ty)) | ||||
})) | ||||
} | ||||
|
||||
// Make sure the suggestion is deterministic. | ||||
names.sort_by(|a, b| a.candidate.as_str().partial_cmp(b.candidate.as_str()).unwrap()); | ||||
let symbols = names.iter().map(|sugg| sugg.candidate).collect::<Vec<Symbol>>(); | ||||
|
||||
match find_best_match_for_name(&symbols, ident, None) { | ||||
Some(sugg) if sugg == ident => None, | ||||
sugg => sugg, | ||||
} | ||||
.and_then(|sugg| names.into_iter().find(|name| name.candidate == sugg)) | ||||
} | ||||
|
||||
pub(crate) fn report_path_resolution_error( | ||||
&mut self, | ||||
path: &[Segment], | ||||
|
@@ -1990,7 +2060,17 @@ impl<'a> Resolver<'a> { | |||
Applicability::MaybeIncorrect, | ||||
)) | ||||
} else { | ||||
None | ||||
self.find_similarly_named_type(ident.name, ribs).map(|sugg| { | ||||
( | ||||
vec![(ident.span, sugg.candidate.to_string())], | ||||
format!( | ||||
"there is {} {} with a similar name", | ||||
sugg.res.article(), | ||||
sugg.res.descr(), | ||||
), | ||||
Applicability::MaybeIncorrect, | ||||
) | ||||
}) | ||||
}; | ||||
|
||||
(format!("use of undeclared type `{}`", ident), suggestion) | ||||
|
@@ -2001,16 +2081,26 @@ impl<'a> Resolver<'a> { | |||
String::from("add `extern crate alloc` to use the `alloc` crate"), | ||||
Applicability::MaybeIncorrect, | ||||
)) | ||||
} else if let Some(sugg) = | ||||
self.find_similarly_named_module_or_crate(ident.name, &parent_scope.module) | ||||
{ | ||||
Some(( | ||||
vec![(ident.span, sugg.to_string())], | ||||
String::from("there is a crate or module with a similar name"), | ||||
jackh726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
Applicability::MaybeIncorrect, | ||||
)) | ||||
} else { | ||||
self.find_similarly_named_module_or_crate(ident.name, &parent_scope.module).map( | ||||
|sugg| { | ||||
( | ||||
vec![(ident.span, sugg.to_string())], | ||||
String::from("there is a crate or module with a similar name"), | ||||
Applicability::MaybeIncorrect, | ||||
) | ||||
}, | ||||
) | ||||
self.find_similarly_named_type(ident.name, ribs).map(|sugg| { | ||||
( | ||||
vec![(ident.span, sugg.candidate.to_string())], | ||||
format!( | ||||
"there is {} {} with a similar name", | ||||
sugg.res.article(), | ||||
sugg.res.descr() | ||||
), | ||||
Applicability::MaybeIncorrect, | ||||
) | ||||
}) | ||||
}; | ||||
(format!("use of undeclared crate or module `{}`", ident), suggestion) | ||||
} | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
struct FooType {} | ||
|
||
impl FooType { | ||
fn bar() {} | ||
} | ||
|
||
fn main() { | ||
FooTyp::bar() | ||
//~^ ERROR failed to resolve: use of undeclared type `FooTyp` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0433]: failed to resolve: use of undeclared type `FooTyp` | ||
--> $DIR/suggest-similar-type-name.rs:8:5 | ||
| | ||
LL | FooTyp::bar() | ||
| ^^^^^^ | ||
| | | ||
| use of undeclared type `FooTyp` | ||
| help: there is a struct with a similar name: `FooType` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0433`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Beginners often write an incorrect type name whose initial letter is | ||
// a lowercase while the correct one is an uppercase. | ||
// (e.g. `string` instead of `String`) | ||
// This tests that we suggest the latter when we encounter the former. | ||
|
||
fn main() { | ||
let _ = string::new(); | ||
//~^ ERROR failed to resolve: use of undeclared crate or module `string` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0433]: failed to resolve: use of undeclared crate or module `string` | ||
--> $DIR/suggest-type-to-lowercase-path.rs:7:13 | ||
| | ||
LL | let _ = string::new(); | ||
| ^^^^^^ | ||
| | | ||
| use of undeclared crate or module `string` | ||
| help: there is a struct with a similar name (notice the capitalization): `String` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0433`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need this?