Skip to content

Commit 0acaa5a

Browse files
committed
Fix intra-doc links to Self and crate
1 parent c8915ee commit 0acaa5a

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1024,12 +1024,18 @@ impl LinkCollector<'_, '_> {
10241024

10251025
let resolved_self;
10261026
// replace `Self` with suitable item's parent name
1027-
if path_str.starts_with("Self::") {
1027+
let is_lone_self = path_str == "Self";
1028+
let is_lone_crate = path_str == "crate";
1029+
if path_str.starts_with("Self::") || is_lone_self {
10281030
if let Some(ref name) = self_name {
1029-
resolved_self = format!("{}::{}", name, &path_str[6..]);
1030-
path_str = &resolved_self;
1031+
if is_lone_self {
1032+
path_str = name;
1033+
} else {
1034+
resolved_self = format!("{}::{}", name, &path_str[6..]);
1035+
path_str = &resolved_self;
1036+
}
10311037
}
1032-
} else if path_str.starts_with("crate::") {
1038+
} else if path_str.starts_with("crate::") || is_lone_crate {
10331039
use rustc_span::def_id::CRATE_DEF_INDEX;
10341040

10351041
// HACK(jynelson): rustc_resolve thinks that `crate` is the crate currently being documented.
@@ -1038,8 +1044,12 @@ impl LinkCollector<'_, '_> {
10381044
// HACK(jynelson)(2): If we just strip `crate::` then suddenly primitives become ambiguous
10391045
// (consider `crate::char`). Instead, change it to `self::`. This works because 'self' is now the crate root.
10401046
// FIXME(#78696): This doesn't always work.
1041-
resolved_self = format!("self::{}", &path_str["crate::".len()..]);
1042-
path_str = &resolved_self;
1047+
if is_lone_crate {
1048+
path_str = "self";
1049+
} else {
1050+
resolved_self = format!("self::{}", &path_str["crate::".len()..]);
1051+
path_str = &resolved_self;
1052+
}
10431053
module_id = DefId { krate, index: CRATE_DEF_INDEX };
10441054
}
10451055

src/test/rustdoc/intra-doc-crate/auxiliary/self.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#![crate_name = "cross_crate_self"]
2+
3+
/// Link to [Self]
4+
/// Link to [crate]
25
pub struct S;
36

47
impl S {
+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// aux-build:self.rs
2+
// build-aux-docs
23

34
extern crate cross_crate_self;
45

56
// @has self/struct.S.html '//a[@href="../self/struct.S.html#method.f"]' "Self::f"
7+
// @has self/struct.S.html '//a[@href="../self/struct.S.html"]' "Self"
8+
// @has self/struct.S.html '//a[@href="../cross_crate_self/index.html"]' "crate"
69
pub use cross_crate_self::S;

0 commit comments

Comments
 (0)