Auto merge of #77253 - jyn514:crate-link, r=Manishearth

Resolve `crate` in intra-doc links properly across crates

Closes https://github.com/rust-lang/rust/issues/77193; see https://github.com/rust-lang/rust/issues/77193#issuecomment-699065946 for an explanation of what's going on here.
~~This also fixes the BTreeMap docs that have been broken for a while; see the description on the second commit for why and how.~~ Nope, see the second commit for why the link had to be changed.

r? `@Manishearth`
cc `@dylni`

`@dylni` note that this doesn't solve your original problem - now _both_ `with_code` and `crate::with_code` will be broken links. However this will fix a lot of other broken links (in particular I think https://docs.rs/sqlx/0.4.0-beta.1/sqlx/query/struct.Query.html is because of this bug). I'll open another issue for resolving additional docs in the new scope.
This commit is contained in:
bors 2020-09-29 12:11:17 +00:00
commit 9e34b72964
4 changed files with 24 additions and 2 deletions

View File

@ -811,7 +811,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
/// types that can be `==` without being identical. See the [module-level
/// documentation] for more.
///
/// [module-level documentation]: crate::collections#insert-and-complex-keys
/// [module-level documentation]: index.html#insert-and-complex-keys
///
/// # Examples
///

View File

@ -914,7 +914,7 @@ impl LinkCollector<'_, '_> {
parent_node
};
let module_id = if let Some(id) = base_node {
let mut module_id = if let Some(id) = base_node {
id
} else {
debug!("attempting to resolve item without parent module: {}", path_str);
@ -937,6 +937,17 @@ impl LinkCollector<'_, '_> {
resolved_self = format!("{}::{}", name, &path_str[6..]);
path_str = &resolved_self;
}
} else if path_str.starts_with("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_self = format!("self::{}", &path_str["crate::".len()..]);
path_str = &resolved_self;
module_id = DefId { krate: item.def_id.krate, index: CRATE_DEF_INDEX };
}
match self.resolve_with_disambiguator(

View File

@ -0,0 +1,5 @@
#![crate_name = "inner"]
/// Links to [crate::g]
pub fn f() {}
pub fn g() {}

View File

@ -0,0 +1,6 @@
// aux-build:intra-link-cross-crate-crate.rs
// build-aux-docs
#![crate_name = "outer"]
extern crate inner;
// @has outer/fn.f.html '//a[@href="../inner/fn.g.html"]' "crate::g"
pub use inner::f;