Rollup merge of #74460 - dennis-hamester:rustdoc-warn-pub-to-priv, r=jyn514
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. This came up during the discussion in https://github.com/rust-lang/rust/pull/74147#discussion_r452597901.
This commit is contained in:
commit
02e350f5a2
@ -790,13 +790,20 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
|
||||
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;
|
||||
@ -1069,6 +1076,13 @@ fn privacy_error(
|
||||
if let Some(sp) = sp {
|
||||
diag.span_label(sp, "this item is private");
|
||||
}
|
||||
|
||||
let note_msg = if cx.render_options.document_private {
|
||||
"this link resolves only because you passed `--document-private-items`, but will break without"
|
||||
} else {
|
||||
"this link will resolve properly if you pass `--document-private-items`"
|
||||
};
|
||||
diag.note(note_msg);
|
||||
});
|
||||
}
|
||||
|
||||
|
11
src/test/rustdoc-ui/intra-links-private.private.stderr
Normal file
11
src/test/rustdoc-ui/intra-links-private.private.stderr
Normal file
@ -0,0 +1,11 @@
|
||||
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
|
||||
= note: this link resolves only because you passed `--document-private-items`, but will break without
|
||||
|
||||
warning: 1 warning emitted
|
||||
|
@ -1,10 +1,11 @@
|
||||
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
|
||||
|
|
||||
= note: `#[warn(intra_doc_link_resolution_failure)]` on by default
|
||||
= note: this link will resolve properly if you pass `--document-private-items`
|
||||
|
||||
warning: 1 warning emitted
|
||||
|
||||
|
@ -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;
|
||||
|
11
src/test/rustdoc-ui/issue-74134.private.stderr
Normal file
11
src/test/rustdoc-ui/issue-74134.private.stderr
Normal file
@ -0,0 +1,11 @@
|
||||
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
|
||||
= note: this link resolves only because you passed `--document-private-items`, but will break without
|
||||
|
||||
warning: 1 warning emitted
|
||||
|
@ -5,6 +5,7 @@ LL | /// [`PrivateType`]
|
||||
| ^^^^^^^^^^^^^ this item is private
|
||||
|
|
||||
= note: `#[warn(intra_doc_link_resolution_failure)]` on by default
|
||||
= note: this link will resolve properly if you pass `--document-private-items`
|
||||
|
||||
warning: 1 warning emitted
|
||||
|
||||
|
@ -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 PublicType;
|
||||
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`]
|
||||
|
Loading…
x
Reference in New Issue
Block a user