From c8b16cdbd0f06c66467c082b9d3e190255fa295d Mon Sep 17 00:00:00 2001 From: Dennis Hamester Date: Wed, 8 Jul 2020 07:39:49 +0200 Subject: [PATCH] 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. --- src/librustdoc/passes/collect_intra_doc_links.rs | 1 + src/test/rustdoc/issue-74134-1.rs | 10 ++++++++++ src/test/rustdoc/issue-74134-2.rs | 11 +++++++++++ src/test/rustdoc/issue-74134-3.rs | 11 +++++++++++ src/test/rustdoc/issue-74134-4.rs | 11 +++++++++++ 5 files changed, 44 insertions(+) create mode 100644 src/test/rustdoc/issue-74134-1.rs create mode 100644 src/test/rustdoc/issue-74134-2.rs create mode 100644 src/test/rustdoc/issue-74134-3.rs create mode 100644 src/test/rustdoc/issue-74134-4.rs diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index f1d1bf439f171..86f94af0c6e31 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -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(""); diff --git a/src/test/rustdoc/issue-74134-1.rs b/src/test/rustdoc/issue-74134-1.rs new file mode 100644 index 0000000000000..72d38638a794c --- /dev/null +++ b/src/test/rustdoc/issue-74134-1.rs @@ -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, +} diff --git a/src/test/rustdoc/issue-74134-2.rs b/src/test/rustdoc/issue-74134-2.rs new file mode 100644 index 0000000000000..f665e360b4982 --- /dev/null +++ b/src/test/rustdoc/issue-74134-2.rs @@ -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, +} diff --git a/src/test/rustdoc/issue-74134-3.rs b/src/test/rustdoc/issue-74134-3.rs new file mode 100644 index 0000000000000..b2709ecdaddda --- /dev/null +++ b/src/test/rustdoc/issue-74134-3.rs @@ -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, +} diff --git a/src/test/rustdoc/issue-74134-4.rs b/src/test/rustdoc/issue-74134-4.rs new file mode 100644 index 0000000000000..efff74f279721 --- /dev/null +++ b/src/test/rustdoc/issue-74134-4.rs @@ -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, +}