Skip to content

Commit 8437d7b

Browse files
committed
Resolve crate properly across crates
1 parent b836329 commit 8437d7b

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ impl LinkCollector<'_, '_> {
911911
parent_node
912912
};
913913

914-
let module_id = if let Some(id) = base_node {
914+
let mut module_id = if let Some(id) = base_node {
915915
id
916916
} else {
917917
debug!("attempting to resolve item without parent module: {}", path_str);
@@ -934,6 +934,17 @@ impl LinkCollector<'_, '_> {
934934
resolved_self = format!("{}::{}", name, &path_str[6..]);
935935
path_str = &resolved_self;
936936
}
937+
} else if path_str.starts_with("crate::") {
938+
use rustc_span::def_id::CRATE_DEF_INDEX;
939+
940+
// HACK(jynelson): rustc_resolve thinks that `crate` is the crate currently being documented.
941+
// But rustdoc wants it to mean the crate this item was originally present in.
942+
// To work around this, remove it and resolve relative to the crate root instead.
943+
// HACK(jynelson)(2): If we just strip `crate::` then suddenly primitives become ambiguous
944+
// (consider `crate::char`). Instead, change it to `self::`. This works because 'self' is now the crate root.
945+
resolved_self = format!("self::{}", &path_str["crate::".len()..]);
946+
path_str = &resolved_self;
947+
module_id = DefId { krate: item.def_id.krate, index: CRATE_DEF_INDEX };
937948
}
938949

939950
match self.resolve_with_disambiguator(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![crate_name = "inner"]
2+
3+
/// Links to [crate::g]
4+
pub fn f() {}
5+
pub fn g() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// aux-build:intra-link-cross-crate-crate.rs
2+
// build-aux-docs
3+
#![crate_name = "outer"]
4+
extern crate inner;
5+
// @has outer/fn.f.html '//a[@href="../inner/fn.g.html"]' "crate::g"
6+
pub use inner::f;

0 commit comments

Comments
 (0)