Skip to content

Commit b7bf22b

Browse files
committed
try to work on rust-lang#77732
this breaks because it still goes through `resolve_with_disambiguator()`; it should skip that logic altogether
1 parent d7b0b8e commit b7bf22b

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Diff for: src/librustdoc/passes/collect_intra_doc_links.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
797797
};
798798
Some(Res::Def(self.cx.tcx.def_kind(id), id))
799799
});
800+
debug!("self_id={:?}", self_id);
800801

801802
if item.is_mod() && item.attrs.inner_docs {
802803
self.mod_ids.push(item.def_id);
@@ -921,11 +922,14 @@ impl LinkCollector<'_, '_> {
921922
};
922923

923924
// 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" {
925928
if let Some(id) = self_id {
926929
debug!("resolving Self as {:?}", id);
927930
// 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..];
929933
} else {
930934
return resolution_failure(
931935
self,
@@ -937,16 +941,20 @@ impl LinkCollector<'_, '_> {
937941
smallvec![ResolutionFailure::NoSelf],
938942
);
939943
}
940-
} else if path_str.starts_with("crate::") {
944+
} else if starts_with_crate || path_str == "crate" {
941945
use rustc_span::def_id::CRATE_DEF_INDEX;
942946

943947
// HACK(jynelson): rustc_resolve thinks that `crate` is the crate currently being documented.
944948
// But rustdoc wants it to mean the crate this item was originally present in.
945949
// To work around this, remove it and resolve relative to the crate root instead.
946950
// HACK(jynelson)(2): If we just strip `crate::` then suddenly primitives become ambiguous
947951
// (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+
}
950958
module_id = DefId { krate: item.def_id.krate, index: CRATE_DEF_INDEX };
951959
self_id = None;
952960
} else {

Diff for: src/test/rustdoc/intra-link-self.rs

+4
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,7 @@ impl MyTrait for MyStruct {
115115
unimplemented!()
116116
}
117117
}
118+
119+
/// [Self]
120+
// @has 'foo/struct.Stdout.html' '//a[@href="https://doc.rust-lang.org/nightly/std/io/struct.Stdout.html"]' 'Self'
121+
pub use std::io::Stdout;

0 commit comments

Comments
 (0)