@@ -11,6 +11,7 @@ use rustc_ast::{
1111 Item , ItemKind , MethodCall , NodeId , Path , Ty , TyKind ,
1212} ;
1313use rustc_ast_pretty:: pprust:: where_bound_predicate_to_string;
14+ use rustc_attr:: collect_doc_alias_symbol_from_attrs;
1415use rustc_data_structures:: fx:: { FxHashSet , FxIndexSet } ;
1516use rustc_errors:: codes:: * ;
1617use rustc_errors:: {
@@ -40,7 +41,7 @@ use crate::late::{
4041} ;
4142use crate :: ty:: fast_reject:: SimplifiedType ;
4243use crate :: {
43- Module , ModuleKind , ModuleOrUniformRoot , PathResult , PathSource , Segment , errors,
44+ Module , ModuleKind , ModuleOrUniformRoot , PathResult , PathSource , Resolver , Segment , errors,
4445 path_names_to_string,
4546} ;
4647
@@ -459,6 +460,18 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
459460 return ( err, Vec :: new ( ) ) ;
460461 }
461462
463+ if let Some ( did) = self . lookup_doc_alias_name ( path, source. namespace ( ) ) {
464+ err. span_label (
465+ self . r . def_span ( did) ,
466+ format ! (
467+ "`{}` has a name defined in the doc alias attribute as `{}`" ,
468+ self . r. tcx. item_name( did) ,
469+ path. last( ) . unwrap( ) . ident. as_str( )
470+ ) ,
471+ ) ;
472+ return ( err, Vec :: new ( ) ) ;
473+ } ;
474+
462475 let ( found, mut candidates) = self . try_lookup_name_relaxed (
463476 & mut err,
464477 source,
@@ -783,6 +796,50 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
783796 ( false , candidates)
784797 }
785798
799+ fn lookup_doc_alias_name ( & mut self , path : & [ Segment ] , ns : Namespace ) -> Option < DefId > {
800+ let item_str = path. last ( ) . unwrap ( ) . ident ;
801+
802+ let find_doc_alias_name = |r : & mut Resolver < ' ra , ' _ > , m : Module < ' ra > | {
803+ for resolution in r. resolutions ( m) . borrow ( ) . values ( ) {
804+ let Some ( did) =
805+ resolution. borrow ( ) . binding . and_then ( |binding| binding. res ( ) . opt_def_id ( ) )
806+ else {
807+ continue ;
808+ } ;
809+ if did. is_local ( ) {
810+ // We don't record the doc alias name in the local crate
811+ // because the people who write doc alias are usually not
812+ // confused by them.
813+ continue ;
814+ }
815+ let symbols = collect_doc_alias_symbol_from_attrs ( r. tcx . get_attrs ( did, sym:: doc) ) ;
816+ if symbols. contains ( & item_str. name ) {
817+ return Some ( did) ;
818+ }
819+ }
820+ None
821+ } ;
822+
823+ if path. len ( ) == 1 {
824+ for rib in self . ribs [ ns] . iter ( ) . rev ( ) {
825+ if let RibKind :: Module ( module) = rib. kind
826+ && let Some ( did) = find_doc_alias_name ( self . r , module)
827+ {
828+ return Some ( did) ;
829+ }
830+ }
831+ } else {
832+ let mod_path = & path[ ..path. len ( ) - 1 ] ;
833+ if let PathResult :: Module ( ModuleOrUniformRoot :: Module ( module) ) =
834+ self . resolve_path ( mod_path, Some ( TypeNS ) , None )
835+ && let Some ( did) = find_doc_alias_name ( self . r , module)
836+ {
837+ return Some ( did) ;
838+ }
839+ }
840+ None
841+ }
842+
786843 fn suggest_trait_and_bounds (
787844 & mut self ,
788845 err : & mut Diag < ' _ > ,
0 commit comments