Skip to content

Commit 07eb13d

Browse files
authored
Unrolled build for rust-lang#116597
Rollup merge of rust-lang#116597 - GuillaumeGomez:foreign-blanket-impl, r=notriddle Prevent showing methods from blanket impls of not available foreign traits to show up in the search results Fixes rust-lang#115480. In the case that the blanket impl trait is not available in the current crate, we prevent adding its methods in the search index. Now how I found how to fix the issue: the `equivalent` method is not generated in the documentation pages but was still added to the search index. To render impls, we iterate over `cache.impls` so I took a look at how this was generated. Inside `formats/cache.rs`, we have `CacheBuilder::populate` where we push impls into `impls` but with this condition: ```rust if cx.cache.traits.contains_key(&trait_did) { ``` I re-used this condition in `CacheBuilder::fold_item` to prevent this method from being added in `cache.search_index` or `cache.orphan_impl_items`. PS: If you want to double-check if the added test works, just comment the code I added in `cache.rs` and it should fail. r? ``@notriddle``
2 parents c1691db + 2d37b00 commit 07eb13d

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/librustdoc/formats/cache.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,23 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
221221
_ => self.cache.stripped_mod,
222222
};
223223

224+
#[inline]
225+
fn is_from_private_dep(tcx: TyCtxt<'_>, cache: &Cache, def_id: DefId) -> bool {
226+
let krate = def_id.krate;
227+
228+
cache.masked_crates.contains(&krate) || tcx.is_private_dep(krate)
229+
}
230+
224231
// If the impl is from a masked crate or references something from a
225232
// masked crate then remove it completely.
226233
if let clean::ImplItem(ref i) = *item.kind &&
227234
(self.cache.masked_crates.contains(&item.item_id.krate())
228235
|| i.trait_
229236
.as_ref()
230-
.map_or(false, |t| self.cache.masked_crates.contains(&t.def_id().krate))
237+
.map_or(false, |t| is_from_private_dep(self.tcx, self.cache, t.def_id()))
231238
|| i.for_
232239
.def_id(self.cache)
233-
.map_or(false, |d| self.cache.masked_crates.contains(&d.krate)))
240+
.map_or(false, |d| is_from_private_dep(self.tcx, self.cache, d)))
234241
{
235242
return None;
236243
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::borrow::Borrow;
2+
3+
pub trait Equivalent<K: ?Sized> {
4+
fn equivalent(&self, key: &K) -> bool;
5+
}
6+
7+
impl<Q: ?Sized, K: ?Sized> Equivalent<K> for Q
8+
where
9+
Q: Eq,
10+
K: Borrow<Q>,
11+
{
12+
fn equivalent(&self, key: &K) -> bool {
13+
PartialEq::eq(self, key.borrow())
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// exact-check
2+
3+
// This test ensures that methods from blanket impls of not available foreign traits
4+
// don't show up in the search results.
5+
6+
const EXPECTED = {
7+
'query': 'equivalent',
8+
'others': [],
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// aux-crate:priv:equivalent=equivalent.rs
2+
// compile-flags: -Zunstable-options --extern equivalent
3+
// edition:2018
4+
5+
extern crate equivalent;
6+
7+
#[derive(Clone, PartialEq, Eq, Debug)]
8+
pub struct LayoutError;

0 commit comments

Comments
 (0)