Skip to content

Commit 61ef8b3

Browse files
committed
Resolve crate properly across crates
1 parent cbc5e4d commit 61ef8b3

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
@@ -961,7 +961,7 @@ impl LinkCollector<'_, '_> {
961961
parent_node
962962
};
963963

964-
let module_id = if let Some(id) = base_node {
964+
let mut module_id = if let Some(id) = base_node {
965965
id
966966
} else {
967967
debug!("attempting to resolve item without parent module: {}", path_str);
@@ -984,6 +984,17 @@ impl LinkCollector<'_, '_> {
984984
resolved_self = format!("{}::{}", name, &path_str[6..]);
985985
path_str = &resolved_self;
986986
}
987+
} else if path_str.starts_with("crate::") {
988+
use rustc_span::def_id::CRATE_DEF_INDEX;
989+
990+
// HACK(jynelson): rustc_resolve thinks that `crate` is the crate currently being documented.
991+
// But rustdoc wants it to mean the crate this item was originally present in.
992+
// To work around this, remove it and resolve relative to the crate root instead.
993+
// HACK(jynelson)(2): If we just strip `crate::` then suddenly primitives become ambiguous
994+
// (consider `crate::char`). Instead, change it to `self::`. This works because 'self' is now the crate root.
995+
resolved_self = format!("self::{}", &path_str["crate::".len()..]);
996+
path_str = &resolved_self;
997+
module_id = DefId { krate: item.def_id.krate, index: CRATE_DEF_INDEX };
987998
}
988999

9891000
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)