Skip to content

Commit

Permalink
Rollup merge of rust-lang#95769 - fmease:fix-issue-95717, r=Guillaume…
Browse files Browse the repository at this point in the history
…Gomez

Hide cross-crate `#[doc(hidden)]` associated items in trait impls

Fixes rust-lang#95717.

r? `@GuillaumeGomez`
This is the bug I ran into in rust-lang#95316.

`@rustbot` label T-rustdoc A-cross-crate-reexports
  • Loading branch information
Dylan-DPC authored Apr 8, 2022
2 parents 00c288c + 4623d51 commit e931b09
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>(),
clean::enter_impl_trait(cx, |cx| {
clean_ty_generics(cx, tcx.generics_of(did), predicates)
Expand Down
19 changes: 19 additions & 0 deletions src/test/rustdoc/auxiliary/cross-crate-hidden-assoc-trait-items.rs
Original file line number Diff line number Diff line change
@@ -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: () = ();
}
23 changes: 23 additions & 0 deletions src/test/rustdoc/cross-crate-hidden-assoc-trait-items.rs
Original file line number Diff line number Diff line change
@@ -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};

0 comments on commit e931b09

Please sign in to comment.