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

Removes references to hidden elementes in rustdoc #66715

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 21 additions & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1983,10 +1983,11 @@ pub struct Method {
pub defaultness: Option<hir::Defaultness>,
pub all_types: Vec<Type>,
pub ret_types: Vec<Type>,
pub parent: Option<DefId>
}

impl<'a> Clean<Method> for (&'a hir::FnSig, &'a hir::Generics, hir::BodyId,
Option<hir::Defaultness>) {
Option<hir::Defaultness>, Option<DefId>) {
fn clean(&self, cx: &DocContext<'_>) -> Method {
let (generics, decl) = enter_impl_trait(cx, || {
(self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
Expand All @@ -1999,6 +2000,7 @@ impl<'a> Clean<Method> for (&'a hir::FnSig, &'a hir::Generics, hir::BodyId,
defaultness: self.3,
all_types,
ret_types,
parent: self.4,
}
}
}
Expand Down Expand Up @@ -2314,7 +2316,7 @@ impl Clean<Item> for hir::TraitItem {
default.map(|e| print_const_expr(cx, e)))
}
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
MethodItem((sig, &self.generics, body, None).clean(cx))
MethodItem((sig, &self.generics, body, None, None).clean(cx))
}
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
let (generics, decl) = enter_impl_trait(cx, || {
Expand Down Expand Up @@ -2349,13 +2351,28 @@ impl Clean<Item> for hir::TraitItem {

impl Clean<Item> for hir::ImplItem {
fn clean(&self, cx: &DocContext<'_>) -> Item {
let impl_container_did = cx.tcx.opt_associated_item(self.hir_id.owner_def_id())
.and_then(|associated_item| match associated_item.container {
ty::AssocItemContainer::ImplContainer(did) => Some(did),
_ => None
});

let for_trait_did = impl_container_did.and_then(|did| cx.tcx.trait_id_of_impl(did));
let parent_item = for_trait_did
.and_then(|did| {
cx.tcx.associated_items(did).find(|item| item.ident.name == self.ident.name)
})
.map(|item| item.def_id);

let inner = match self.kind {
hir::ImplItemKind::Const(ref ty, expr) => {
AssocConstItem(ty.clean(cx),
Some(print_const_expr(cx, expr)))
}
hir::ImplItemKind::Method(ref sig, body) => {
MethodItem((sig, &self.generics, body, Some(self.defaultness)).clean(cx))
MethodItem(
(sig, &self.generics, body, Some(self.defaultness), parent_item).clean(cx)
)
}
hir::ImplItemKind::TyAlias(ref ty) => TypedefItem(Typedef {
type_: ty.clean(cx),
Expand Down Expand Up @@ -2448,6 +2465,7 @@ impl Clean<Item> for ty::AssocItem {
defaultness,
all_types,
ret_types,
parent: None
})
} else {
TyMethodItem(TyMethod {
Expand Down
19 changes: 17 additions & 2 deletions src/librustdoc/passes/strip_hidden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::clean::{self, AttributesExt, NestedAttributesExt};
use crate::clean::Item;
use crate::core::DocContext;
use crate::fold::{DocFolder, StripItem};
use crate::passes::{ImplStripper, Pass};
use crate::passes::Pass;

pub const STRIP_HIDDEN: Pass = Pass {
name: "strip-hidden",
Expand All @@ -25,7 +25,7 @@ pub fn strip_hidden(krate: clean::Crate, _: &DocContext<'_>) -> clean::Crate {
};

// strip all impls referencing stripped items
let mut stripper = ImplStripper { retained: &retained };
let mut stripper = ReferencesStripper { retained: &retained };
let krate = stripper.fold_crate(krate);

krate
Expand Down Expand Up @@ -61,3 +61,18 @@ impl<'a> DocFolder for Stripper<'a> {
self.fold_item_recur(i)
}
}

struct ReferencesStripper<'a> {
retained: &'a DefIdSet
}

impl<'a> DocFolder for ReferencesStripper<'a> {
fn fold_item(&mut self, i: Item) -> Option<Item> {
if let clean::ItemEnum::MethodItem(ref method) = &i.inner {
if method.parent.map(|did| !self.retained.contains(&did)).unwrap_or(false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if method.parent.map(|did| !self.retained.contains(&did)).unwrap_or(false) {
if method.parent.map_or(false, |did| !self.retained.contains(&did)) {

return None;
}
}
self.fold_item_recur(i)
}
}
9 changes: 7 additions & 2 deletions src/test/rustdoc/auxiliary/issue-13698.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// compile-flags: -Cmetadata=aux

pub trait Foo {
pub trait FooAux {
#[doc(hidden)]
fn foo(&self) {}

#[doc(hidden)]
fn foo2(&self);
}

impl Foo for i32 {}
impl FooAux for i32 {
fn foo2(&self) {}
}
12 changes: 10 additions & 2 deletions src/test/rustdoc/issue-13698.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ extern crate issue_13698;

pub struct Foo;
// @!has issue_13698/struct.Foo.html '//*[@id="method.foo"]' 'fn foo'
impl issue_13698::Foo for Foo {}
// @!has - '//*[@id="method.foo2"]' 'fn foo2'
impl issue_13698::FooAux for Foo {
fn foo2(&self) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: indent of 2 instead of 4. ;)

}

pub trait Bar {
#[doc(hidden)]
fn bar(&self) {}
#[doc(hidden)]
fn qux(&self);
}

// @!has issue_13698/struct.Foo.html '//*[@id="method.bar"]' 'fn bar'
impl Bar for Foo {}
// @!has - '//*[@id="method.qux"]' 'fn qux'
impl Bar for Foo {
fn qux(&self) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: indent of 2 instead of 4. ;)

}