@@ -431,6 +431,43 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
431
431
432
432
look_for_tests ( & cx, & dox, & item, true ) ;
433
433
434
+ // find item's parent to resolve `Self` in item's docs below
435
+ let parent_name = self . cx . as_local_hir_id ( item. def_id ) . and_then ( |item_hir| {
436
+ let parent_hir = self . cx . tcx . hir ( ) . get_parent_item ( item_hir) ;
437
+ let item_parent = self . cx . tcx . hir ( ) . find ( parent_hir) ;
438
+ match item_parent {
439
+ Some ( hir:: Node :: Item ( hir:: Item {
440
+ kind :
441
+ hir:: ItemKind :: Impl {
442
+ self_ty :
443
+ hir:: Ty {
444
+ kind :
445
+ hir:: TyKind :: Path ( hir:: QPath :: Resolved (
446
+ _,
447
+ hir:: Path { segments, .. } ,
448
+ ) ) ,
449
+ ..
450
+ } ,
451
+ ..
452
+ } ,
453
+ ..
454
+ } ) ) => segments. first ( ) . and_then ( |seg| Some ( seg. ident . to_string ( ) ) ) ,
455
+ Some ( hir:: Node :: Item ( hir:: Item {
456
+ ident, kind : hir:: ItemKind :: Enum ( ..) , ..
457
+ } ) )
458
+ | Some ( hir:: Node :: Item ( hir:: Item {
459
+ ident, kind : hir:: ItemKind :: Struct ( ..) , ..
460
+ } ) )
461
+ | Some ( hir:: Node :: Item ( hir:: Item {
462
+ ident, kind : hir:: ItemKind :: Union ( ..) , ..
463
+ } ) )
464
+ | Some ( hir:: Node :: Item ( hir:: Item {
465
+ ident, kind : hir:: ItemKind :: Trait ( ..) , ..
466
+ } ) ) => Some ( ident. to_string ( ) ) ,
467
+ _ => None ,
468
+ }
469
+ } ) ;
470
+
434
471
for ( ori_link, link_range) in markdown_links ( & dox) {
435
472
// Bail early for real links.
436
473
if ori_link. contains ( '/' ) {
@@ -467,7 +504,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
467
504
} ;
468
505
let ( res, fragment) = {
469
506
let mut kind = None ;
470
- let path_str = if let Some ( prefix) =
507
+ let mut path_str = if let Some ( prefix) =
471
508
[ "struct@" , "enum@" , "type@" , "trait@" , "union@" ]
472
509
. iter ( )
473
510
. find ( |p| link. starts_with ( * * p) )
@@ -521,6 +558,15 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
521
558
let base_node =
522
559
if item. is_mod ( ) && item. attrs . inner_docs { None } else { parent_node } ;
523
560
561
+ let resolved_self;
562
+ // replace `Self` with suitable item's parent name
563
+ if path_str. starts_with ( "Self::" ) {
564
+ if let Some ( ref name) = parent_name {
565
+ resolved_self = format ! ( "{}::{}" , name, & path_str[ 6 ..] ) ;
566
+ path_str = & resolved_self;
567
+ }
568
+ }
569
+
524
570
match kind {
525
571
Some ( ns @ ValueNS ) => {
526
572
match self . resolve (
@@ -529,7 +575,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
529
575
& current_item,
530
576
base_node,
531
577
& extra_fragment,
532
- None ,
578
+ Some ( & item ) ,
533
579
) {
534
580
Ok ( res) => res,
535
581
Err ( ErrorKind :: ResolutionFailure ) => {
@@ -552,7 +598,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
552
598
& current_item,
553
599
base_node,
554
600
& extra_fragment,
555
- None ,
601
+ Some ( & item ) ,
556
602
) {
557
603
Ok ( res) => res,
558
604
Err ( ErrorKind :: ResolutionFailure ) => {
@@ -577,7 +623,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
577
623
& current_item,
578
624
base_node,
579
625
& extra_fragment,
580
- None ,
626
+ Some ( & item ) ,
581
627
) {
582
628
Err ( ErrorKind :: AnchorFailure ( msg) ) => {
583
629
anchor_failure ( cx, & item, & ori_link, & dox, link_range, msg) ;
0 commit comments