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

Prevent showing methods from blanket impls of not available foreign traits to show up in the search results #116597

Merged
merged 3 commits into from
Oct 11, 2023
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
11 changes: 9 additions & 2 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/rustdoc-js/auxiliary/equivalent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::borrow::Borrow;

pub trait Equivalent<K: ?Sized> {
fn equivalent(&self, key: &K) -> bool;
}

impl<Q: ?Sized, K: ?Sized> Equivalent<K> for Q
where
Q: Eq,
K: Borrow<Q>,
{
fn equivalent(&self, key: &K) -> bool {
PartialEq::eq(self, key.borrow())
}
}
9 changes: 9 additions & 0 deletions tests/rustdoc-js/search-non-local-trait-impl.js
Original file line number Diff line number Diff line change
@@ -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': [],
};
8 changes: 8 additions & 0 deletions tests/rustdoc-js/search-non-local-trait-impl.rs
Original file line number Diff line number Diff line change
@@ -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;
Loading