Skip to content

Commit 27a1353

Browse files
authored
Rollup merge of rust-lang#74147 - dennis-hamester:fix/issue-74134, r=jyn514
rustdoc: Allow linking from private items to private types Fixes rust-lang#74134 After PR rust-lang#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.
2 parents 3239e02 + 8789525 commit 27a1353

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+1
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
799799

800800
let hir_id = self.cx.tcx.hir().as_local_hir_id(local);
801801
if !self.cx.tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_id)
802+
&& (item.visibility == Visibility::Public)
802803
&& !self.cx.render_options.document_private
803804
{
804805
let item_name = item.name.as_deref().unwrap_or("<unknown>");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: `[PrivateType]` public documentation for `public_item` links to a private item
2+
--> $DIR/issue-74134.rs:19:10
3+
|
4+
LL | /// [`PrivateType`]
5+
| ^^^^^^^^^^^^^ this item is private
6+
|
7+
= note: `#[warn(intra_doc_link_resolution_failure)]` on by default
8+
9+
warning: 1 warning emitted
10+

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

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// revisions: public private
2+
// [private]compile-flags: --document-private-items
3+
// check-pass
4+
5+
// There are 4 cases here:
6+
// 1. public item -> public type: no warning
7+
// 2. public item -> private type: warning, if --document-private-items is not passed
8+
// 3. private item -> public type: no warning
9+
// 4. private item -> private type: no warning
10+
// All 4 cases are tested with and without --document-private-items.
11+
//
12+
// Case 4 without --document-private-items is the one described in issue #74134.
13+
14+
struct PrivateType;
15+
pub struct PublicType;
16+
17+
pub struct Public {
18+
/// [`PublicType`]
19+
/// [`PrivateType`]
20+
//[public]~^ WARNING public documentation for `public_item` links to a private
21+
pub public_item: u32,
22+
23+
/// [`PublicType`]
24+
/// [`PrivateType`]
25+
private_item: u32,
26+
}
27+
28+
// The following cases are identical to the ones above, except that they are in a private
29+
// module. Thus they all fall into cases 3 and 4 and should not produce a warning.
30+
31+
mod private {
32+
pub struct Public {
33+
/// [`super::PublicType`]
34+
/// [`super::PrivateType`]
35+
pub public_item: u32,
36+
37+
/// [`super::PublicType`]
38+
/// [`super::PrivateType`]
39+
private_item: u32,
40+
}
41+
}

0 commit comments

Comments
 (0)