@@ -12,7 +12,8 @@ use rustc_hir::def_id::DefId;
12
12
use rustc_middle:: ty;
13
13
use rustc_resolve:: ParentScope ;
14
14
use rustc_session:: lint;
15
- use rustc_span:: symbol:: { Ident , Symbol } ;
15
+ use rustc_span:: symbol:: Ident ;
16
+ use rustc_span:: symbol:: Symbol ;
16
17
use rustc_span:: DUMMY_SP ;
17
18
18
19
use std:: ops:: Range ;
@@ -130,6 +131,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
130
131
current_item : & Option < String > ,
131
132
parent_id : Option < hir:: HirId > ,
132
133
extra_fragment : & Option < String > ,
134
+ item_opt : Option < & Item > ,
133
135
) -> Result < ( Res , Option < String > ) , ErrorKind > {
134
136
let cx = self . cx ;
135
137
@@ -230,16 +232,44 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
230
232
DefKind :: Struct | DefKind :: Union | DefKind :: Enum | DefKind :: TyAlias ,
231
233
did,
232
234
) => {
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
+
239
268
if let Some ( item) = item {
240
269
let out = match item. kind {
241
270
ty:: AssocKind :: Fn if ns == ValueNS => "method" ,
242
271
ty:: AssocKind :: Const if ns == ValueNS => "associatedconstant" ,
272
+ ty:: AssocKind :: Type if ns == ValueNS => "associatedtype" ,
243
273
_ => return self . variant_field ( path_str, current_item, module_id) ,
244
274
} ;
245
275
if extra_fragment. is_some ( ) {
@@ -484,8 +514,14 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
484
514
485
515
match kind {
486
516
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
+ ) {
489
525
Ok ( res) => res,
490
526
Err ( ErrorKind :: ResolutionFailure ) => {
491
527
resolution_failure ( cx, & item, path_str, & dox, link_range) ;
@@ -501,8 +537,14 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
501
537
}
502
538
}
503
539
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
+ ) {
506
548
Ok ( res) => res,
507
549
Err ( ErrorKind :: ResolutionFailure ) => {
508
550
resolution_failure ( cx, & item, path_str, & dox, link_range) ;
@@ -526,6 +568,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
526
568
& current_item,
527
569
base_node,
528
570
& extra_fragment,
571
+ None ,
529
572
) {
530
573
Err ( ErrorKind :: AnchorFailure ( msg) ) => {
531
574
anchor_failure ( cx, & item, & ori_link, & dox, link_range, msg) ;
@@ -539,6 +582,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
539
582
& current_item,
540
583
base_node,
541
584
& extra_fragment,
585
+ Some ( & item) ,
542
586
) {
543
587
Err ( ErrorKind :: AnchorFailure ( msg) ) => {
544
588
anchor_failure ( cx, & item, & ori_link, & dox, link_range, msg) ;
0 commit comments