Skip to content

Commit 6e2249d

Browse files
authored
Rollup merge of #80881 - jyn514:intra-doc-self, r=GuillaumeGomez
Fix intra-doc links to `Self` and `crate` Closes #77732.
2 parents 95a6279 + a52341d commit 6e2249d

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+20-21
Original file line numberDiff line numberDiff line change
@@ -504,15 +504,9 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
504504
match res {
505505
// FIXME(#76467): make this fallthrough to lookup the associated
506506
// item a separate function.
507-
Res::Def(DefKind::AssocFn | DefKind::AssocConst, _) => {
508-
assert_eq!(ns, ValueNS);
509-
}
510-
Res::Def(DefKind::AssocTy, _) => {
511-
assert_eq!(ns, TypeNS);
512-
}
513-
Res::Def(DefKind::Variant, _) => {
514-
return handle_variant(cx, res, extra_fragment);
515-
}
507+
Res::Def(DefKind::AssocFn | DefKind::AssocConst, _) => assert_eq!(ns, ValueNS),
508+
Res::Def(DefKind::AssocTy, _) => assert_eq!(ns, TypeNS),
509+
Res::Def(DefKind::Variant, _) => return handle_variant(cx, res, extra_fragment),
516510
// Not a trait item; just return what we found.
517511
Res::Primitive(ty) => {
518512
if extra_fragment.is_some() {
@@ -522,12 +516,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
522516
}
523517
return Ok((res, Some(ty.as_str().to_owned())));
524518
}
525-
Res::Def(DefKind::Mod, _) => {
526-
return Ok((res, extra_fragment.clone()));
527-
}
528-
_ => {
529-
return Ok((res, extra_fragment.clone()));
530-
}
519+
_ => return Ok((res, extra_fragment.clone())),
531520
}
532521
}
533522

@@ -1024,12 +1013,18 @@ impl LinkCollector<'_, '_> {
10241013

10251014
let resolved_self;
10261015
// replace `Self` with suitable item's parent name
1027-
if path_str.starts_with("Self::") {
1016+
let is_lone_self = path_str == "Self";
1017+
let is_lone_crate = path_str == "crate";
1018+
if path_str.starts_with("Self::") || is_lone_self {
10281019
if let Some(ref name) = self_name {
1029-
resolved_self = format!("{}::{}", name, &path_str[6..]);
1030-
path_str = &resolved_self;
1020+
if is_lone_self {
1021+
path_str = name;
1022+
} else {
1023+
resolved_self = format!("{}::{}", name, &path_str[6..]);
1024+
path_str = &resolved_self;
1025+
}
10311026
}
1032-
} else if path_str.starts_with("crate::") {
1027+
} else if path_str.starts_with("crate::") || is_lone_crate {
10331028
use rustc_span::def_id::CRATE_DEF_INDEX;
10341029

10351030
// HACK(jynelson): rustc_resolve thinks that `crate` is the crate currently being documented.
@@ -1038,8 +1033,12 @@ impl LinkCollector<'_, '_> {
10381033
// HACK(jynelson)(2): If we just strip `crate::` then suddenly primitives become ambiguous
10391034
// (consider `crate::char`). Instead, change it to `self::`. This works because 'self' is now the crate root.
10401035
// FIXME(#78696): This doesn't always work.
1041-
resolved_self = format!("self::{}", &path_str["crate::".len()..]);
1042-
path_str = &resolved_self;
1036+
if is_lone_crate {
1037+
path_str = "self";
1038+
} else {
1039+
resolved_self = format!("self::{}", &path_str["crate::".len()..]);
1040+
path_str = &resolved_self;
1041+
}
10431042
module_id = DefId { krate, index: CRATE_DEF_INDEX };
10441043
}
10451044

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)