From 4623d51573b22c95ec25c2041aa43221a9e61597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Thu, 7 Apr 2022 16:50:51 +0200 Subject: [PATCH] Hide cross-crate doc-hidden assoc items in trait impls --- src/librustdoc/clean/inline.rs | 21 +++++++++++++---- .../cross-crate-hidden-assoc-trait-items.rs | 19 +++++++++++++++ .../cross-crate-hidden-assoc-trait-items.rs | 23 +++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/test/rustdoc/auxiliary/cross-crate-hidden-assoc-trait-items.rs create mode 100644 src/test/rustdoc/cross-crate-hidden-assoc-trait-items.rs diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 8c19cf973fc86..d06e4fa1cc2f5 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -425,13 +425,26 @@ crate fn build_impl( None => ( tcx.associated_items(did) .in_definition_order() - .filter_map(|item| { - if associated_trait.is_some() || item.vis.is_public() { - Some(item.clean(cx)) + .filter(|item| { + // If this is a trait impl, filter out associated items whose corresponding item + // in the associated trait is marked `doc(hidden)`. + // If this is an inherent impl, filter out private associated items. + if let Some(associated_trait) = associated_trait { + let trait_item = tcx + .associated_items(associated_trait.def_id) + .find_by_name_and_kind( + tcx, + item.ident(tcx), + item.kind, + associated_trait.def_id, + ) + .unwrap(); // corresponding associated item has to exist + !tcx.is_doc_hidden(trait_item.def_id) } else { - None + item.vis.is_public() } }) + .map(|item| item.clean(cx)) .collect::>(), clean::enter_impl_trait(cx, |cx| { clean_ty_generics(cx, tcx.generics_of(did), predicates) diff --git a/src/test/rustdoc/auxiliary/cross-crate-hidden-assoc-trait-items.rs b/src/test/rustdoc/auxiliary/cross-crate-hidden-assoc-trait-items.rs new file mode 100644 index 0000000000000..3baf8a6c07ee8 --- /dev/null +++ b/src/test/rustdoc/auxiliary/cross-crate-hidden-assoc-trait-items.rs @@ -0,0 +1,19 @@ +pub trait Tr { + type VisibleAssoc; + #[doc(hidden)] + type HiddenAssoc; + + const VISIBLE_ASSOC: (); + #[doc(hidden)] + const HIDDEN_ASSOC: (); +} + +pub struct Ty; + +impl Tr for Ty { + type VisibleAssoc = (); + type HiddenAssoc = (); + + const VISIBLE_ASSOC: () = (); + const HIDDEN_ASSOC: () = (); +} diff --git a/src/test/rustdoc/cross-crate-hidden-assoc-trait-items.rs b/src/test/rustdoc/cross-crate-hidden-assoc-trait-items.rs new file mode 100644 index 0000000000000..d02bc4fe71250 --- /dev/null +++ b/src/test/rustdoc/cross-crate-hidden-assoc-trait-items.rs @@ -0,0 +1,23 @@ +// Regression test for issue #95717 +// Hide cross-crate `#[doc(hidden)]` associated items in trait impls. + +#![crate_name = "dependent"] +// edition:2021 +// aux-crate:dependency=cross-crate-hidden-assoc-trait-items.rs + +// The trait `Tr` contains 2 hidden and 2 visisible associated items. +// Instead of checking for the absence of the hidden items, check for the presence of the +// visible items instead and assert that there are *exactly two* associated items +// (by counting the number of `section`s). This is more robust and future-proof. + +// @has dependent/struct.Ty.html +// @has - '//*[@id="associatedtype.VisibleAssoc"]' 'type VisibleAssoc = ()' +// @has - '//*[@id="associatedconstant.VISIBLE_ASSOC"]' 'const VISIBLE_ASSOC: ()' +// @count - '//*[@class="impl-items"]/section' 2 + +// @has dependent/trait.Tr.html +// @has - '//*[@id="associatedtype.VisibleAssoc-1"]' 'type VisibleAssoc = ()' +// @has - '//*[@id="associatedconstant.VISIBLE_ASSOC-1"]' 'const VISIBLE_ASSOC: ()' +// @count - '//*[@class="impl-items"]/section' 2 + +pub use dependency::{Tr, Ty};