about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2020-07-13 22:23:11 -0700
committerGitHub <noreply@github.com>2020-07-13 22:23:11 -0700
commite4a9b361267b1bcd9ead320f19ef15a6fa4e48f2 (patch)
tree27f6749555466c0b9718ff1040ad180165822bd0 /src
parent549aa03d862d45f5c33fae2ea2a2d2d537207bda (diff)
parent87895251ea372a8a776a229c45dd703b4e5b22eb (diff)
downloadrust-e4a9b361267b1bcd9ead320f19ef15a6fa4e48f2.tar.gz
rust-e4a9b361267b1bcd9ead320f19ef15a6fa4e48f2.zip
Rollup merge of #74147 - dennis-hamester:fix/issue-74134, r=jyn514
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.
Diffstat (limited to 'src')
-rw-r--r--src/librustdoc/passes/collect_intra_doc_links.rs1
-rw-r--r--src/test/rustdoc-ui/issue-74134.public.stderr10
-rw-r--r--src/test/rustdoc-ui/issue-74134.rs41
3 files changed, 52 insertions, 0 deletions
diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs
index 4fcf6ceb44d..f707c1a3e1a 100644
--- a/src/librustdoc/passes/collect_intra_doc_links.rs
+++ b/src/librustdoc/passes/collect_intra_doc_links.rs
@@ -799,6 +799,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>");
diff --git a/src/test/rustdoc-ui/issue-74134.public.stderr b/src/test/rustdoc-ui/issue-74134.public.stderr
new file mode 100644
index 00000000000..03f95f19d32
--- /dev/null
+++ b/src/test/rustdoc-ui/issue-74134.public.stderr
@@ -0,0 +1,10 @@
+warning: `[PrivateType]` public documentation for `public_item` links to a private item
+  --> $DIR/issue-74134.rs:19:10
+   |
+LL |     /// [`PrivateType`]
+   |          ^^^^^^^^^^^^^ this item is private
+   |
+   = note: `#[warn(intra_doc_link_resolution_failure)]` on by default
+
+warning: 1 warning emitted
+
diff --git a/src/test/rustdoc-ui/issue-74134.rs b/src/test/rustdoc-ui/issue-74134.rs
new file mode 100644
index 00000000000..d561c2dd890
--- /dev/null
+++ b/src/test/rustdoc-ui/issue-74134.rs
@@ -0,0 +1,41 @@
+// revisions: public private
+// [private]compile-flags: --document-private-items
+// check-pass
+
+// There are 4 cases here:
+// 1. public item  -> public type:  no warning
+// 2. public item  -> private type: warning, if --document-private-items is not passed
+// 3. private item -> public type:  no warning
+// 4. private item -> private type: no warning
+// All 4 cases are tested with and without --document-private-items.
+//
+// Case 4 without --document-private-items is the one described in issue #74134.
+
+struct PrivateType;
+pub struct PublicType;
+
+pub struct Public {
+    /// [`PublicType`]
+    /// [`PrivateType`]
+    //[public]~^ WARNING public documentation for `public_item` links to a private
+    pub public_item: u32,
+
+    /// [`PublicType`]
+    /// [`PrivateType`]
+    private_item: u32,
+}
+
+// The following cases are identical to the ones above, except that they are in a private
+// module. Thus they all fall into cases 3 and 4 and should not produce a warning.
+
+mod private {
+    pub struct Public {
+        /// [`super::PublicType`]
+        /// [`super::PrivateType`]
+        pub public_item: u32,
+
+        /// [`super::PublicType`]
+        /// [`super::PrivateType`]
+        private_item: u32,
+    }
+}