@@ -4,9 +4,10 @@ use crate::diagnostics::{ImportSuggestion, LabelSuggestion, TypoSuggestion};
44use crate :: late:: { AliasPossibility , LateResolutionVisitor , RibKind } ;
55use crate :: late:: { LifetimeBinderKind , LifetimeRes , LifetimeRibKind , LifetimeUseSet } ;
66use crate :: ty:: fast_reject:: SimplifiedType ;
7- use crate :: { errors, path_names_to_string} ;
7+ use crate :: { errors, path_names_to_string, Resolver } ;
88use crate :: { Module , ModuleKind , ModuleOrUniformRoot } ;
99use crate :: { PathResult , PathSource , Segment } ;
10+ use rustc_attr:: collect_doc_alias_symbol_from_attrs;
1011use rustc_hir:: def:: Namespace :: { self , * } ;
1112
1213use rustc_ast:: ptr:: P ;
@@ -452,6 +453,18 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
452453 return ( err, Vec :: new ( ) ) ;
453454 }
454455
456+ if let Some ( did) = self . lookup_doc_alias_name ( path, source. namespace ( ) ) {
457+ err. span_label (
458+ self . r . def_span ( did) ,
459+ format ! (
460+ "`{}` has a name defined in the doc alias attribute as `{}`" ,
461+ self . r. tcx. item_name( did) ,
462+ path. last( ) . unwrap( ) . ident. as_str( )
463+ ) ,
464+ ) ;
465+ return ( err, Vec :: new ( ) ) ;
466+ } ;
467+
455468 let ( found, mut candidates) = self . try_lookup_name_relaxed (
456469 & mut err,
457470 source,
@@ -776,6 +789,50 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
776789 return ( false , candidates) ;
777790 }
778791
792+ fn lookup_doc_alias_name ( & mut self , path : & [ Segment ] , ns : Namespace ) -> Option < DefId > {
793+ let item_str = path. last ( ) . unwrap ( ) . ident ;
794+
795+ let find_doc_alias_name = |r : & mut Resolver < ' a , ' _ > , m : Module < ' a > | {
796+ for resolution in r. resolutions ( m) . borrow ( ) . values ( ) {
797+ let Some ( did) =
798+ resolution. borrow ( ) . binding . and_then ( |binding| binding. res ( ) . opt_def_id ( ) )
799+ else {
800+ continue ;
801+ } ;
802+ if did. is_local ( ) {
803+ // We don't record the doc alias name in the local crate
804+ // because the people who write doc alias are usually not
805+ // confused by them.
806+ continue ;
807+ }
808+ let symbols = collect_doc_alias_symbol_from_attrs ( r. tcx . get_attrs ( did, sym:: doc) ) ;
809+ if symbols. contains ( & item_str. name ) {
810+ return Some ( did) ;
811+ }
812+ }
813+ None
814+ } ;
815+
816+ if path. len ( ) == 1 {
817+ for rib in self . ribs [ ns] . iter ( ) . rev ( ) {
818+ if let RibKind :: Module ( module) = rib. kind
819+ && let Some ( did) = find_doc_alias_name ( self . r , module)
820+ {
821+ return Some ( did) ;
822+ }
823+ }
824+ } else {
825+ let mod_path = & path[ ..path. len ( ) - 1 ] ;
826+ if let PathResult :: Module ( ModuleOrUniformRoot :: Module ( module) ) =
827+ self . resolve_path ( mod_path, Some ( TypeNS ) , None )
828+ && let Some ( did) = find_doc_alias_name ( self . r , module)
829+ {
830+ return Some ( did) ;
831+ }
832+ }
833+ None
834+ }
835+
779836 fn suggest_trait_and_bounds (
780837 & mut self ,
781838 err : & mut Diag < ' _ > ,
0 commit comments