3 use crate::core::DocContext;
4 use crate::fold::DocFolder;
6 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7 use rustc_hir::def_id::DefId;
8 use rustc_middle::ty::DefIdTree;
9 use rustc_span::symbol::sym;
11 crate const COLLECT_TRAIT_IMPLS: Pass = Pass {
12 name: "collect-trait-impls",
13 run: collect_trait_impls,
14 description: "retrieves trait impls for items in the crate",
17 crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
18 let (mut krate, synth_impls) = cx.sess().time("collect_synthetic_impls", || {
19 let mut synth = SyntheticImplCollector { cx, impls: Vec::new() };
20 (synth.fold_crate(krate), synth.impls)
23 let prims: FxHashSet<PrimitiveType> = krate.primitives.iter().map(|p| p.1).collect();
26 let mut coll = ItemCollector::new();
27 krate = cx.sess().time("collect_items_for_trait_impls", || coll.fold_crate(krate));
31 let mut new_items = Vec::new();
33 for &cnum in cx.tcx.crates(()).iter() {
34 for &(did, _) in cx.tcx.all_trait_implementations(cnum).iter() {
35 inline::build_impl(cx, None, did, None, &mut new_items);
39 // Also try to inline primitive impls from other crates.
40 for &def_id in PrimitiveType::all_impls(cx.tcx).values().flatten() {
41 if !def_id.is_local() {
42 cx.tcx.sess.prof.generic_activity("build_primitive_trait_impls").run(|| {
43 inline::build_impl(cx, None, def_id, None, &mut new_items);
45 // FIXME(eddyb) is this `doc(hidden)` check needed?
46 if !cx.tcx.get_attrs(def_id).lists(sym::doc).has_word(sym::hidden) {
47 let impls = get_auto_trait_and_blanket_impls(cx, def_id);
48 new_items.extend(impls.filter(|i| cx.inlined.insert(i.def_id)));
54 let mut cleaner = BadImplStripper { prims, items: crate_items };
55 let mut type_did_to_deref_target: FxHashMap<DefId, &Type> = FxHashMap::default();
57 // Follow all `Deref` targets of included items and recursively add them as valid
59 map: &FxHashMap<DefId, &Type>,
60 cleaner: &mut BadImplStripper,
63 if let Some(target) = map.get(&type_did) {
64 debug!("add_deref_target: type {:?}, target {:?}", type_did, target);
65 if let Some(target_prim) = target.primitive_type() {
66 cleaner.prims.insert(target_prim);
67 } else if let Some(target_did) = target.def_id_no_primitives() {
68 // `impl Deref<Target = S> for S`
69 if target_did == type_did {
70 // Avoid infinite cycles
73 cleaner.items.insert(target_did.into());
74 add_deref_target(map, cleaner, target_did);
79 // scan through included items ahead of time to splice in Deref targets to the "valid" sets
80 for it in &new_items {
81 if let ImplItem(Impl { ref for_, ref trait_, ref items, .. }) = *it.kind {
82 if trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait()
83 && cleaner.keep_impl(for_, true)
87 .find_map(|item| match *item.kind {
88 TypedefItem(ref t, true) => Some(&t.type_),
91 .expect("Deref impl without Target type");
93 if let Some(prim) = target.primitive_type() {
94 cleaner.prims.insert(prim);
95 } else if let Some(did) = target.def_id(&cx.cache) {
96 cleaner.items.insert(did.into());
98 if let Some(for_did) = for_.def_id_no_primitives() {
99 if type_did_to_deref_target.insert(for_did, target).is_none() {
100 // Since only the `DefId` portion of the `Type` instances is known to be same for both the
101 // `Deref` target type and the impl for type positions, this map of types is keyed by
102 // `DefId` and for convenience uses a special cleaner that accepts `DefId`s directly.
103 if cleaner.keep_impl_with_def_id(for_did.into()) {
104 add_deref_target(&type_did_to_deref_target, &mut cleaner, for_did);
112 new_items.retain(|it| {
113 if let ImplItem(Impl { ref for_, ref trait_, ref blanket_impl, .. }) = *it.kind {
116 trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait(),
117 ) || trait_.as_ref().map_or(false, |t| cleaner.keep_impl_with_def_id(t.def_id().into()))
118 || blanket_impl.is_some()
124 // `tcx.crates(())` doesn't include the local crate, and `tcx.all_trait_implementations`
125 // doesn't work with it anyway, so pull them from the HIR map instead
126 let mut extra_attrs = Vec::new();
127 for &trait_did in cx.tcx.all_traits(()).iter() {
128 for &impl_did in cx.tcx.hir().trait_impls(trait_did) {
129 let impl_did = impl_did.to_def_id();
130 cx.tcx.sess.prof.generic_activity("build_local_trait_impl").run(|| {
131 let mut parent = cx.tcx.parent(impl_did);
132 while let Some(did) = parent {
137 .filter(|attr| attr.has_name(sym::doc))
139 if let Some([attr]) = attr.meta_item_list().as_deref() {
140 attr.has_name(sym::cfg)
147 parent = cx.tcx.parent(did);
149 inline::build_impl(cx, None, impl_did, Some(&extra_attrs), &mut new_items);
155 let items = if let ModuleItem(Module { ref mut items, .. }) = *krate.module.kind {
158 panic!("collect-trait-impls can't run");
161 items.extend(synth_impls);
162 items.extend(new_items);
166 struct SyntheticImplCollector<'a, 'tcx> {
167 cx: &'a mut DocContext<'tcx>,
171 impl<'a, 'tcx> DocFolder for SyntheticImplCollector<'a, 'tcx> {
172 fn fold_item(&mut self, i: Item) -> Option<Item> {
173 if i.is_struct() || i.is_enum() || i.is_union() {
174 // FIXME(eddyb) is this `doc(hidden)` check needed?
178 .get_attrs(i.def_id.expect_def_id())
180 .has_word(sym::hidden)
183 .extend(get_auto_trait_and_blanket_impls(self.cx, i.def_id.expect_def_id()));
187 Some(self.fold_item_recur(i))
192 struct ItemCollector {
193 items: FxHashSet<ItemId>,
202 impl DocFolder for ItemCollector {
203 fn fold_item(&mut self, i: Item) -> Option<Item> {
204 self.items.insert(i.def_id);
206 Some(self.fold_item_recur(i))
210 struct BadImplStripper {
211 prims: FxHashSet<PrimitiveType>,
212 items: FxHashSet<ItemId>,
215 impl BadImplStripper {
216 fn keep_impl(&self, ty: &Type, is_deref: bool) -> bool {
217 if let Generic(_) = ty {
218 // keep impls made on generics
220 } else if let Some(prim) = ty.primitive_type() {
221 self.prims.contains(&prim)
222 } else if let Some(did) = ty.def_id_no_primitives() {
223 is_deref || self.keep_impl_with_def_id(did.into())
229 fn keep_impl_with_def_id(&self, did: ItemId) -> bool {
230 self.items.contains(&did)