3 use crate::core::DocContext;
4 use crate::fold::DocFolder;
6 use rustc_data_structures::fx::FxHashSet;
7 use rustc_middle::ty::DefIdTree;
8 use rustc_span::symbol::sym;
10 crate const COLLECT_TRAIT_IMPLS: Pass = Pass {
11 name: "collect-trait-impls",
12 run: collect_trait_impls,
13 description: "retrieves trait impls for items in the crate",
16 crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
17 let (mut krate, synth_impls) = cx.sess().time("collect_synthetic_impls", || {
18 let mut synth = SyntheticImplCollector { cx, impls: Vec::new() };
19 (synth.fold_crate(krate), synth.impls)
22 let prims: FxHashSet<PrimitiveType> = krate.primitives.iter().map(|p| p.1).collect();
25 let mut coll = ItemCollector::new();
26 krate = cx.sess().time("collect_items_for_trait_impls", || coll.fold_crate(krate));
30 let mut new_items = Vec::new();
32 for &cnum in cx.tcx.crates(()).iter() {
33 for &(did, _) in cx.tcx.all_trait_implementations(cnum).iter() {
34 cx.tcx.sess.prof.generic_activity("build_extern_trait_impl").run(|| {
35 inline::build_impl(cx, None, did, None, &mut new_items);
40 // Also try to inline primitive impls from other crates.
41 for &def_id in PrimitiveType::all_impls(cx.tcx).values().flatten() {
42 if !def_id.is_local() {
43 cx.tcx.sess.prof.generic_activity("build_primitive_trait_impls").run(|| {
44 inline::build_impl(cx, None, def_id, None, &mut new_items);
46 // FIXME(eddyb) is this `doc(hidden)` check needed?
47 if !cx.tcx.get_attrs(def_id).lists(sym::doc).has_word(sym::hidden) {
48 let impls = get_auto_trait_and_blanket_impls(cx, def_id);
49 new_items.extend(impls.filter(|i| cx.inlined.insert(i.def_id)));
55 let mut cleaner = BadImplStripper { prims, items: crate_items };
57 // scan through included items ahead of time to splice in Deref targets to the "valid" sets
58 for it in &new_items {
59 if let ImplItem(Impl { ref for_, ref trait_, ref items, .. }) = *it.kind {
60 if cleaner.keep_impl(for_)
61 && trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait()
65 .find_map(|item| match *item.kind {
66 TypedefItem(ref t, true) => Some(&t.type_),
69 .expect("Deref impl without Target type");
71 if let Some(prim) = target.primitive_type() {
72 cleaner.prims.insert(prim);
73 } else if let Some(did) = target.def_id() {
74 cleaner.items.insert(did.into());
80 new_items.retain(|it| {
81 if let ImplItem(Impl { ref for_, ref trait_, ref blanket_impl, .. }) = *it.kind {
82 cleaner.keep_impl(for_)
85 .map_or(false, |t| cleaner.keep_impl_with_def_id(t.def_id().into()))
86 || blanket_impl.is_some()
92 // `tcx.crates(())` doesn't include the local crate, and `tcx.all_trait_implementations`
93 // doesn't work with it anyway, so pull them from the HIR map instead
94 let mut extra_attrs = Vec::new();
95 for &trait_did in cx.tcx.all_traits(()).iter() {
96 for &impl_did in cx.tcx.hir().trait_impls(trait_did) {
97 let impl_did = impl_did.to_def_id();
98 cx.tcx.sess.prof.generic_activity("build_local_trait_impl").run(|| {
99 let mut parent = cx.tcx.parent(impl_did);
100 while let Some(did) = parent {
105 .filter(|attr| attr.has_name(sym::doc))
107 if let Some([attr]) = attr.meta_item_list().as_deref() {
108 attr.has_name(sym::cfg)
115 parent = cx.tcx.parent(did);
117 inline::build_impl(cx, None, impl_did, Some(&extra_attrs), &mut new_items);
123 let items = if let ModuleItem(Module { ref mut items, .. }) = *krate.module.kind {
126 panic!("collect-trait-impls can't run");
129 items.extend(synth_impls);
130 items.extend(new_items);
134 struct SyntheticImplCollector<'a, 'tcx> {
135 cx: &'a mut DocContext<'tcx>,
139 impl<'a, 'tcx> DocFolder for SyntheticImplCollector<'a, 'tcx> {
140 fn fold_item(&mut self, i: Item) -> Option<Item> {
141 if i.is_struct() || i.is_enum() || i.is_union() {
142 // FIXME(eddyb) is this `doc(hidden)` check needed?
146 .get_attrs(i.def_id.expect_def_id())
148 .has_word(sym::hidden)
151 .extend(get_auto_trait_and_blanket_impls(self.cx, i.def_id.expect_def_id()));
155 Some(self.fold_item_recur(i))
160 struct ItemCollector {
161 items: FxHashSet<ItemId>,
170 impl DocFolder for ItemCollector {
171 fn fold_item(&mut self, i: Item) -> Option<Item> {
172 self.items.insert(i.def_id);
174 Some(self.fold_item_recur(i))
178 struct BadImplStripper {
179 prims: FxHashSet<PrimitiveType>,
180 items: FxHashSet<ItemId>,
183 impl BadImplStripper {
184 fn keep_impl(&self, ty: &Type) -> bool {
185 if let Generic(_) = ty {
186 // keep impls made on generics
188 } else if let Some(prim) = ty.primitive_type() {
189 self.prims.contains(&prim)
190 } else if let Some(did) = ty.def_id() {
191 self.keep_impl_with_def_id(did.into())
197 fn keep_impl_with_def_id(&self, did: ItemId) -> bool {
198 self.items.contains(&did)