Skip to content

Commit 5a0eb4f

Browse files
Move intra-doc link to current crate into rustc_resolve directly
1 parent 200dd77 commit 5a0eb4f

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

compiler/rustc_resolve/src/late.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4951,10 +4951,26 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
49514951
path = &path[..idx];
49524952
need_traits_in_scope = true;
49534953
for ns in [TypeNS, ValueNS, MacroNS] {
4954-
self.resolve_and_cache_rustdoc_path(path, ns);
4954+
if let Some(res) = self.resolve_and_cache_rustdoc_path(path, ns) {
4955+
// Rustdoc ignores tool attribute resolutions and attempts
4956+
// to resolve their prefixes for diagnostics.
4957+
any_resolved |=
4958+
!matches!(res, Res::NonMacroAttr(NonMacroAttrKind::Tool));
4959+
}
49554960
}
49564961
}
49574962
}
4963+
// Only link to current crate if nothing else matched.
4964+
if !any_resolved && &*path_str == self.r.tcx.crate_name(LOCAL_CRATE).as_str() {
4965+
self.r
4966+
.doc_link_resolutions
4967+
.entry(self.parent_scope.module.nearest_parent_mod().expect_local())
4968+
.or_default()
4969+
.insert(
4970+
(Symbol::intern(&path_str), ValueNS),
4971+
Some(Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id())),
4972+
);
4973+
}
49584974
}
49594975

49604976
if need_traits_in_scope {

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,14 +455,7 @@ impl<'tcx> LinkCollector<'_, 'tcx> {
455455
)
456456
})
457457
.and_then(|res| res.try_into().ok())
458-
.or_else(|| resolve_primitive(path_str, ns))
459-
.or_else(|| {
460-
if self.cx.tcx.crate_name(LOCAL_CRATE).as_str() == path_str {
461-
Some(Res::from_def_id(self.cx.tcx, CRATE_DEF_ID.to_def_id()))
462-
} else {
463-
None
464-
}
465-
});
458+
.or_else(|| resolve_primitive(path_str, ns));
466459
debug!("{path_str} resolved to {result:?} in namespace {ns:?}");
467460
result
468461
}

tests/rustdoc/intra-doc/module-scope-name-resolution-55364.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
// https://github.com/rust-lang/rust/issues/55364
2-
#![crate_name="foofoo"]
2+
#![crate_name="foo"]
33

44
// First a module with inner documentation
55

6-
//@ has foofoo/subone/index.html
7-
// These foofoo/bar links in the module's documentation should refer inside `subone`
6+
//@ has foo/subone/index.html
7+
// These foo/bar links in the module's documentation should refer inside `subone`
88
//@ has - '//section[@id="main-content"]/details[@open=""]/div[@class="docblock"]//a[@href="fn.foo.html"]' 'foo'
99
//@ has - '//section[@id="main-content"]/details[@open=""]/div[@class="docblock"]//a[@href="fn.bar.html"]' 'bar'
1010
pub mod subone {
1111
//! See either [foo] or [bar].
1212
1313
// This should refer to subone's `bar`
14-
//@ has foofoo/subone/fn.foo.html
14+
//@ has foo/subone/fn.foo.html
1515
//@ has - '//section[@id="main-content"]/details/div[@class="docblock"]//a[@href="fn.bar.html"]' 'bar'
1616
/// See [bar]
1717
pub fn foo() {}
1818
// This should refer to subone's `foo`
19-
//@ has foofoo/subone/fn.bar.html
19+
//@ has foo/subone/fn.bar.html
2020
//@ has - '//section[@id="main-content"]/details/div[@class="docblock"]//a[@href="fn.foo.html"]' 'foo'
2121
/// See [foo]
2222
pub fn bar() {}
2323
}
2424

2525
// A module with outer documentation
2626

27-
//@ has foofoo/subtwo/index.html
27+
//@ has foo/subtwo/index.html
2828
// These foo/bar links in the module's documentation should not reference inside `subtwo`
2929
//@ !has - '//section[@id="main-content"]/div[@class="docblock"]//a[@href="fn.foo.html"]' 'foo'
3030
//@ !has - '//section[@id="main-content"]/div[@class="docblock"]//a[@href="fn.bar.html"]' 'bar'
@@ -39,13 +39,13 @@ pub mod subtwo {
3939

4040
// Despite the module's docs referring to the top level foo/bar,
4141
// this should refer to subtwo's `bar`
42-
//@ has foofoo/subtwo/fn.foo.html
42+
//@ has foo/subtwo/fn.foo.html
4343
//@ has - '//section[@id="main-content"]/details/div[@class="docblock"]//a[@href="fn.bar.html"]' 'bar'
4444
/// See [bar]
4545
pub fn foo() {}
4646
// Despite the module's docs referring to the top level foo/bar,
4747
// this should refer to subtwo's `foo`
48-
//@ has foofoo/subtwo/fn.bar.html
48+
//@ has foo/subtwo/fn.bar.html
4949
//@ has - '//section[@id="main-content"]/details/div[@class="docblock"]//a[@href="fn.foo.html"]' 'foo'
5050
/// See [foo]
5151
pub fn bar() {}
@@ -60,7 +60,7 @@ pub fn bar() {}
6060

6161
// This module refers to the outer foo/bar by means of `super::`
6262

63-
//@ has foofoo/subthree/index.html
63+
//@ has foo/subthree/index.html
6464
// This module should also refer to the top level foo/bar
6565
//@ has - '//section[@id="main-content"]/details/div[@class="docblock"]//a[@href="../fn.foo.html"]' 'foo'
6666
//@ has - '//section[@id="main-content"]/details/div[@class="docblock"]//a[@href="../fn.bar.html"]' 'bar'
@@ -70,7 +70,7 @@ pub mod subthree {
7070

7171
// Next we go *deeper* - In order to ensure it's not just "this or parent"
7272
// we test `crate::` and a `super::super::...` chain
73-
//@ has foofoo/subfour/subfive/subsix/subseven/subeight/index.html
73+
//@ has foo/subfour/subfive/subsix/subseven/subeight/index.html
7474
//@ has - '//section[@id="main-content"]/dl[@class="item-table"]/dd//a[@href="../../../../../subone/fn.foo.html"]' 'other foo'
7575
//@ has - '//section[@id="main-content"]/dl[@class="item-table"]/dd//a[@href="../../../../../subtwo/fn.bar.html"]' 'other bar'
7676
pub mod subfour {

0 commit comments

Comments
 (0)