Skip to content

Commit 07bcd4b

Browse files
Allow to link to current crate
1 parent ac91805 commit 07bcd4b

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,14 @@ impl<'tcx> LinkCollector<'_, 'tcx> {
455455
)
456456
})
457457
.and_then(|res| res.try_into().ok())
458-
.or_else(|| resolve_primitive(path_str, ns));
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+
});
459466
debug!("{path_str} resolved to {result:?} in namespace {ns:?}");
460467
result
461468
}

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="foo"]
2+
#![crate_name="foofoo"]
33

44
// First a module with inner documentation
55

6-
//@ has foo/subone/index.html
7-
// These foo/bar links in the module's documentation should refer inside `subone`
6+
//@ has foofoo/subone/index.html
7+
// These foofoo/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 foo/subone/fn.foo.html
14+
//@ has foofoo/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 foo/subone/fn.bar.html
19+
//@ has foofoo/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 foo/subtwo/index.html
27+
//@ has foofoo/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 foo/subtwo/fn.foo.html
42+
//@ has foofoo/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 foo/subtwo/fn.bar.html
48+
//@ has foofoo/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 foo/subthree/index.html
63+
//@ has foofoo/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 foo/subfour/subfive/subsix/subseven/subeight/index.html
73+
//@ has foofoo/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)