@@ -797,6 +797,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
797
797
} ;
798
798
Some ( Res :: Def ( self . cx . tcx . def_kind ( id) , id) )
799
799
} ) ;
800
+ debug ! ( "self_id={:?}" , self_id) ;
800
801
801
802
if item. is_mod ( ) && item. attrs . inner_docs {
802
803
self . mod_ids . push ( item. def_id ) ;
@@ -921,11 +922,14 @@ impl LinkCollector<'_, '_> {
921
922
} ;
922
923
923
924
// replace `Self` with suitable item's parent name
924
- if path_str. starts_with ( "Self::" ) {
925
+ let starts_with_self = path_str. starts_with ( "Self::" ) ;
926
+ let starts_with_crate = path_str. starts_with ( "crate::" ) ;
927
+ if starts_with_self || path_str == "Self" {
925
928
if let Some ( id) = self_id {
926
929
debug ! ( "resolving Self as {:?}" , id) ;
927
930
// FIXME: this overwrites the link text in both error messages and the link body
928
- path_str = & path_str[ "Self::" . len ( ) ..] ;
931
+ let idx = if starts_with_self { "Self::" } else { "Self" } . len ( ) ;
932
+ path_str = & path_str[ idx..] ;
929
933
} else {
930
934
return resolution_failure (
931
935
self ,
@@ -937,16 +941,20 @@ impl LinkCollector<'_, '_> {
937
941
smallvec ! [ ResolutionFailure :: NoSelf ] ,
938
942
) ;
939
943
}
940
- } else if path_str. starts_with ( "crate::" ) {
944
+ } else if starts_with_crate || path_str == "crate" {
941
945
use rustc_span:: def_id:: CRATE_DEF_INDEX ;
942
946
943
947
// HACK(jynelson): rustc_resolve thinks that `crate` is the crate currently being documented.
944
948
// But rustdoc wants it to mean the crate this item was originally present in.
945
949
// To work around this, remove it and resolve relative to the crate root instead.
946
950
// HACK(jynelson)(2): If we just strip `crate::` then suddenly primitives become ambiguous
947
951
// (consider `crate::char`). Instead, change it to `self::`. This works because 'self' is now the crate root.
948
- resolved_crate = format ! ( "self::{}" , & path_str[ "crate::" . len( ) ..] ) ;
949
- path_str = & resolved_crate;
952
+ if starts_with_crate {
953
+ resolved_crate = format ! ( "self::{}" , & path_str[ "crate::" . len( ) ..] ) ;
954
+ path_str = & resolved_crate;
955
+ } else {
956
+ path_str = "self" ;
957
+ }
950
958
module_id = DefId { krate : item. def_id . krate , index : CRATE_DEF_INDEX } ;
951
959
self_id = None ;
952
960
} else {
0 commit comments