Skip to content

Commit

Permalink
try to work on rust-lang#77732
Browse files Browse the repository at this point in the history
this breaks because it still goes through
`resolve_with_disambiguator()`; it should skip that logic altogether
  • Loading branch information
jyn514 committed Oct 9, 2020
1 parent d7b0b8e commit b7bf22b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
};
Some(Res::Def(self.cx.tcx.def_kind(id), id))
});
debug!("self_id={:?}", self_id);

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

// replace `Self` with suitable item's parent name
if path_str.starts_with("Self::") {
let starts_with_self = path_str.starts_with("Self::");
let starts_with_crate = path_str.starts_with("crate::");
if starts_with_self || path_str == "Self" {
if let Some(id) = self_id {
debug!("resolving Self as {:?}", id);
// FIXME: this overwrites the link text in both error messages and the link body
path_str = &path_str["Self::".len()..];
let idx = if starts_with_self { "Self::" } else { "Self" }.len();
path_str = &path_str[idx..];
} else {
return resolution_failure(
self,
Expand All @@ -937,16 +941,20 @@ impl LinkCollector<'_, '_> {
smallvec![ResolutionFailure::NoSelf],
);
}
} else if path_str.starts_with("crate::") {
} else if starts_with_crate || path_str == "crate" {
use rustc_span::def_id::CRATE_DEF_INDEX;

// HACK(jynelson): rustc_resolve thinks that `crate` is the crate currently being documented.
// But rustdoc wants it to mean the crate this item was originally present in.
// To work around this, remove it and resolve relative to the crate root instead.
// HACK(jynelson)(2): If we just strip `crate::` then suddenly primitives become ambiguous
// (consider `crate::char`). Instead, change it to `self::`. This works because 'self' is now the crate root.
resolved_crate = format!("self::{}", &path_str["crate::".len()..]);
path_str = &resolved_crate;
if starts_with_crate {
resolved_crate = format!("self::{}", &path_str["crate::".len()..]);
path_str = &resolved_crate;
} else {
path_str = "self";
}
module_id = DefId { krate: item.def_id.krate, index: CRATE_DEF_INDEX };
self_id = None;
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/test/rustdoc/intra-link-self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ impl MyTrait for MyStruct {
unimplemented!()
}
}

/// [Self]
// @has 'foo/struct.Stdout.html' '//a[@href="https://doc.rust-lang.org/nightly/std/io/struct.Stdout.html"]' 'Self'
pub use std::io::Stdout;

0 comments on commit b7bf22b

Please sign in to comment.