Skip to content

Commit 3239f06

Browse files
committed
rustdoc: Don't show hidden trait methods
By skipping trait items whose attributes include `hidden`, we void showing such trait methods.
1 parent 67365d6 commit 3239f06

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

src/librustdoc/clean/inline.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,45 @@ crate fn build_impl(
389389
}
390390
}
391391

392+
let document_hidden = cx.render_options.document_hidden;
392393
let predicates = tcx.explicit_predicates_of(did);
393394
let (trait_items, generics) = match impl_item {
394395
Some(impl_) => (
395396
impl_
396397
.items
397398
.iter()
398-
.map(|item| tcx.hir().impl_item(item.id).clean(cx))
399+
.map(|item| tcx.hir().impl_item(item.id))
400+
.filter(|item| {
401+
// Filter out impl items whose corresponding trait item has `doc(hidden)`
402+
// not to document such impl items.
403+
// For inherent impls, we don't do any filtering.
404+
405+
// When `--document-hidden-items` is passed, we don't
406+
// do any filtering, too.
407+
if document_hidden {
408+
return true;
409+
}
410+
if let Some(associated_trait) = associated_trait {
411+
let assoc_kind = match item.kind {
412+
hir::ImplItemKind::Const(..) => ty::AssocKind::Const,
413+
hir::ImplItemKind::Fn(..) => ty::AssocKind::Fn,
414+
hir::ImplItemKind::TyAlias(..) => ty::AssocKind::Type,
415+
};
416+
let trait_item = tcx
417+
.associated_items(associated_trait.def_id)
418+
.find_by_name_and_kind(
419+
tcx,
420+
item.ident,
421+
assoc_kind,
422+
associated_trait.def_id,
423+
)
424+
.unwrap(); // SAFETY: For all impl items there exists trait item that has the same name.
425+
!tcx.get_attrs(trait_item.def_id).lists(sym::doc).has_word(sym::hidden)
426+
} else {
427+
true
428+
}
429+
})
430+
.map(|item| item.clean(cx))
399431
.collect::<Vec<_>>(),
400432
impl_.generics.clean(cx),
401433
),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// compile-flags: -Z unstable-options --document-hidden-items
2+
3+
// test for trait methods with `doc(hidden)` with `--document-hidden-items` passed.
4+
#![crate_name = "foo"]
5+
6+
// @has foo/trait.Trait.html
7+
// @has - '//*[@id="associatedtype.Foo"]' 'type Foo'
8+
// @has - '//*[@id="associatedtype.Bar"]' 'type Bar'
9+
// @has - '//*[@id="tymethod.f"]' 'fn f()'
10+
// @has - '//*[@id="tymethod.g"]' 'fn g()'
11+
pub trait Trait {
12+
#[doc(hidden)]
13+
type Foo;
14+
type Bar;
15+
#[doc(hidden)]
16+
fn f();
17+
fn g();
18+
}
19+
20+
// @has foo/struct.S.html
21+
// @has - '//*[@id="associatedtype.Foo"]' 'type Foo'
22+
// @has - '//*[@id="associatedtype.Bar"]' 'type Bar'
23+
// @has - '//*[@id="method.f"]' 'fn f()'
24+
// @has - '//*[@id="method.g"]' 'fn g()'
25+
pub struct S;
26+
impl Trait for S {
27+
type Foo = ();
28+
type Bar = ();
29+
fn f() {}
30+
fn g() {}
31+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// test for trait methods with `doc(hidden)`.
2+
#![crate_name = "foo"]
3+
4+
// @has foo/trait.Trait.html
5+
// @!has - '//*[@id="associatedtype.Foo"]' 'type Foo'
6+
// @has - '//*[@id="associatedtype.Bar"]' 'type Bar'
7+
// @!has - '//*[@id="tymethod.f"]' 'fn f()'
8+
// @has - '//*[@id="tymethod.g"]' 'fn g()'
9+
pub trait Trait {
10+
#[doc(hidden)]
11+
type Foo;
12+
type Bar;
13+
#[doc(hidden)]
14+
fn f();
15+
fn g();
16+
}
17+
18+
// @has foo/struct.S.html
19+
// @!has - '//*[@id="associatedtype.Foo"]' 'type Foo'
20+
// @has - '//*[@id="associatedtype.Bar"]' 'type Bar'
21+
// @!has - '//*[@id="method.f"]' 'fn f()'
22+
// @has - '//*[@id="method.g"]' 'fn g()'
23+
pub struct S;
24+
impl Trait for S {
25+
type Foo = ();
26+
type Bar = ();
27+
fn f() {}
28+
fn g() {}
29+
}

0 commit comments

Comments
 (0)