From a314707867141365e862624419b04788694b9169 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 11 Oct 2023 11:33:31 +0200 Subject: [PATCH 1/3] Prevent showing methods from blanket impls of not available foreign traits to show up in the search results --- src/librustdoc/formats/cache.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index b916baecc14..36d32837ddf 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -310,7 +310,19 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { for_: clean::Type::BorrowedRef { type_, .. }, .. } => type_.def_id(&self.cache), - ParentStackItem::Impl { for_, .. } => for_.def_id(&self.cache), + ParentStackItem::Impl { for_, trait_, .. } => { + if let Some(trait_) = trait_ { + let trait_did = trait_.def_id(); + // If this is a foreign trait impl but the trait documentation + // is not available, we should not allow the methods to show up + // in the search results. + if !trait_did.is_local() && self.tcx.is_private_dep(trait_did.krate) + { + return None; + } + } + for_.def_id(&self.cache) + } ParentStackItem::Type(item_id) => item_id.as_def_id(), }; let path = did From efac0b9c024ebd960fe0e22ea06c0c6030759bf1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 11 Oct 2023 11:33:51 +0200 Subject: [PATCH 2/3] Add regression test for #115480 --- tests/rustdoc-js/auxiliary/equivalent.rs | 15 +++++++++++++++ tests/rustdoc-js/search-non-local-trait-impl.js | 9 +++++++++ tests/rustdoc-js/search-non-local-trait-impl.rs | 8 ++++++++ 3 files changed, 32 insertions(+) create mode 100644 tests/rustdoc-js/auxiliary/equivalent.rs create mode 100644 tests/rustdoc-js/search-non-local-trait-impl.js create mode 100644 tests/rustdoc-js/search-non-local-trait-impl.rs diff --git a/tests/rustdoc-js/auxiliary/equivalent.rs b/tests/rustdoc-js/auxiliary/equivalent.rs new file mode 100644 index 00000000000..a19b5a2d44d --- /dev/null +++ b/tests/rustdoc-js/auxiliary/equivalent.rs @@ -0,0 +1,15 @@ +use std::borrow::Borrow; + +pub trait Equivalent { + fn equivalent(&self, key: &K) -> bool; +} + +impl Equivalent for Q +where + Q: Eq, + K: Borrow, +{ + fn equivalent(&self, key: &K) -> bool { + PartialEq::eq(self, key.borrow()) + } +} diff --git a/tests/rustdoc-js/search-non-local-trait-impl.js b/tests/rustdoc-js/search-non-local-trait-impl.js new file mode 100644 index 00000000000..9ebeceb69f9 --- /dev/null +++ b/tests/rustdoc-js/search-non-local-trait-impl.js @@ -0,0 +1,9 @@ +// exact-check + +// This test ensures that methods from blanket impls of not available foreign traits +// don't show up in the search results. + +const EXPECTED = { + 'query': 'equivalent', + 'others': [], +}; diff --git a/tests/rustdoc-js/search-non-local-trait-impl.rs b/tests/rustdoc-js/search-non-local-trait-impl.rs new file mode 100644 index 00000000000..462b75b0b13 --- /dev/null +++ b/tests/rustdoc-js/search-non-local-trait-impl.rs @@ -0,0 +1,8 @@ +// aux-crate:priv:equivalent=equivalent.rs +// compile-flags: -Zunstable-options --extern equivalent +// edition:2018 + +extern crate equivalent; + +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct LayoutError; From 2d37b00e24bfb1051e4a1d587bf766378d19e9b7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 11 Oct 2023 11:41:09 +0200 Subject: [PATCH 3/3] Handle private dep at the same level as masked crates --- src/librustdoc/formats/cache.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 36d32837ddf..4ccb5f2be34 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -221,16 +221,23 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { _ => self.cache.stripped_mod, }; + #[inline] + fn is_from_private_dep(tcx: TyCtxt<'_>, cache: &Cache, def_id: DefId) -> bool { + let krate = def_id.krate; + + cache.masked_crates.contains(&krate) || tcx.is_private_dep(krate) + } + // If the impl is from a masked crate or references something from a // masked crate then remove it completely. if let clean::ImplItem(ref i) = *item.kind && (self.cache.masked_crates.contains(&item.item_id.krate()) || i.trait_ .as_ref() - .map_or(false, |t| self.cache.masked_crates.contains(&t.def_id().krate)) + .map_or(false, |t| is_from_private_dep(self.tcx, self.cache, t.def_id())) || i.for_ .def_id(self.cache) - .map_or(false, |d| self.cache.masked_crates.contains(&d.krate))) + .map_or(false, |d| is_from_private_dep(self.tcx, self.cache, d))) { return None; } @@ -310,19 +317,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { for_: clean::Type::BorrowedRef { type_, .. }, .. } => type_.def_id(&self.cache), - ParentStackItem::Impl { for_, trait_, .. } => { - if let Some(trait_) = trait_ { - let trait_did = trait_.def_id(); - // If this is a foreign trait impl but the trait documentation - // is not available, we should not allow the methods to show up - // in the search results. - if !trait_did.is_local() && self.tcx.is_private_dep(trait_did.krate) - { - return None; - } - } - for_.def_id(&self.cache) - } + ParentStackItem::Impl { for_, .. } => for_.def_id(&self.cache), ParentStackItem::Type(item_id) => item_id.as_def_id(), }; let path = did