diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 4d8d0040cdcd7..4dc8cf30803e9 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1983,10 +1983,11 @@ pub struct Method { pub defaultness: Option, pub all_types: Vec, pub ret_types: Vec, + pub parent: Option } impl<'a> Clean for (&'a hir::FnSig, &'a hir::Generics, hir::BodyId, - Option) { + Option, Option) { fn clean(&self, cx: &DocContext<'_>) -> Method { let (generics, decl) = enter_impl_trait(cx, || { (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx)) @@ -1999,6 +2000,7 @@ impl<'a> Clean for (&'a hir::FnSig, &'a hir::Generics, hir::BodyId, defaultness: self.3, all_types, ret_types, + parent: self.4, } } } @@ -2314,7 +2316,7 @@ impl Clean 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, || { @@ -2349,13 +2351,28 @@ impl Clean for hir::TraitItem { impl Clean 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), @@ -2448,6 +2465,7 @@ impl Clean for ty::AssocItem { defaultness, all_types, ret_types, + parent: None }) } else { TyMethodItem(TyMethod { diff --git a/src/librustdoc/passes/strip_hidden.rs b/src/librustdoc/passes/strip_hidden.rs index 0159e03f6f299..055da248b95ca 100644 --- a/src/librustdoc/passes/strip_hidden.rs +++ b/src/librustdoc/passes/strip_hidden.rs @@ -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", @@ -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 @@ -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 { + if let clean::ItemEnum::MethodItem(ref method) = &i.inner { + if method.parent.map(|did| !self.retained.contains(&did)).unwrap_or(false) { + return None; + } + } + self.fold_item_recur(i) + } +} diff --git a/src/test/rustdoc/auxiliary/issue-13698.rs b/src/test/rustdoc/auxiliary/issue-13698.rs index a65ebfe36a497..5e0e62dcb42b9 100644 --- a/src/test/rustdoc/auxiliary/issue-13698.rs +++ b/src/test/rustdoc/auxiliary/issue-13698.rs @@ -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) {} +} diff --git a/src/test/rustdoc/issue-13698.rs b/src/test/rustdoc/issue-13698.rs index 3046a8a28627d..5bbdcde924b05 100644 --- a/src/test/rustdoc/issue-13698.rs +++ b/src/test/rustdoc/issue-13698.rs @@ -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) {} +} 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) {} +}