]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/passes/collect_trait_impls.rs
Rollup merge of #90396 - b-naber:type_flags_ices_default_anon_consts, r=lcnr
[rust.git] / src / librustdoc / passes / collect_trait_impls.rs
1 use super::Pass;
2 use crate::clean::*;
3 use crate::core::DocContext;
4 use crate::fold::DocFolder;
5
6 use rustc_data_structures::fx::FxHashSet;
7 use rustc_middle::ty::DefIdTree;
8 use rustc_span::symbol::sym;
9
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",
14 };
15
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)
20     });
21
22     let prims: FxHashSet<PrimitiveType> = krate.primitives.iter().map(|p| p.1).collect();
23
24     let crate_items = {
25         let mut coll = ItemCollector::new();
26         krate = cx.sess().time("collect_items_for_trait_impls", || coll.fold_crate(krate));
27         coll.items
28     };
29
30     let mut new_items = Vec::new();
31
32     for &cnum in cx.tcx.crates(()).iter() {
33         for &(did, _) in cx.tcx.all_trait_implementations(cnum).iter() {
34             inline::build_impl(cx, None, did, None, &mut new_items);
35         }
36     }
37
38     // Also try to inline primitive impls from other crates.
39     for &def_id in PrimitiveType::all_impls(cx.tcx).values().flatten() {
40         if !def_id.is_local() {
41             cx.tcx.sess.prof.generic_activity("build_primitive_trait_impls").run(|| {
42                 inline::build_impl(cx, None, def_id, None, &mut new_items);
43
44                 // FIXME(eddyb) is this `doc(hidden)` check needed?
45                 if !cx.tcx.get_attrs(def_id).lists(sym::doc).has_word(sym::hidden) {
46                     let impls = get_auto_trait_and_blanket_impls(cx, def_id);
47                     new_items.extend(impls.filter(|i| cx.inlined.insert(i.def_id)));
48                 }
49             });
50         }
51     }
52
53     let mut cleaner = BadImplStripper { prims, items: crate_items };
54
55     // scan through included items ahead of time to splice in Deref targets to the "valid" sets
56     for it in &new_items {
57         if let ImplItem(Impl { ref for_, ref trait_, ref items, .. }) = *it.kind {
58             if cleaner.keep_impl(for_)
59                 && trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait()
60             {
61                 let target = items
62                     .iter()
63                     .find_map(|item| match *item.kind {
64                         TypedefItem(ref t, true) => Some(&t.type_),
65                         _ => None,
66                     })
67                     .expect("Deref impl without Target type");
68
69                 if let Some(prim) = target.primitive_type() {
70                     cleaner.prims.insert(prim);
71                 } else if let Some(did) = target.def_id(&cx.cache) {
72                     cleaner.items.insert(did.into());
73                 }
74             }
75         }
76     }
77
78     new_items.retain(|it| {
79         if let ImplItem(Impl { ref for_, ref trait_, ref blanket_impl, .. }) = *it.kind {
80             cleaner.keep_impl(for_)
81                 || trait_
82                     .as_ref()
83                     .map_or(false, |t| cleaner.keep_impl_with_def_id(t.def_id().into()))
84                 || blanket_impl.is_some()
85         } else {
86             true
87         }
88     });
89
90     // `tcx.crates(())` doesn't include the local crate, and `tcx.all_trait_implementations`
91     // doesn't work with it anyway, so pull them from the HIR map instead
92     let mut extra_attrs = Vec::new();
93     for &trait_did in cx.tcx.all_traits(()).iter() {
94         for &impl_did in cx.tcx.hir().trait_impls(trait_did) {
95             let impl_did = impl_did.to_def_id();
96             cx.tcx.sess.prof.generic_activity("build_local_trait_impl").run(|| {
97                 let mut parent = cx.tcx.parent(impl_did);
98                 while let Some(did) = parent {
99                     extra_attrs.extend(
100                         cx.tcx
101                             .get_attrs(did)
102                             .iter()
103                             .filter(|attr| attr.has_name(sym::doc))
104                             .filter(|attr| {
105                                 if let Some([attr]) = attr.meta_item_list().as_deref() {
106                                     attr.has_name(sym::cfg)
107                                 } else {
108                                     false
109                                 }
110                             })
111                             .cloned(),
112                     );
113                     parent = cx.tcx.parent(did);
114                 }
115                 inline::build_impl(cx, None, impl_did, Some(&extra_attrs), &mut new_items);
116                 extra_attrs.clear();
117             });
118         }
119     }
120
121     let items = if let ModuleItem(Module { ref mut items, .. }) = *krate.module.kind {
122         items
123     } else {
124         panic!("collect-trait-impls can't run");
125     };
126
127     items.extend(synth_impls);
128     items.extend(new_items);
129     krate
130 }
131
132 struct SyntheticImplCollector<'a, 'tcx> {
133     cx: &'a mut DocContext<'tcx>,
134     impls: Vec<Item>,
135 }
136
137 impl<'a, 'tcx> DocFolder for SyntheticImplCollector<'a, 'tcx> {
138     fn fold_item(&mut self, i: Item) -> Option<Item> {
139         if i.is_struct() || i.is_enum() || i.is_union() {
140             // FIXME(eddyb) is this `doc(hidden)` check needed?
141             if !self
142                 .cx
143                 .tcx
144                 .get_attrs(i.def_id.expect_def_id())
145                 .lists(sym::doc)
146                 .has_word(sym::hidden)
147             {
148                 self.impls
149                     .extend(get_auto_trait_and_blanket_impls(self.cx, i.def_id.expect_def_id()));
150             }
151         }
152
153         Some(self.fold_item_recur(i))
154     }
155 }
156
157 #[derive(Default)]
158 struct ItemCollector {
159     items: FxHashSet<ItemId>,
160 }
161
162 impl ItemCollector {
163     fn new() -> Self {
164         Self::default()
165     }
166 }
167
168 impl DocFolder for ItemCollector {
169     fn fold_item(&mut self, i: Item) -> Option<Item> {
170         self.items.insert(i.def_id);
171
172         Some(self.fold_item_recur(i))
173     }
174 }
175
176 struct BadImplStripper {
177     prims: FxHashSet<PrimitiveType>,
178     items: FxHashSet<ItemId>,
179 }
180
181 impl BadImplStripper {
182     fn keep_impl(&self, ty: &Type) -> bool {
183         if let Generic(_) = ty {
184             // keep impls made on generics
185             true
186         } else if let Some(prim) = ty.primitive_type() {
187             self.prims.contains(&prim)
188         } else if let Some(did) = ty.def_id_no_primitives() {
189             self.keep_impl_with_def_id(did.into())
190         } else {
191             false
192         }
193     }
194
195     fn keep_impl_with_def_id(&self, did: ItemId) -> bool {
196         self.items.contains(&did)
197     }
198 }