Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: Don't show hidden trait methods #89198

Merged
merged 2 commits into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,45 @@ crate fn build_impl(
}
}

let document_hidden = cx.render_options.document_hidden;
let predicates = tcx.explicit_predicates_of(did);
let (trait_items, generics) = match impl_item {
Some(impl_) => (
impl_
.items
.iter()
.map(|item| tcx.hir().impl_item(item.id).clean(cx))
.map(|item| tcx.hir().impl_item(item.id))
.filter(|item| {
// Filter out impl items whose corresponding trait item has `doc(hidden)`
// not to document such impl items.
// For inherent impls, we don't do any filtering, because that's already done in strip_hidden.rs.

// When `--document-hidden-items` is passed, we don't
// do any filtering, too.
if document_hidden {
return true;
}
if let Some(associated_trait) = associated_trait {
let assoc_kind = match item.kind {
hir::ImplItemKind::Const(..) => ty::AssocKind::Const,
hir::ImplItemKind::Fn(..) => ty::AssocKind::Fn,
hir::ImplItemKind::TyAlias(..) => ty::AssocKind::Type,
};
let trait_item = tcx
.associated_items(associated_trait.def_id)
.find_by_name_and_kind(
tcx,
item.ident,
assoc_kind,
associated_trait.def_id,
)
.unwrap(); // SAFETY: For all impl items there exists trait item that has the same name.
!tcx.get_attrs(trait_item.def_id).lists(sym::doc).has_word(sym::hidden)
} else {
true
}
})
.map(|item| item.clean(cx))
.collect::<Vec<_>>(),
impl_.generics.clean(cx),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// compile-flags: -Z unstable-options --document-hidden-items

// test for trait methods with `doc(hidden)` with `--document-hidden-items` passed.
#![crate_name = "foo"]

// @has foo/trait.Trait.html
// @has - '//*[@id="associatedtype.Foo"]' 'type Foo'
// @has - '//*[@id="associatedtype.Bar"]' 'type Bar'
// @has - '//*[@id="tymethod.f"]' 'fn f()'
// @has - '//*[@id="tymethod.g"]' 'fn g()'
pub trait Trait {
#[doc(hidden)]
type Foo;
type Bar;
#[doc(hidden)]
fn f();
fn g();
}

// @has foo/struct.S.html
// @has - '//*[@id="associatedtype.Foo"]' 'type Foo'
// @has - '//*[@id="associatedtype.Bar"]' 'type Bar'
// @has - '//*[@id="method.f"]' 'fn f()'
// @has - '//*[@id="method.g"]' 'fn g()'
pub struct S;
impl Trait for S {
type Foo = ();
type Bar = ();
fn f() {}
fn g() {}
}
29 changes: 29 additions & 0 deletions src/test/rustdoc/hidden-trait-methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// test for trait methods with `doc(hidden)`.
#![crate_name = "foo"]

// @has foo/trait.Trait.html
// @!has - '//*[@id="associatedtype.Foo"]' 'type Foo'
// @has - '//*[@id="associatedtype.Bar"]' 'type Bar'
// @!has - '//*[@id="tymethod.f"]' 'fn f()'
// @has - '//*[@id="tymethod.g"]' 'fn g()'
pub trait Trait {
#[doc(hidden)]
type Foo;
type Bar;
#[doc(hidden)]
fn f();
fn g();
}

// @has foo/struct.S.html
// @!has - '//*[@id="associatedtype.Foo"]' 'type Foo'
// @has - '//*[@id="associatedtype.Bar"]' 'type Bar'
// @!has - '//*[@id="method.f"]' 'fn f()'
// @has - '//*[@id="method.g"]' 'fn g()'
pub struct S;
impl Trait for S {
type Foo = ();
type Bar = ();
fn f() {}
fn g() {}
}