Skip to content

Commit

Permalink
rustdoc: Allow linking from private items to private types
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dennis-hamester committed Jul 8, 2020
1 parent 8ac1525 commit c8b16cd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {

let hir_id = self.cx.tcx.hir().as_local_hir_id(local);
if !self.cx.tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_id)
&& (item.visibility == Visibility::Public)
&& !self.cx.render_options.document_private
{
let item_name = item.name.as_deref().unwrap_or("<unknown>");
Expand Down
10 changes: 10 additions & 0 deletions src/test/rustdoc/issue-74134-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![deny(intra_doc_link_resolution_failure)]

// Linking from a private item to a private type is fine without --document-private-items.

struct Private;

pub struct Public {
/// [`Private`]
private: Private,
}
11 changes: 11 additions & 0 deletions src/test/rustdoc/issue-74134-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// compile-flags: --document-private-items
#![deny(intra_doc_link_resolution_failure)]

// Linking from a private item to a private type is fine with --document-private-items.

struct Private;

pub struct Public {
/// [`Private`]
private: Private,
}
11 changes: 11 additions & 0 deletions src/test/rustdoc/issue-74134-3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// should-fail
#![deny(intra_doc_link_resolution_failure)]

// Linking from a public item to a private type fails without --document-private-items.

struct Private;

pub struct Public {
/// [`Private`]
pub public: u32,
}
11 changes: 11 additions & 0 deletions src/test/rustdoc/issue-74134-4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// compile-flags: --document-private-items
#![deny(intra_doc_link_resolution_failure)]

// Linking from a public item to a private type is fine with --document-private-items.

struct Private;

pub struct Public {
/// [`Private`]
pub public: u32,
}

0 comments on commit c8b16cd

Please sign in to comment.