3 use crate::core::DocContext;
4 use crate::visit::DocVisitor;
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(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate {
18 let synth_impls = cx.sess().time("collect_synthetic_impls", || {
19 let mut synth = SyntheticImplCollector { cx, impls: Vec::new() };
20 synth.visit_crate(&krate);
24 let prims: FxHashSet<PrimitiveType> = krate.primitives.iter().map(|p| p.1).collect();
27 let mut coll = ItemCollector::new();
28 cx.sess().time("collect_items_for_trait_impls", || coll.visit_crate(&krate));
32 let mut new_items = Vec::new();
34 for &cnum in cx.tcx.crates(()).iter() {
35 for &(did, _) in cx.tcx.all_trait_implementations(cnum).iter() {
36 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 };
56 let mut type_did_to_deref_target: FxHashMap<DefId, &Type> = FxHashMap::default();
58 // Follow all `Deref` targets of included items and recursively add them as valid
61 map: &FxHashMap<DefId, &Type>,
62 cleaner: &mut BadImplStripper,
65 if let Some(target) = map.get(&type_did) {
66 debug!("add_deref_target: type {:?}, target {:?}", type_did, target);
67 if let Some(target_prim) = target.primitive_type() {
68 cleaner.prims.insert(target_prim);
69 } else if let Some(target_did) = target.def_id(&cx.cache) {
70 // `impl Deref<Target = S> for S`
71 if target_did == type_did {
72 // Avoid infinite cycles
75 cleaner.items.insert(target_did.into());
76 add_deref_target(cx, map, cleaner, target_did);
81 // scan through included items ahead of time to splice in Deref targets to the "valid" sets
82 for it in &new_items {
83 if let ImplItem(Impl { ref for_, ref trait_, ref items, .. }) = *it.kind {
84 if trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait()
85 && cleaner.keep_impl(for_, true)
89 .find_map(|item| match *item.kind {
90 TypedefItem(ref t, true) => Some(&t.type_),
93 .expect("Deref impl without Target type");
95 if let Some(prim) = target.primitive_type() {
96 cleaner.prims.insert(prim);
97 } else if let Some(did) = target.def_id(&cx.cache) {
98 cleaner.items.insert(did.into());
100 if let Some(for_did) = for_.def_id_no_primitives() {
101 if type_did_to_deref_target.insert(for_did, target).is_none() {
102 // Since only the `DefId` portion of the `Type` instances is known to be same for both the
103 // `Deref` target type and the impl for type positions, this map of types is keyed by
104 // `DefId` and for convenience uses a special cleaner that accepts `DefId`s directly.
105 if cleaner.keep_impl_with_def_id(for_did.into()) {
106 add_deref_target(cx, &type_did_to_deref_target, &mut cleaner, for_did);
114 new_items.retain(|it| {
115 if let ImplItem(Impl { ref for_, ref trait_, ref kind, .. }) = *it.kind {
118 trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait(),
119 ) || trait_.as_ref().map_or(false, |t| cleaner.keep_impl_with_def_id(t.def_id().into()))
126 // `tcx.crates(())` doesn't include the local crate, and `tcx.all_trait_implementations`
127 // doesn't work with it anyway, so pull them from the HIR map instead
128 let mut extra_attrs = Vec::new();
129 for &trait_did in cx.tcx.all_traits(()).iter() {
130 for &impl_did in cx.tcx.hir().trait_impls(trait_did) {
131 let impl_did = impl_did.to_def_id();
132 cx.tcx.sess.prof.generic_activity("build_local_trait_impl").run(|| {
133 let mut parent = cx.tcx.parent(impl_did);
134 while let Some(did) = parent {
139 .filter(|attr| attr.has_name(sym::doc))
141 if let Some([attr]) = attr.meta_item_list().as_deref() {
142 attr.has_name(sym::cfg)
149 parent = cx.tcx.parent(did);
151 inline::build_impl(cx, None, impl_did, Some(&extra_attrs), &mut new_items);
157 if let ModuleItem(Module { items, .. }) = &mut *krate.module.kind {
158 items.extend(synth_impls);
159 items.extend(new_items);
161 panic!("collect-trait-impls can't run");
167 struct SyntheticImplCollector<'a, 'tcx> {
168 cx: &'a mut DocContext<'tcx>,
172 impl<'a, 'tcx> DocVisitor for SyntheticImplCollector<'a, 'tcx> {
173 fn visit_item(&mut self, i: &Item) {
174 if i.is_struct() || i.is_enum() || i.is_union() {
175 // FIXME(eddyb) is this `doc(hidden)` check needed?
179 .get_attrs(i.def_id.expect_def_id())
181 .has_word(sym::hidden)
184 .extend(get_auto_trait_and_blanket_impls(self.cx, i.def_id.expect_def_id()));
188 self.visit_item_recur(i)
193 struct ItemCollector {
194 items: FxHashSet<ItemId>,
203 impl DocVisitor for ItemCollector {
204 fn visit_item(&mut self, i: &Item) {
205 self.items.insert(i.def_id);
207 self.visit_item_recur(i)
211 struct BadImplStripper {
212 prims: FxHashSet<PrimitiveType>,
213 items: FxHashSet<ItemId>,
216 impl BadImplStripper {
217 fn keep_impl(&self, ty: &Type, is_deref: bool) -> bool {
218 if let Generic(_) = ty {
219 // keep impls made on generics
221 } else if let Some(prim) = ty.primitive_type() {
222 self.prims.contains(&prim)
223 } else if let Some(did) = ty.def_id_no_primitives() {
224 is_deref || self.keep_impl_with_def_id(did.into())
230 fn keep_impl_with_def_id(&self, did: ItemId) -> bool {
231 self.items.contains(&did)