@@ -460,7 +460,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
460460 return ( err, Vec :: new ( ) ) ;
461461 }
462462
463- let ( found, candidates) = self . try_lookup_name_relaxed (
463+ let ( found, mut candidates) = self . try_lookup_name_relaxed (
464464 & mut err,
465465 source,
466466 path,
@@ -473,11 +473,14 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
473473 return ( err, candidates) ;
474474 }
475475
476- let mut fallback = self . suggest_trait_and_bounds ( & mut err, source, res, span, & base_error) ;
476+ if self . suggest_shadowed ( & mut err, source, path, following_seg, span) {
477+ // if there is already a shadowed name, don'suggest candidates for importing
478+ candidates. clear ( ) ;
479+ }
477480
478- // if we have suggested using pattern matching, then don't add needless suggestions
479- // for typos.
480- fallback |= self . suggest_typo ( & mut err, source, path, following_seg, span, & base_error) ;
481+ // if we have suggested using pattern matching, then don't add needless suggestions for typos.
482+ let fallback = self . suggest_trait_and_bounds ( & mut err , source , res , span , & base_error )
483+ || self . suggest_typo ( & mut err, source, path, following_seg, span, & base_error) ;
481484
482485 if fallback {
483486 // Fallback label.
@@ -872,25 +875,6 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
872875 let ident_span = path. last ( ) . map_or ( span, |ident| ident. ident . span ) ;
873876 let typo_sugg =
874877 self . lookup_typo_candidate ( path, following_seg, source. namespace ( ) , is_expected) ;
875- let is_in_same_file = & |sp1, sp2| {
876- let source_map = self . r . tcx . sess . source_map ( ) ;
877- let file1 = source_map. span_to_filename ( sp1) ;
878- let file2 = source_map. span_to_filename ( sp2) ;
879- file1 == file2
880- } ;
881- // print 'you might have meant' if the candidate is (1) is a shadowed name with
882- // accessible definition and (2) either defined in the same crate as the typo
883- // (could be in a different file) or introduced in the same file as the typo
884- // (could belong to a different crate)
885- if let TypoCandidate :: Shadowed ( res, Some ( sugg_span) ) = typo_sugg
886- && res. opt_def_id ( ) . is_some_and ( |id| id. is_local ( ) || is_in_same_file ( span, sugg_span) )
887- {
888- err. span_label (
889- sugg_span,
890- format ! ( "you might have meant to refer to this {}" , res. descr( ) ) ,
891- ) ;
892- return true ;
893- }
894878 let mut fallback = false ;
895879 let typo_sugg = typo_sugg. to_opt_suggestion ( ) ;
896880 if !self . r . add_typo_suggestion ( err, typo_sugg, ident_span) {
@@ -918,6 +902,39 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
918902 fallback
919903 }
920904
905+ fn suggest_shadowed (
906+ & mut self ,
907+ err : & mut Diagnostic ,
908+ source : PathSource < ' _ > ,
909+ path : & [ Segment ] ,
910+ following_seg : Option < & Segment > ,
911+ span : Span ,
912+ ) -> bool {
913+ let is_expected = & |res| source. is_expected ( res) ;
914+ let typo_sugg =
915+ self . lookup_typo_candidate ( path, following_seg, source. namespace ( ) , is_expected) ;
916+ let is_in_same_file = & |sp1, sp2| {
917+ let source_map = self . r . tcx . sess . source_map ( ) ;
918+ let file1 = source_map. span_to_filename ( sp1) ;
919+ let file2 = source_map. span_to_filename ( sp2) ;
920+ file1 == file2
921+ } ;
922+ // print 'you might have meant' if the candidate is (1) is a shadowed name with
923+ // accessible definition and (2) either defined in the same crate as the typo
924+ // (could be in a different file) or introduced in the same file as the typo
925+ // (could belong to a different crate)
926+ if let TypoCandidate :: Shadowed ( res, Some ( sugg_span) ) = typo_sugg
927+ && res. opt_def_id ( ) . is_some_and ( |id| id. is_local ( ) || is_in_same_file ( span, sugg_span) )
928+ {
929+ err. span_label (
930+ sugg_span,
931+ format ! ( "you might have meant to refer to this {}" , res. descr( ) ) ,
932+ ) ;
933+ return true ;
934+ }
935+ false
936+ }
937+
921938 fn err_code_special_cases (
922939 & mut self ,
923940 err : & mut Diagnostic ,
0 commit comments