Skip to content

Commit ed53de0

Browse files
rustdoc: Always warn when linking from public to private items
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.
1 parent 9e92106 commit ed53de0

6 files changed

+36
-10
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -790,13 +790,20 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
790790
item.attrs.links.push((ori_link, None, fragment));
791791
} else {
792792
debug!("intra-doc link to {} resolved to {:?}", path_str, res);
793-
if let Some(local) = res.opt_def_id().and_then(|def_id| def_id.as_local()) {
793+
794+
// item can be non-local e.g. when using #[doc(primitive = "pointer")]
795+
if let Some((src_id, dst_id)) = res
796+
.opt_def_id()
797+
.and_then(|def_id| def_id.as_local())
798+
.and_then(|dst_id| item.def_id.as_local().map(|src_id| (src_id, dst_id)))
799+
{
794800
use rustc_hir::def_id::LOCAL_CRATE;
795801

796-
let hir_id = self.cx.tcx.hir().as_local_hir_id(local);
797-
if !self.cx.tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_id)
798-
&& (item.visibility == Visibility::Public)
799-
&& !self.cx.render_options.document_private
802+
let hir_src = self.cx.tcx.hir().as_local_hir_id(src_id);
803+
let hir_dst = self.cx.tcx.hir().as_local_hir_id(dst_id);
804+
805+
if self.cx.tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_src)
806+
&& !self.cx.tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_dst)
800807
{
801808
privacy_error(cx, &item, &path_str, &dox, link_range);
802809
continue;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: public documentation for `DocMe` links to private item `DontDocMe`
2+
--> $DIR/intra-links-private.rs:5:11
3+
|
4+
LL | /// docs [DontDocMe]
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/intra-links-private.public.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: public documentation for `DocMe` links to private item `DontDocMe`
2-
--> $DIR/intra-links-private.rs:6:11
2+
--> $DIR/intra-links-private.rs:5:11
33
|
44
LL | /// docs [DontDocMe]
55
| ^^^^^^^^^ this item is private
+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// check-pass
22
// revisions: public private
33
// [private]compile-flags: --document-private-items
4-
#![cfg_attr(private, deny(intra_doc_link_resolution_failure))]
54

65
/// docs [DontDocMe]
7-
//[public]~^ WARNING public documentation for `DocMe` links to private item `DontDocMe`
6+
//~^ WARNING public documentation for `DocMe` links to private item `DontDocMe`
87
// FIXME: for [private] we should also make sure the link was actually generated
98
pub struct DocMe;
109
struct DontDocMe;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: public documentation for `public_item` links to private item `PrivateType`
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// There are 4 cases here:
66
// 1. public item -> public type: no warning
7-
// 2. public item -> private type: warning, if --document-private-items is not passed
7+
// 2. public item -> private type: warning
88
// 3. private item -> public type: no warning
99
// 4. private item -> private type: no warning
1010
// All 4 cases are tested with and without --document-private-items.
@@ -17,7 +17,7 @@ pub struct PublicType;
1717
pub struct Public {
1818
/// [`PublicType`]
1919
/// [`PrivateType`]
20-
//[public]~^ WARNING public documentation for `public_item` links to private item `PrivateType`
20+
//~^ WARNING public documentation for `public_item` links to private item `PrivateType`
2121
pub public_item: u32,
2222

2323
/// [`PublicType`]

0 commit comments

Comments
 (0)