From 755c02dbd4101de94229d8e726c8a0cbcb247cbb Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Tue, 18 Sep 2018 15:42:35 -0500 Subject: [PATCH] filter collected trait impls against items in the crate --- src/librustdoc/passes/collect_trait_impls.rs | 209 +++++++++++++------ 1 file changed, 149 insertions(+), 60 deletions(-) diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index 56940483d7be2..70e1a9b0ebc9d 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -10,6 +10,9 @@ use clean::*; +use rustc::util::nodemap::FxHashSet; +use rustc::hir::def_id::DefId; + use super::Pass; use core::DocContext; use fold::DocFolder; @@ -22,71 +25,118 @@ pub fn collect_trait_impls(krate: Crate, cx: &DocContext) -> Crate { let mut synth = SyntheticImplCollector::new(cx); let mut krate = synth.fold_crate(krate); - if let Some(ref mut it) = krate.module { - if let ModuleItem(Module { ref mut items, .. }) = it.inner { - items.extend(synth.impls); + let prims: FxHashSet = + krate.primitives.iter().map(|p| p.1).collect(); - for &cnum in cx.tcx.crates().iter() { - for &did in cx.tcx.all_trait_implementations(cnum).iter() { - inline::build_impl(cx, did, items); - } - } + let crate_items = { + let mut coll = ItemCollector::new(); + krate = coll.fold_crate(krate); + coll.items + }; - // `tcx.crates()` doesn't include the local crate, and `tcx.all_trait_implementations` - // doesn't work with it anyway, so pull them from the HIR map instead - for &trait_did in cx.all_traits.iter() { - for &impl_node in cx.tcx.hir.trait_impls(trait_did) { - let impl_did = cx.tcx.hir.local_def_id(impl_node); - inline::build_impl(cx, impl_did, items); - } - } + let mut new_items = Vec::new(); + + for &cnum in cx.tcx.crates().iter() { + for &did in cx.tcx.all_trait_implementations(cnum).iter() { + inline::build_impl(cx, did, &mut new_items); + } + } - // Also try to inline primitive impls from other crates. - let lang_items = cx.tcx.lang_items(); - let primitive_impls = [ - lang_items.isize_impl(), - lang_items.i8_impl(), - lang_items.i16_impl(), - lang_items.i32_impl(), - lang_items.i64_impl(), - lang_items.i128_impl(), - lang_items.usize_impl(), - lang_items.u8_impl(), - lang_items.u16_impl(), - lang_items.u32_impl(), - lang_items.u64_impl(), - lang_items.u128_impl(), - lang_items.f32_impl(), - lang_items.f64_impl(), - lang_items.f32_runtime_impl(), - lang_items.f64_runtime_impl(), - lang_items.char_impl(), - lang_items.str_impl(), - lang_items.slice_impl(), - lang_items.slice_u8_impl(), - lang_items.str_alloc_impl(), - lang_items.slice_alloc_impl(), - lang_items.slice_u8_alloc_impl(), - lang_items.const_ptr_impl(), - lang_items.mut_ptr_impl(), - ]; - - for def_id in primitive_impls.iter().filter_map(|&def_id| def_id) { - if !def_id.is_local() { - inline::build_impl(cx, def_id, items); - - let auto_impls = get_auto_traits_with_def_id(cx, def_id); - let blanket_impls = get_blanket_impls_with_def_id(cx, def_id); - let mut renderinfo = cx.renderinfo.borrow_mut(); - - let new_impls: Vec = auto_impls.into_iter() - .chain(blanket_impls.into_iter()) - .filter(|i| renderinfo.inlined.insert(i.def_id)) - .collect(); - - items.extend(new_impls); + // Also try to inline primitive impls from other crates. + let lang_items = cx.tcx.lang_items(); + let primitive_impls = [ + lang_items.isize_impl(), + lang_items.i8_impl(), + lang_items.i16_impl(), + lang_items.i32_impl(), + lang_items.i64_impl(), + lang_items.i128_impl(), + lang_items.usize_impl(), + lang_items.u8_impl(), + lang_items.u16_impl(), + lang_items.u32_impl(), + lang_items.u64_impl(), + lang_items.u128_impl(), + lang_items.f32_impl(), + lang_items.f64_impl(), + lang_items.f32_runtime_impl(), + lang_items.f64_runtime_impl(), + lang_items.char_impl(), + lang_items.str_impl(), + lang_items.slice_impl(), + lang_items.slice_u8_impl(), + lang_items.str_alloc_impl(), + lang_items.slice_alloc_impl(), + lang_items.slice_u8_alloc_impl(), + lang_items.const_ptr_impl(), + lang_items.mut_ptr_impl(), + ]; + + for def_id in primitive_impls.iter().filter_map(|&def_id| def_id) { + if !def_id.is_local() { + inline::build_impl(cx, def_id, &mut new_items); + + let auto_impls = get_auto_traits_with_def_id(cx, def_id); + let blanket_impls = get_blanket_impls_with_def_id(cx, def_id); + let mut renderinfo = cx.renderinfo.borrow_mut(); + + let new_impls: Vec = auto_impls.into_iter() + .chain(blanket_impls.into_iter()) + .filter(|i| renderinfo.inlined.insert(i.def_id)) + .collect(); + + new_items.extend(new_impls); + } + } + + let mut cleaner = BadImplStripper { + prims, + items: crate_items, + }; + + // scan through included items ahead of time to splice in Deref targets to the "valid" sets + for it in &new_items { + if let ImplItem(Impl { ref for_, ref trait_, ref items, .. }) = it.inner { + if cleaner.keep_item(for_) && trait_.def_id() == cx.tcx.lang_items().deref_trait() { + let target = items.iter().filter_map(|item| { + match item.inner { + TypedefItem(ref t, true) => Some(&t.type_), + _ => None, + } + }).next().expect("Deref impl without Target type"); + + if let Some(prim) = target.primitive_type() { + cleaner.prims.insert(prim); + } else if let Some(did) = target.def_id() { + cleaner.items.insert(did); } } + } + } + + new_items.retain(|it| { + if let ImplItem(Impl { ref for_, ref trait_, ref blanket_impl, .. }) = it.inner { + cleaner.keep_item(for_) || + trait_.as_ref().map_or(false, |t| cleaner.keep_item(t)) || + blanket_impl.is_some() + } else { + true + } + }); + + // `tcx.crates()` doesn't include the local crate, and `tcx.all_trait_implementations` + // doesn't work with it anyway, so pull them from the HIR map instead + for &trait_did in cx.all_traits.iter() { + for &impl_node in cx.tcx.hir.trait_impls(trait_did) { + let impl_did = cx.tcx.hir.local_def_id(impl_node); + inline::build_impl(cx, impl_did, &mut new_items); + } + } + + if let Some(ref mut it) = krate.module { + if let ModuleItem(Module { ref mut items, .. }) = it.inner { + items.extend(synth.impls); + items.extend(new_items); } else { panic!("collect-trait-impls can't run"); } @@ -128,3 +178,42 @@ impl<'a, 'tcx, 'rcx, 'cstore> DocFolder for SyntheticImplCollector<'a, 'tcx, 'rc self.fold_item_recur(i) } } + +#[derive(Default)] +struct ItemCollector { + items: FxHashSet, +} + +impl ItemCollector { + fn new() -> Self { + Self::default() + } +} + +impl DocFolder for ItemCollector { + fn fold_item(&mut self, i: Item) -> Option { + self.items.insert(i.def_id); + + self.fold_item_recur(i) + } +} + +struct BadImplStripper { + prims: FxHashSet, + items: FxHashSet, +} + +impl BadImplStripper { + fn keep_item(&self, ty: &Type) -> bool { + if let Generic(_) = ty { + // keep impls made on generics + true + } else if let Some(prim) = ty.primitive_type() { + self.prims.contains(&prim) + } else if let Some(did) = ty.def_id() { + self.items.contains(&did) + } else { + false + } + } +}