@@ -12,7 +12,8 @@ use rustc_hir::def_id::DefId;
1212use rustc_middle:: ty;
1313use rustc_resolve:: ParentScope ;
1414use rustc_session:: lint;
15- use rustc_span:: symbol:: { Ident , Symbol } ;
15+ use rustc_span:: symbol:: Ident ;
16+ use rustc_span:: symbol:: Symbol ;
1617use rustc_span:: DUMMY_SP ;
1718
1819use std:: ops:: Range ;
@@ -130,6 +131,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
130131 current_item : & Option < String > ,
131132 parent_id : Option < hir:: HirId > ,
132133 extra_fragment : & Option < String > ,
134+ item_opt : Option < & Item > ,
133135 ) -> Result < ( Res , Option < String > ) , ErrorKind > {
134136 let cx = self . cx ;
135137
@@ -230,16 +232,44 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
230232 DefKind :: Struct | DefKind :: Union | DefKind :: Enum | DefKind :: TyAlias ,
231233 did,
232234 ) => {
233- let item = cx
234- . tcx
235- . inherent_impls ( did)
236- . iter ( )
237- . flat_map ( |imp| cx. tcx . associated_items ( * imp) . in_definition_order ( ) )
238- . find ( |item| item. ident . name == item_name) ;
235+ // We need item's parent to know if it's
236+ // trait impl or struct/enum/etc impl
237+ let item_parent = item_opt
238+ . and_then ( |item| self . cx . as_local_hir_id ( item. def_id ) )
239+ . and_then ( |item_hir| {
240+ let parent_hir = self . cx . tcx . hir ( ) . get_parent_item ( item_hir) ;
241+ self . cx . tcx . hir ( ) . find ( parent_hir)
242+ } ) ;
243+ let item = match item_parent {
244+ Some ( hir:: Node :: Item ( hir:: Item {
245+ kind : hir:: ItemKind :: Impl { of_trait : Some ( _) , self_ty, .. } ,
246+ ..
247+ } ) ) => {
248+ // trait impl
249+ cx. tcx
250+ . associated_item_def_ids ( self_ty. hir_id . owner )
251+ . iter ( )
252+ . map ( |child| {
253+ let associated_item = cx. tcx . associated_item ( * child) ;
254+ associated_item
255+ } )
256+ . find ( |child| child. ident . name == item_name)
257+ }
258+ _ => {
259+ // struct/enum/etc. impl
260+ cx. tcx
261+ . inherent_impls ( did)
262+ . iter ( )
263+ . flat_map ( |imp| cx. tcx . associated_items ( * imp) . in_definition_order ( ) )
264+ . find ( |item| item. ident . name == item_name)
265+ }
266+ } ;
267+
239268 if let Some ( item) = item {
240269 let out = match item. kind {
241270 ty:: AssocKind :: Fn if ns == ValueNS => "method" ,
242271 ty:: AssocKind :: Const if ns == ValueNS => "associatedconstant" ,
272+ ty:: AssocKind :: Type if ns == ValueNS => "associatedtype" ,
243273 _ => return self . variant_field ( path_str, current_item, module_id) ,
244274 } ;
245275 if extra_fragment. is_some ( ) {
@@ -484,8 +514,14 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
484514
485515 match kind {
486516 Some ( ns @ ValueNS ) => {
487- match self . resolve ( path_str, ns, & current_item, base_node, & extra_fragment)
488- {
517+ match self . resolve (
518+ path_str,
519+ ns,
520+ & current_item,
521+ base_node,
522+ & extra_fragment,
523+ None ,
524+ ) {
489525 Ok ( res) => res,
490526 Err ( ErrorKind :: ResolutionFailure ) => {
491527 resolution_failure ( cx, & item, path_str, & dox, link_range) ;
@@ -501,8 +537,14 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
501537 }
502538 }
503539 Some ( ns @ TypeNS ) => {
504- match self . resolve ( path_str, ns, & current_item, base_node, & extra_fragment)
505- {
540+ match self . resolve (
541+ path_str,
542+ ns,
543+ & current_item,
544+ base_node,
545+ & extra_fragment,
546+ None ,
547+ ) {
506548 Ok ( res) => res,
507549 Err ( ErrorKind :: ResolutionFailure ) => {
508550 resolution_failure ( cx, & item, path_str, & dox, link_range) ;
@@ -526,6 +568,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
526568 & current_item,
527569 base_node,
528570 & extra_fragment,
571+ None ,
529572 ) {
530573 Err ( ErrorKind :: AnchorFailure ( msg) ) => {
531574 anchor_failure ( cx, & item, & ori_link, & dox, link_range, msg) ;
@@ -539,6 +582,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
539582 & current_item,
540583 base_node,
541584 & extra_fragment,
585+ Some ( & item) ,
542586 ) {
543587 Err ( ErrorKind :: AnchorFailure ( msg) ) => {
544588 anchor_failure ( cx, & item, & ori_link, & dox, link_range, msg) ;
0 commit comments