Skip to content

Commit 9e34b72

Browse files
committed
Auto merge of #77253 - jyn514:crate-link, r=Manishearth
Resolve `crate` in intra-doc links properly across crates Closes #77193; see #77193 (comment) 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.
2 parents 26373fb + 4065846 commit 9e34b72

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

library/alloc/src/collections/btree/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
811811
/// types that can be `==` without being identical. See the [module-level
812812
/// documentation] for more.
813813
///
814-
/// [module-level documentation]: crate::collections#insert-and-complex-keys
814+
/// [module-level documentation]: index.html#insert-and-complex-keys
815815
///
816816
/// # Examples
817817
///

src/librustdoc/passes/collect_intra_doc_links.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ impl LinkCollector<'_, '_> {
914914
parent_node
915915
};
916916

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

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