diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 9f284486616a0..fab45e977b0df 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -442,16 +442,16 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { // `public_items` map, so we can skip inserting into the // paths map if there was already an entry present and we're // not a public item. - if !self.cache.paths.contains_key(&item.item_id.expect_def_id()) + let item_def_id = item.item_id.expect_def_id(); + if !self.cache.paths.contains_key(&item_def_id) || self .cache .effective_visibilities - .is_directly_public(self.tcx, item.item_id.expect_def_id()) + .is_directly_public(self.tcx, item_def_id) { - self.cache.paths.insert( - item.item_id.expect_def_id(), - (self.cache.stack.clone(), item.type_()), - ); + self.cache + .paths + .insert(item_def_id, (self.cache.stack.clone(), item.type_())); } } } @@ -514,9 +514,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { && adt.is_fundamental() { for ty in generics { - if let Some(did) = ty.def_id(self.cache) { - dids.insert(did); - } + dids.extend(ty.def_id(self.cache)); } } } @@ -529,32 +527,26 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { .primitive_type() .and_then(|t| self.cache.primitive_locations.get(&t).cloned()); - if let Some(did) = did { - dids.insert(did); - } + dids.extend(did); } } if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) { for bound in generics { - if let Some(did) = bound.def_id(self.cache) { - dids.insert(did); - } + dids.extend(bound.def_id(self.cache)); } } let impl_item = Impl { impl_item: item }; - if impl_item.trait_did().map_or(true, |d| self.cache.traits.contains_key(&d)) { + let impl_did = impl_item.def_id(); + let trait_did = impl_item.trait_did(); + if trait_did.map_or(true, |d| self.cache.traits.contains_key(&d)) { for did in dids { - if self.impl_ids.entry(did).or_default().insert(impl_item.def_id()) { - self.cache - .impls - .entry(did) - .or_insert_with(Vec::new) - .push(impl_item.clone()); + if self.impl_ids.entry(did).or_default().insert(impl_did) { + self.cache.impls.entry(did).or_default().push(impl_item.clone()); } } } else { - let trait_did = impl_item.trait_did().expect("no trait did"); + let trait_did = trait_did.expect("no trait did"); self.cache.orphan_trait_impls.push((trait_did, dids, impl_item)); } None diff --git a/src/librustdoc/formats/mod.rs b/src/librustdoc/formats/mod.rs index 84adca8efa997..39924c494c6e0 100644 --- a/src/librustdoc/formats/mod.rs +++ b/src/librustdoc/formats/mod.rs @@ -2,11 +2,12 @@ pub(crate) mod cache; pub(crate) mod item_type; pub(crate) mod renderer; +use cache::Cache; pub(crate) use renderer::{run_format, FormatRenderer}; use rustc_hir::def_id::DefId; +use rustc_middle::ty::TyCtxt; use crate::clean::{self, ItemId}; -use crate::html::render::Context; /// Metadata about implementations for a type or trait. #[derive(Clone, Debug)] @@ -43,8 +44,7 @@ impl Impl { // Returns true if this is an implementation on a "local" type, meaning: // the type is in the current crate, or the type and the trait are both // re-exported by the current crate. - pub(crate) fn is_on_local_type(&self, cx: &Context<'_>) -> bool { - let cache = cx.cache(); + pub(crate) fn is_on_local_type(&self, cache: &Cache, tcx: TyCtxt<'_>) -> bool { let for_type = &self.inner_impl().for_; if let Some(for_type_did) = for_type.def_id(cache) { // The "for" type is local if it's in the paths for the current crate. @@ -67,7 +67,7 @@ impl Impl { // a foreign type. To make sure that confusion doesn't pass on to // the reader, consider all implementations in std, core, and alloc // to be on local types. - let crate_name = cx.tcx().crate_name(trait_did.krate); + let crate_name = tcx.crate_name(trait_did.krate); if matches!(crate_name.as_str(), "std" | "core" | "alloc") { return true; } diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 24476e80778e1..7cab894e10437 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -980,8 +980,9 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean: } } - let (local, mut foreign) = - implementors.iter().partition::, _>(|i| i.is_on_local_type(cx)); + let (local, mut foreign) = implementors + .iter() + .partition::, _>(|i| i.is_on_local_type(cx.cache(), cx.tcx())); let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) = local.iter().partition(|i| i.inner_impl().kind.is_auto()); diff --git a/src/librustdoc/html/render/sidebar.rs b/src/librustdoc/html/render/sidebar.rs index 6e826446c0e0e..c8098f02894db 100644 --- a/src/librustdoc/html/render/sidebar.rs +++ b/src/librustdoc/html/render/sidebar.rs @@ -199,7 +199,7 @@ fn sidebar_trait<'a>( foreign_impls.extend( implementors .iter() - .filter(|i| !i.is_on_local_type(cx)) + .filter(|i| !i.is_on_local_type(cx.cache(), cx.tcx())) .filter_map(|i| super::extract_for_impl_name(&i.impl_item, cx)) .map(|(name, id)| Link::new(id, name)), ); diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 033f01864f149..584b31faaf5d6 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -217,10 +217,12 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { fn after_krate(&mut self) -> Result<(), Error> { debug!("Done with crate"); + /* debug!("Adding Primitive impls"); for primitive in Rc::clone(&self.cache).primitive_locations.values() { self.get_impls(*primitive); } + */ let e = ExternalCrate { crate_num: LOCAL_CRATE }; diff --git a/tests/rustdoc-json/primitives/no_primitive_impl_pollution.rs b/tests/rustdoc-json/primitives/no_primitive_impl_pollution.rs new file mode 100644 index 0000000000000..0a7dec0199d64 --- /dev/null +++ b/tests/rustdoc-json/primitives/no_primitive_impl_pollution.rs @@ -0,0 +1,2 @@ +//@ count "$.index[*]" 1 +fn main() {}