@@ -1744,15 +1744,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17441744 let child_doc_visible = doc_visible
17451745 && did. is_none_or ( |did| did. is_local ( ) || !this. tcx . is_doc_hidden ( did) ) ;
17461746
1747- // collect results based on the filter function
1748- // avoid suggesting anything from the same module in which we are resolving
1749- // avoid suggesting anything with a hygienic name
1750- if ident. name == lookup_ident. name
1751- && ns == namespace
1752- && in_module != parent_scope. module
1753- && ident. ctxt . is_root ( )
1754- && filter_fn ( res)
1755- {
1747+ let mk_path = |target_name| {
17561748 // create the path
17571749 let mut segms = if lookup_ident. span . at_least_rust_2018 ( ) {
17581750 // crate-local absolute paths start with `crate::` in edition 2018
@@ -1763,8 +1755,49 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17631755 } ;
17641756 segms. append ( & mut path_segments. clone ( ) ) ;
17651757
1766- segms. push ( ast:: PathSegment :: from_ident ( ident. orig ( orig_ident_span) ) ) ;
1767- let path = Path { span : name_binding. span , segments : segms } ;
1758+ segms. push ( ast:: PathSegment :: from_ident ( Ident :: new (
1759+ target_name,
1760+ orig_ident_span,
1761+ ) ) ) ;
1762+ Path { span : name_binding. span , segments : segms }
1763+ } ;
1764+ if let DeclKind :: Import { import, source_decl, .. } = name_binding. kind
1765+ && let ImportKind :: Single { source, target, .. } = import. kind
1766+ && source != target
1767+ && source. name == lookup_ident. name
1768+ && ns == namespace
1769+ && ident. ctxt . is_root ( )
1770+ && filter_fn ( res)
1771+ && this. is_accessible_from ( source_decl. vis ( ) , parent_scope. module )
1772+ && this. is_accessible_from ( name_binding. vis ( ) , parent_scope. module )
1773+ && !matches ! ( res, Res :: Err | Res :: SelfTyAlias { .. } | Res :: SelfTyParam { .. } )
1774+ && lookup_ident. name != kw:: SelfLower
1775+ && lookup_ident. name != kw:: SelfUpper
1776+ {
1777+ // Detect we've already imported the item with a different name.
1778+ candidates. push ( ImportSuggestion {
1779+ did,
1780+ descr : res. descr ( ) ,
1781+ path : mk_path ( target. name ) ,
1782+ accessible : child_accessible,
1783+ doc_visible : child_doc_visible,
1784+ note : None ,
1785+ via_import,
1786+ is_stable,
1787+ } ) ;
1788+ }
1789+
1790+ // collect results based on the filter function
1791+ // avoid suggesting anything from the same module in which we are resolving
1792+ // avoid suggesting anything with a hygienic name
1793+ if ident. name == lookup_ident. name
1794+ && ns == namespace
1795+ && in_module != parent_scope. module
1796+ && ident. ctxt . is_root ( )
1797+ && filter_fn ( res)
1798+ && !matches ! ( res, Res :: Err | Res :: SelfTyAlias { .. } | Res :: SelfTyParam { .. } )
1799+ {
1800+ let path = mk_path ( ident. name ) ;
17681801
17691802 if child_accessible
17701803 // Remove invisible match if exists
@@ -4105,19 +4138,34 @@ fn show_candidates(
41054138
41064139 let mut showed = false ;
41074140 let mut accessible_path_strings: Vec < PathString < ' _ > > = Vec :: new ( ) ;
4141+ let mut accessible_renames: Vec < PathString < ' _ > > = Vec :: new ( ) ;
41084142 let mut inaccessible_path_strings: Vec < PathString < ' _ > > = Vec :: new ( ) ;
41094143
41104144 candidates. iter ( ) . for_each ( |c| {
41114145 if c. accessible {
41124146 // Don't suggest `#[doc(hidden)]` items from other crates
41134147 if c. doc_visible {
4114- accessible_path_strings. push ( (
4115- pprust:: path_to_string ( & c. path ) ,
4116- c. descr ,
4117- c. did . and_then ( |did| Some ( tcx. source_span ( did. as_local ( ) ?) ) ) ,
4118- & c. note ,
4119- c. via_import ,
4120- ) )
4148+ if let [ first, .., last] | [ first @ last] = & path
4149+ && !first. ident . span . in_external_macro ( tcx. sess . source_map ( ) )
4150+ && first. ident . span . eq_ctxt ( last. ident . span )
4151+ && c. path . segments . len ( ) == 1
4152+ {
4153+ accessible_renames. push ( (
4154+ pprust:: path_to_string ( & c. path ) ,
4155+ c. descr ,
4156+ c. did . and_then ( |did| Some ( tcx. source_span ( did. as_local ( ) ?) ) ) ,
4157+ & c. note ,
4158+ c. via_import ,
4159+ ) ) ;
4160+ } else {
4161+ accessible_path_strings. push ( (
4162+ pprust:: path_to_string ( & c. path ) ,
4163+ c. descr ,
4164+ c. did . and_then ( |did| Some ( tcx. source_span ( did. as_local ( ) ?) ) ) ,
4165+ & c. note ,
4166+ c. via_import ,
4167+ ) ) ;
4168+ }
41214169 }
41224170 } else {
41234171 inaccessible_path_strings. push ( (
@@ -4126,13 +4174,15 @@ fn show_candidates(
41264174 c. did . and_then ( |did| Some ( tcx. source_span ( did. as_local ( ) ?) ) ) ,
41274175 & c. note ,
41284176 c. via_import ,
4129- ) )
4177+ ) ) ;
41304178 }
41314179 } ) ;
41324180
41334181 // we want consistent results across executions, but candidates are produced
41344182 // by iterating through a hash map, so make sure they are ordered:
4135- for path_strings in [ & mut accessible_path_strings, & mut inaccessible_path_strings] {
4183+ for path_strings in
4184+ [ & mut accessible_path_strings, & mut accessible_renames, & mut inaccessible_path_strings]
4185+ {
41364186 path_strings. sort_by ( |a, b| a. 0 . cmp ( & b. 0 ) ) ;
41374187 path_strings. dedup_by ( |a, b| a. 0 == b. 0 ) ;
41384188 let core_path_strings =
@@ -4155,6 +4205,39 @@ fn show_candidates(
41554205 path_strings. extend ( foreign_crate_path_strings) ;
41564206 }
41574207
4208+ if !accessible_renames. is_empty ( ) {
4209+ let ( determiner, kind, s, through) =
4210+ if let [ ( _, descr, _, _, via_import) ] = & accessible_renames[ ..] {
4211+ ( "this" , * descr, "" , if * via_import { " through its public re-export" } else { "" } )
4212+ } else {
4213+ // Get the unique item kinds and if there's only one, we use the right kind name
4214+ // instead of the more generic "items".
4215+ let kinds = accessible_path_strings
4216+ . iter ( )
4217+ . map ( |( _, descr, _, _, _) | * descr)
4218+ . collect :: < UnordSet < & str > > ( ) ;
4219+ let kind = if let Some ( kind) = kinds. get_only ( ) { kind } else { "item" } ;
4220+ let s = if kind. ends_with ( 's' ) { "es" } else { "s" } ;
4221+
4222+ ( "one of these" , kind, s, "" )
4223+ } ;
4224+
4225+ let instead = if let Instead :: Yes = instead { " instead" } else { "" } ;
4226+ let msg = format ! ( "consider using {determiner} {kind}{s}{through}{instead}" ) ;
4227+
4228+ for note in accessible_path_strings. iter ( ) . flat_map ( |cand| cand. 3 . as_ref ( ) ) {
4229+ err. note ( note. clone ( ) ) ;
4230+ }
4231+
4232+ err. span_suggestions_with_style (
4233+ path[ 0 ] . ident . span . to ( path[ path. len ( ) - 1 ] . ident . span ) ,
4234+ msg,
4235+ accessible_renames. into_iter ( ) . map ( |a| a. 0 ) ,
4236+ Applicability :: MaybeIncorrect ,
4237+ SuggestionStyle :: ShowAlways ,
4238+ ) ;
4239+ showed = true ;
4240+ }
41584241 if !accessible_path_strings. is_empty ( ) {
41594242 let ( determiner, kind, s, name, through) =
41604243 if let [ ( name, descr, _, _, via_import) ] = & accessible_path_strings[ ..] {
0 commit comments