]> git.lizzy.rs Git - rust.git/commitdiff
rustdoc: Always warn when linking from public to private items
authorDennis Hamester <dennis.hamester@gmail.com>
Sat, 11 Jul 2020 17:39:02 +0000 (19:39 +0200)
committerDennis Hamester <dennis.hamester@gmail.com>
Wed, 22 Jul 2020 19:36:30 +0000 (21:36 +0200)
Change the logic such that linking from a public to a private item always
triggers intra_doc_link_resolution_failure. Previously, the warning was
not emitted when --document-private-items is passed.

Also don't rely anymore on the item's visibility, which would falsely trigger
the lint now that the check for --document-private-items is gone.

src/librustdoc/passes/collect_intra_doc_links.rs
src/test/rustdoc-ui/intra-links-private.private.stderr [new file with mode: 0644]
src/test/rustdoc-ui/intra-links-private.public.stderr
src/test/rustdoc-ui/intra-links-private.rs
src/test/rustdoc-ui/issue-74134.private.stderr [new file with mode: 0644]
src/test/rustdoc-ui/issue-74134.rs

index 6f221da1168c63c5022837209d842423f625a0c9..8e113cfa563c8010ed8e3fe18bb58e386d7fc230 100644 (file)
@@ -790,13 +790,20 @@ fn fold_item(&mut self, mut item: Item) -> Option<Item> {
                 item.attrs.links.push((ori_link, None, fragment));
             } else {
                 debug!("intra-doc link to {} resolved to {:?}", path_str, res);
-                if let Some(local) = res.opt_def_id().and_then(|def_id| def_id.as_local()) {
+
+                // item can be non-local e.g. when using #[doc(primitive = "pointer")]
+                if let Some((src_id, dst_id)) = res
+                    .opt_def_id()
+                    .and_then(|def_id| def_id.as_local())
+                    .and_then(|dst_id| item.def_id.as_local().map(|src_id| (src_id, dst_id)))
+                {
                     use rustc_hir::def_id::LOCAL_CRATE;
 
-                    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 hir_src = self.cx.tcx.hir().as_local_hir_id(src_id);
+                    let hir_dst = self.cx.tcx.hir().as_local_hir_id(dst_id);
+
+                    if self.cx.tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_src)
+                        && !self.cx.tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_dst)
                     {
                         privacy_error(cx, &item, &path_str, &dox, link_range);
                         continue;
diff --git a/src/test/rustdoc-ui/intra-links-private.private.stderr b/src/test/rustdoc-ui/intra-links-private.private.stderr
new file mode 100644 (file)
index 0000000..6a23afb
--- /dev/null
@@ -0,0 +1,10 @@
+warning: public documentation for `DocMe` links to private item `DontDocMe`
+  --> $DIR/intra-links-private.rs:5:11
+   |
+LL | /// docs [DontDocMe]
+   |           ^^^^^^^^^ this item is private
+   |
+   = note: `#[warn(intra_doc_link_resolution_failure)]` on by default
+
+warning: 1 warning emitted
+
index a124435b08a99940fdd6f20f54ae61ad0157c538..6a23afb0798a9307d19ab1aeda285702be8883bf 100644 (file)
@@ -1,5 +1,5 @@
 warning: public documentation for `DocMe` links to private item `DontDocMe`
-  --> $DIR/intra-links-private.rs:6:11
+  --> $DIR/intra-links-private.rs:5:11
    |
 LL | /// docs [DontDocMe]
    |           ^^^^^^^^^ this item is private
index 1b97f6e61bd2369df84cf09f229cafaebb9692db..613236d75d2ee952e847f7ac23590a95509bfccd 100644 (file)
@@ -1,10 +1,9 @@
 // check-pass
 // revisions: public private
 // [private]compile-flags: --document-private-items
-#![cfg_attr(private, deny(intra_doc_link_resolution_failure))]
 
 /// docs [DontDocMe]
-//[public]~^ WARNING public documentation for `DocMe` links to private item `DontDocMe`
+//~^ WARNING public documentation for `DocMe` links to private item `DontDocMe`
 // FIXME: for [private] we should also make sure the link was actually generated
 pub struct DocMe;
 struct DontDocMe;
diff --git a/src/test/rustdoc-ui/issue-74134.private.stderr b/src/test/rustdoc-ui/issue-74134.private.stderr
new file mode 100644 (file)
index 0000000..3c41f7e
--- /dev/null
@@ -0,0 +1,10 @@
+warning: public documentation for `public_item` links to private item `PrivateType`
+  --> $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
+
index fe484b43fb6fce0726ed17097de87f6a4a3f9d83..b1be9123aaf838cf4750c84dc06fb23d1f01b5cd 100644 (file)
@@ -4,7 +4,7 @@
 
 // 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
+// 2. public item  -> private type: warning
 // 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.
@@ -17,7 +17,7 @@
 pub struct Public {
     /// [`PublicType`]
     /// [`PrivateType`]
-    //[public]~^ WARNING public documentation for `public_item` links to private item `PrivateType`
+    //~^ WARNING public documentation for `public_item` links to private item `PrivateType`
     pub public_item: u32,
 
     /// [`PublicType`]