@@ -24,7 +24,7 @@ use rustc_span::hygiene::MacroKind;
2424use rustc_span:: lev_distance:: find_best_match_for_name;
2525use rustc_span:: source_map:: SourceMap ;
2626use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
27- use rustc_span:: { BytePos , Span } ;
27+ use rustc_span:: { BytePos , Span , SyntaxContext } ;
2828
2929use crate :: imports:: { Import , ImportKind , ImportResolver } ;
3030use crate :: late:: { PatternSource , Rib } ;
@@ -47,13 +47,15 @@ pub(crate) type Suggestion = (Vec<(Span, String)>, String, Applicability);
4747/// similarly named label and whether or not it is reachable.
4848pub ( crate ) type LabelSuggestion = ( Ident , bool ) ;
4949
50+ #[ derive( Debug ) ]
5051pub ( crate ) enum SuggestionTarget {
5152 /// The target has a similar name as the name used by the programmer (probably a typo)
5253 SimilarlyNamed ,
5354 /// The target is the only valid item that can be used in the corresponding context
5455 SingleItem ,
5556}
5657
58+ #[ derive( Debug ) ]
5759pub ( crate ) struct TypoSuggestion {
5860 pub candidate : Symbol ,
5961 pub res : Res ,
@@ -482,11 +484,12 @@ impl<'a> Resolver<'a> {
482484 module : Module < ' a > ,
483485 names : & mut Vec < TypoSuggestion > ,
484486 filter_fn : & impl Fn ( Res ) -> bool ,
487+ ctxt : Option < SyntaxContext > ,
485488 ) {
486489 for ( key, resolution) in self . resolutions ( module) . borrow ( ) . iter ( ) {
487490 if let Some ( binding) = resolution. borrow ( ) . binding {
488491 let res = binding. res ( ) ;
489- if filter_fn ( res) {
492+ if filter_fn ( res) && ctxt . map_or ( true , |ctxt| ctxt == key . ident . span . ctxt ( ) ) {
490493 names. push ( TypoSuggestion :: typo_from_res ( key. ident . name , res) ) ;
491494 }
492495 }
@@ -1181,10 +1184,10 @@ impl<'a> Resolver<'a> {
11811184 Scope :: CrateRoot => {
11821185 let root_ident = Ident :: new ( kw:: PathRoot , ident. span ) ;
11831186 let root_module = this. resolve_crate_root ( root_ident) ;
1184- this. add_module_candidates ( root_module, & mut suggestions, filter_fn) ;
1187+ this. add_module_candidates ( root_module, & mut suggestions, filter_fn, None ) ;
11851188 }
11861189 Scope :: Module ( module, _) => {
1187- this. add_module_candidates ( module, & mut suggestions, filter_fn) ;
1190+ this. add_module_candidates ( module, & mut suggestions, filter_fn, None ) ;
11881191 }
11891192 Scope :: MacroUsePrelude => {
11901193 suggestions. extend ( this. macro_use_prelude . iter ( ) . filter_map (
@@ -1221,7 +1224,7 @@ impl<'a> Resolver<'a> {
12211224 Scope :: StdLibPrelude => {
12221225 if let Some ( prelude) = this. prelude {
12231226 let mut tmp_suggestions = Vec :: new ( ) ;
1224- this. add_module_candidates ( prelude, & mut tmp_suggestions, filter_fn) ;
1227+ this. add_module_candidates ( prelude, & mut tmp_suggestions, filter_fn, None ) ;
12251228 suggestions. extend (
12261229 tmp_suggestions
12271230 . into_iter ( )
0 commit comments