Skip to content

Commit c8b16cd

Browse files
rustdoc: Allow linking from private items to private types
Fixes #74134 After PR #72771 this would trigger an intra_doc_link_resolution_failure warning when rustdoc is invoked without --document-private-items. Links from private items to private types are however never actually generated in that case and thus shouldn't produce a warning. These links are in fact a very useful tool to document crate internals. Tests are added for all 4 combinations of public/private items and link targets. Test 1 is the case mentioned above and fails without this commit. Tests 2 - 4 passed before already but are added nonetheless to prevent regressions.
1 parent 8ac1525 commit c8b16cd

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
796796

797797
let hir_id = self.cx.tcx.hir().as_local_hir_id(local);
798798
if !self.cx.tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_id)
799+
&& (item.visibility == Visibility::Public)
799800
&& !self.cx.render_options.document_private
800801
{
801802
let item_name = item.name.as_deref().unwrap_or("<unknown>");

src/test/rustdoc/issue-74134-1.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![deny(intra_doc_link_resolution_failure)]
2+
3+
// Linking from a private item to a private type is fine without --document-private-items.
4+
5+
struct Private;
6+
7+
pub struct Public {
8+
/// [`Private`]
9+
private: Private,
10+
}

src/test/rustdoc/issue-74134-2.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-flags: --document-private-items
2+
#![deny(intra_doc_link_resolution_failure)]
3+
4+
// Linking from a private item to a private type is fine with --document-private-items.
5+
6+
struct Private;
7+
8+
pub struct Public {
9+
/// [`Private`]
10+
private: Private,
11+
}

src/test/rustdoc/issue-74134-3.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// should-fail
2+
#![deny(intra_doc_link_resolution_failure)]
3+
4+
// Linking from a public item to a private type fails without --document-private-items.
5+
6+
struct Private;
7+
8+
pub struct Public {
9+
/// [`Private`]
10+
pub public: u32,
11+
}

src/test/rustdoc/issue-74134-4.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-flags: --document-private-items
2+
#![deny(intra_doc_link_resolution_failure)]
3+
4+
// Linking from a public item to a private type is fine with --document-private-items.
5+
6+
struct Private;
7+
8+
pub struct Public {
9+
/// [`Private`]
10+
pub public: u32,
11+
}

0 commit comments

Comments
 (0)