@@ -102,7 +102,6 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
102
102
fn variant_field (
103
103
& self ,
104
104
path_str : & ' path str ,
105
- current_item : & Option < String > ,
106
105
module_id : DefId ,
107
106
) -> Result < ( Res , Option < String > ) , ErrorKind < ' path > > {
108
107
let cx = self . cx ;
@@ -125,14 +124,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
125
124
split. next ( ) . map ( |f| ( f, Symbol :: intern ( f) ) ) . ok_or_else ( no_res) ?;
126
125
let path = split
127
126
. next ( )
128
- . map ( |f| {
129
- if f == "self" || f == "Self" {
130
- if let Some ( name) = current_item. as_ref ( ) {
131
- return name. clone ( ) ;
132
- }
133
- }
134
- f. to_owned ( )
135
- } )
127
+ . map ( |f| f. to_owned ( ) )
136
128
// If there's no third component, we saw `[a::b]` before and it failed to resolve.
137
129
// So there's no partial res.
138
130
. ok_or_else ( no_res) ?;
@@ -237,7 +229,6 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
237
229
& self ,
238
230
path_str : & ' path str ,
239
231
ns : Namespace ,
240
- current_item : & Option < String > ,
241
232
module_id : DefId ,
242
233
extra_fragment : & Option < String > ,
243
234
) -> Result < ( Res , Option < String > ) , ErrorKind < ' path > > {
@@ -296,14 +287,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
296
287
let ( item_str, item_name) = split. next ( ) . map ( |i| ( i, Symbol :: intern ( i) ) ) . unwrap ( ) ;
297
288
let path_root = split
298
289
. next ( )
299
- . map ( |f| {
300
- if f == "self" || f == "Self" {
301
- if let Some ( name) = current_item. as_ref ( ) {
302
- return name. clone ( ) ;
303
- }
304
- }
305
- f. to_owned ( )
306
- } )
290
+ . map ( |f| f. to_owned ( ) )
307
291
// If there's no `::`, it's not an associated item.
308
292
// So we can be sure that `rustc_resolve` was accurate when it said it wasn't resolved.
309
293
. ok_or_else ( || {
@@ -365,7 +349,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
365
349
let ty_res = match ty_res {
366
350
Err ( ( ) ) | Ok ( Res :: Err ) => {
367
351
return if ns == Namespace :: ValueNS {
368
- self . variant_field ( path_str, current_item , module_id)
352
+ self . variant_field ( path_str, module_id)
369
353
} else {
370
354
Err ( ResolutionFailure :: NotResolved {
371
355
module_id,
@@ -501,7 +485,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
501
485
} ;
502
486
res. unwrap_or_else ( || {
503
487
if ns == Namespace :: ValueNS {
504
- self . variant_field ( path_str, current_item , module_id)
488
+ self . variant_field ( path_str, module_id)
505
489
} else {
506
490
Err ( ResolutionFailure :: NotResolved {
507
491
module_id,
@@ -524,7 +508,6 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
524
508
ns : Namespace ,
525
509
path_str : & str ,
526
510
module_id : DefId ,
527
- current_item : & Option < String > ,
528
511
extra_fragment : & Option < String > ,
529
512
) -> Option < Res > {
530
513
let check_full_res_inner = |this : & Self , result : Result < Res , ErrorKind < ' _ > > | {
@@ -540,7 +523,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
540
523
} ;
541
524
// cannot be used for macro namespace
542
525
let check_full_res = |this : & Self , ns| {
543
- let result = this. resolve ( path_str, ns, current_item , module_id, extra_fragment) ;
526
+ let result = this. resolve ( path_str, ns, module_id, extra_fragment) ;
544
527
check_full_res_inner ( this, result. map ( |( res, _) | res) )
545
528
} ;
546
529
let check_full_res_macro = |this : & Self | {
@@ -736,32 +719,40 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
736
719
trace ! ( "got parent node for {:?} {:?}, id {:?}" , item. type_( ) , item. name, item. def_id) ;
737
720
}
738
721
739
- let current_item = match item. inner {
740
- ModuleItem ( ..) => {
741
- if item. attrs . inner_docs {
742
- if item. def_id . is_top_level_module ( ) { item. name . clone ( ) } else { None }
743
- } else {
744
- match parent_node. or ( self . mod_ids . last ( ) . copied ( ) ) {
745
- Some ( parent) if !parent. is_top_level_module ( ) => {
746
- // FIXME: can we pull the parent module's name from elsewhere?
747
- Some ( self . cx . tcx . item_name ( parent) . to_string ( ) )
748
- }
749
- _ => None ,
750
- }
751
- }
752
- }
753
- ImplItem ( Impl { ref for_, .. } ) => {
754
- for_. def_id ( ) . map ( |did| self . cx . tcx . item_name ( did) . to_string ( ) )
755
- }
756
- // we don't display docs on `extern crate` items anyway, so don't process them.
757
- ExternCrateItem ( ..) => {
758
- debug ! ( "ignoring extern crate item {:?}" , item. def_id) ;
759
- return self . fold_item_recur ( item) ;
760
- }
761
- ImportItem ( Import :: Simple ( ref name, ..) ) => Some ( name. clone ( ) ) ,
762
- MacroItem ( ..) => None ,
763
- _ => item. name . clone ( ) ,
722
+ // find item's parent to resolve `Self` in item's docs below
723
+ debug ! ( "looking for the `Self` type" ) ;
724
+ let self_id = if item. is_fake ( ) {
725
+ None
726
+ } else if matches ! (
727
+ self . cx. tcx. def_kind( item. def_id) ,
728
+ DefKind :: AssocConst
729
+ | DefKind :: AssocFn
730
+ | DefKind :: AssocTy
731
+ | DefKind :: Variant
732
+ | DefKind :: Field
733
+ ) {
734
+ self . cx . tcx . parent ( item. def_id )
735
+ // HACK(jynelson): `clean` marks associated types as `TypedefItem`, not as `AssocTypeItem`.
736
+ // Fixing this breaks `fn render_deref_methods`.
737
+ // As a workaround, see if the parent of the item is an `impl`; if so this must be an associated item,
738
+ // regardless of what rustdoc wants to call it.
739
+ } else if let Some ( parent) = self . cx . tcx . parent ( item. def_id ) {
740
+ let parent_kind = self . cx . tcx . def_kind ( parent) ;
741
+ Some ( if parent_kind == DefKind :: Impl { parent } else { item. def_id } )
742
+ } else {
743
+ // FIXME: this should really be `Some(item.def_id)`, but for some reason that panics in `opt_item_name`
744
+ None
764
745
} ;
746
+ let self_name = self_id. and_then ( |self_id| {
747
+ if matches ! ( self . cx. tcx. def_kind( self_id) , DefKind :: Impl ) {
748
+ debug ! ( "using type_of()" ) ;
749
+ // NOTE: uses Debug to avoid shortening paths
750
+ Some ( format ! ( "{:?}" , self . cx. tcx. type_of( self_id) ) )
751
+ } else {
752
+ debug ! ( "using item_name()" ) ;
753
+ self . cx . tcx . opt_item_name ( self_id) . map ( |sym| sym. to_string ( ) )
754
+ }
755
+ } ) ;
765
756
766
757
if item. is_mod ( ) && item. attrs . inner_docs {
767
758
self . mod_ids . push ( item. def_id ) ;
@@ -770,53 +761,8 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
770
761
let dox = item. attrs . collapsed_doc_value ( ) . unwrap_or_else ( String :: new) ;
771
762
trace ! ( "got documentation '{}'" , dox) ;
772
763
773
- // find item's parent to resolve `Self` in item's docs below
774
- let parent_name = self . cx . as_local_hir_id ( item. def_id ) . and_then ( |item_hir| {
775
- let parent_hir = self . cx . tcx . hir ( ) . get_parent_item ( item_hir) ;
776
- let item_parent = self . cx . tcx . hir ( ) . find ( parent_hir) ;
777
- match item_parent {
778
- Some ( hir:: Node :: Item ( hir:: Item {
779
- kind :
780
- hir:: ItemKind :: Impl {
781
- self_ty :
782
- hir:: Ty {
783
- kind :
784
- hir:: TyKind :: Path ( hir:: QPath :: Resolved (
785
- _,
786
- hir:: Path { segments, .. } ,
787
- ) ) ,
788
- ..
789
- } ,
790
- ..
791
- } ,
792
- ..
793
- } ) ) => segments. first ( ) . map ( |seg| seg. ident . to_string ( ) ) ,
794
- Some ( hir:: Node :: Item ( hir:: Item {
795
- ident, kind : hir:: ItemKind :: Enum ( ..) , ..
796
- } ) )
797
- | Some ( hir:: Node :: Item ( hir:: Item {
798
- ident, kind : hir:: ItemKind :: Struct ( ..) , ..
799
- } ) )
800
- | Some ( hir:: Node :: Item ( hir:: Item {
801
- ident, kind : hir:: ItemKind :: Union ( ..) , ..
802
- } ) )
803
- | Some ( hir:: Node :: Item ( hir:: Item {
804
- ident, kind : hir:: ItemKind :: Trait ( ..) , ..
805
- } ) ) => Some ( ident. to_string ( ) ) ,
806
- _ => None ,
807
- }
808
- } ) ;
809
-
810
764
for ( ori_link, link_range) in markdown_links ( & dox) {
811
- self . resolve_link (
812
- & mut item,
813
- & dox,
814
- & current_item,
815
- parent_node,
816
- & parent_name,
817
- ori_link,
818
- link_range,
819
- ) ;
765
+ self . resolve_link ( & mut item, & dox, & self_name, parent_node, ori_link, link_range) ;
820
766
}
821
767
822
768
if item. is_mod ( ) && !item. attrs . inner_docs {
@@ -840,9 +786,8 @@ impl LinkCollector<'_, '_> {
840
786
& self ,
841
787
item : & mut Item ,
842
788
dox : & str ,
843
- current_item : & Option < String > ,
789
+ self_name : & Option < String > ,
844
790
parent_node : Option < DefId > ,
845
- parent_name : & Option < String > ,
846
791
ori_link : String ,
847
792
link_range : Option < Range < usize > > ,
848
793
) {
@@ -933,8 +878,10 @@ impl LinkCollector<'_, '_> {
933
878
934
879
// replace `Self` with suitable item's parent name
935
880
if path_str. starts_with ( "Self::" ) {
936
- if let Some ( ref name) = parent_name {
881
+ if let Some ( ref name) = self_name {
882
+ debug ! ( "replacing Self with {}" , name) ;
937
883
resolved_self = format ! ( "{}::{}" , name, & path_str[ 6 ..] ) ;
884
+ // FIXME: this overwrites the link text in both error messages and the link body
938
885
path_str = & resolved_self;
939
886
}
940
887
} else if path_str. starts_with ( "crate::" ) {
@@ -955,7 +902,6 @@ impl LinkCollector<'_, '_> {
955
902
item,
956
903
dox,
957
904
path_str,
958
- current_item,
959
905
module_id,
960
906
extra_fragment,
961
907
& ori_link,
@@ -1083,15 +1029,14 @@ impl LinkCollector<'_, '_> {
1083
1029
item : & mut Item ,
1084
1030
dox : & str ,
1085
1031
path_str : & str ,
1086
- current_item : & Option < String > ,
1087
1032
base_node : DefId ,
1088
1033
extra_fragment : Option < String > ,
1089
1034
ori_link : & str ,
1090
1035
link_range : Option < Range < usize > > ,
1091
1036
) -> Option < ( Res , Option < String > ) > {
1092
1037
match disambiguator. map ( Disambiguator :: ns) {
1093
1038
Some ( ns @ ( ValueNS | TypeNS ) ) => {
1094
- match self . resolve ( path_str, ns, & current_item , base_node, & extra_fragment) {
1039
+ match self . resolve ( path_str, ns, base_node, & extra_fragment) {
1095
1040
Ok ( res) => Some ( res) ,
1096
1041
Err ( ErrorKind :: Resolve ( box mut kind) ) => {
1097
1042
// We only looked in one namespace. Try to give a better error if possible.
@@ -1104,7 +1049,6 @@ impl LinkCollector<'_, '_> {
1104
1049
new_ns,
1105
1050
path_str,
1106
1051
base_node,
1107
- & current_item,
1108
1052
& extra_fragment,
1109
1053
) {
1110
1054
kind = ResolutionFailure :: WrongNamespace ( res, ns) ;
@@ -1138,13 +1082,7 @@ impl LinkCollector<'_, '_> {
1138
1082
macro_ns : self
1139
1083
. macro_resolve ( path_str, base_node)
1140
1084
. map ( |res| ( res, extra_fragment. clone ( ) ) ) ,
1141
- type_ns : match self . resolve (
1142
- path_str,
1143
- TypeNS ,
1144
- & current_item,
1145
- base_node,
1146
- & extra_fragment,
1147
- ) {
1085
+ type_ns : match self . resolve ( path_str, TypeNS , base_node, & extra_fragment) {
1148
1086
Ok ( res) => {
1149
1087
debug ! ( "got res in TypeNS: {:?}" , res) ;
1150
1088
Ok ( res)
@@ -1155,13 +1093,7 @@ impl LinkCollector<'_, '_> {
1155
1093
}
1156
1094
Err ( ErrorKind :: Resolve ( box kind) ) => Err ( kind) ,
1157
1095
} ,
1158
- value_ns : match self . resolve (
1159
- path_str,
1160
- ValueNS ,
1161
- & current_item,
1162
- base_node,
1163
- & extra_fragment,
1164
- ) {
1096
+ value_ns : match self . resolve ( path_str, ValueNS , base_node, & extra_fragment) {
1165
1097
Ok ( res) => Ok ( res) ,
1166
1098
Err ( ErrorKind :: AnchorFailure ( msg) ) => {
1167
1099
anchor_failure ( self . cx , & item, ori_link, dox, link_range, msg) ;
@@ -1229,13 +1161,9 @@ impl LinkCollector<'_, '_> {
1229
1161
Err ( mut kind) => {
1230
1162
// `macro_resolve` only looks in the macro namespace. Try to give a better error if possible.
1231
1163
for & ns in & [ TypeNS , ValueNS ] {
1232
- if let Some ( res) = self . check_full_res (
1233
- ns,
1234
- path_str,
1235
- base_node,
1236
- & current_item,
1237
- & extra_fragment,
1238
- ) {
1164
+ if let Some ( res) =
1165
+ self . check_full_res ( ns, path_str, base_node, & extra_fragment)
1166
+ {
1239
1167
kind = ResolutionFailure :: WrongNamespace ( res, MacroNS ) ;
1240
1168
break ;
1241
1169
}
@@ -1558,7 +1486,7 @@ fn resolution_failure(
1558
1486
name = start;
1559
1487
for & ns in & [ TypeNS , ValueNS , MacroNS ] {
1560
1488
if let Some ( res) =
1561
- collector. check_full_res ( ns, & start, module_id, & None , & None )
1489
+ collector. check_full_res ( ns, & start, module_id, & None )
1562
1490
{
1563
1491
debug ! ( "found partial_res={:?}" , res) ;
1564
1492
* partial_res = Some ( res) ;
0 commit comments