Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve crate in intra-doc links properly across crates #77253

Merged
merged 2 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
ollie27 marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Examples
///
Expand Down
13 changes: 12 additions & 1 deletion src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,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);
Expand All @@ -934,6 +934,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(
Expand Down
5 changes: 5 additions & 0 deletions src/test/rustdoc/auxiliary/intra-link-cross-crate-crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![crate_name = "inner"]

/// Links to [crate::g]
pub fn f() {}
pub fn g() {}
6 changes: 6 additions & 0 deletions src/test/rustdoc/intra-link-cross-crate-crate.rs
Original file line number Diff line number Diff line change
@@ -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;