]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/clean/blanket_impl.rs
Rollup merge of #89086 - WaffleLapkin:stabilize_iter_map_while, r=kennytm
[rust.git] / src / librustdoc / clean / blanket_impl.rs
1 use crate::rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
2 use rustc_hir as hir;
3 use rustc_infer::infer::{InferOk, TyCtxtInferExt};
4 use rustc_infer::traits;
5 use rustc_middle::ty::subst::Subst;
6 use rustc_middle::ty::{ToPredicate, WithConstness};
7 use rustc_span::DUMMY_SP;
8
9 use super::*;
10
11 crate struct BlanketImplFinder<'a, 'tcx> {
12     crate cx: &'a mut core::DocContext<'tcx>,
13 }
14
15 impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
16     crate fn get_blanket_impls(&mut self, item_def_id: DefId) -> Vec<Item> {
17         let param_env = self.cx.tcx.param_env(item_def_id);
18         let ty = self.cx.tcx.type_of(item_def_id);
19
20         trace!("get_blanket_impls({:?})", ty);
21         let mut impls = Vec::new();
22         for &trait_def_id in self.cx.tcx.all_traits(()).iter() {
23             if !self.cx.cache.access_levels.is_public(trait_def_id)
24                 || self.cx.generated_synthetics.get(&(ty, trait_def_id)).is_some()
25             {
26                 continue;
27             }
28             // NOTE: doesn't use `for_each_relevant_impl` to avoid looking at anything besides blanket impls
29             let trait_impls = self.cx.tcx.trait_impls_of(trait_def_id);
30             for &impl_def_id in trait_impls.blanket_impls() {
31                 trace!(
32                     "get_blanket_impls: Considering impl for trait '{:?}' {:?}",
33                     trait_def_id,
34                     impl_def_id
35                 );
36                 let trait_ref = self.cx.tcx.impl_trait_ref(impl_def_id).unwrap();
37                 let is_param = matches!(trait_ref.self_ty().kind(), ty::Param(_));
38                 let may_apply = is_param && self.cx.tcx.infer_ctxt().enter(|infcx| {
39                     let substs = infcx.fresh_substs_for_item(DUMMY_SP, item_def_id);
40                     let ty = ty.subst(infcx.tcx, substs);
41                     let param_env = param_env.subst(infcx.tcx, substs);
42
43                     let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
44                     let trait_ref = trait_ref.subst(infcx.tcx, impl_substs);
45
46                     // Require the type the impl is implemented on to match
47                     // our type, and ignore the impl if there was a mismatch.
48                     let cause = traits::ObligationCause::dummy();
49                     let eq_result = infcx.at(&cause, param_env).eq(trait_ref.self_ty(), ty);
50                     if let Ok(InferOk { value: (), obligations }) = eq_result {
51                         // FIXME(eddyb) ignoring `obligations` might cause false positives.
52                         drop(obligations);
53
54                         trace!(
55                             "invoking predicate_may_hold: param_env={:?}, trait_ref={:?}, ty={:?}",
56                             param_env,
57                             trait_ref,
58                             ty
59                         );
60                         let predicates = self
61                             .cx
62                             .tcx
63                             .predicates_of(impl_def_id)
64                             .instantiate(self.cx.tcx, impl_substs)
65                             .predicates
66                             .into_iter()
67                             .chain(Some(trait_ref.without_const().to_predicate(infcx.tcx)));
68                         for predicate in predicates {
69                             debug!("testing predicate {:?}", predicate);
70                             let obligation = traits::Obligation::new(
71                                 traits::ObligationCause::dummy(),
72                                 param_env,
73                                 predicate,
74                             );
75                             match infcx.evaluate_obligation(&obligation) {
76                                 Ok(eval_result) if eval_result.may_apply() => {}
77                                 Err(traits::OverflowError) => {}
78                                 _ => {
79                                     return false;
80                                 }
81                             }
82                         }
83                         true
84                     } else {
85                         false
86                     }
87                 });
88                 debug!(
89                     "get_blanket_impls: found applicable impl: {} for trait_ref={:?}, ty={:?}",
90                     may_apply, trait_ref, ty
91                 );
92                 if !may_apply {
93                     continue;
94                 }
95
96                 self.cx.generated_synthetics.insert((ty, trait_def_id));
97
98                 impls.push(Item {
99                     name: None,
100                     attrs: Default::default(),
101                     visibility: Inherited,
102                     def_id: ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
103                     kind: Box::new(ImplItem(Impl {
104                         span: Span::new(self.cx.tcx.def_span(impl_def_id)),
105                         unsafety: hir::Unsafety::Normal,
106                         generics: (
107                             self.cx.tcx.generics_of(impl_def_id),
108                             self.cx.tcx.explicit_predicates_of(impl_def_id),
109                         )
110                             .clean(self.cx),
111                         // FIXME(eddyb) compute both `trait_` and `for_` from
112                         // the post-inference `trait_ref`, as it's more accurate.
113                         trait_: Some(trait_ref.clean(self.cx).get_trait_type().unwrap()),
114                         for_: ty.clean(self.cx),
115                         items: self
116                             .cx
117                             .tcx
118                             .associated_items(impl_def_id)
119                             .in_definition_order()
120                             .collect::<Vec<_>>()
121                             .clean(self.cx),
122                         negative_polarity: false,
123                         synthetic: false,
124                         blanket_impl: Some(Box::new(trait_ref.self_ty().clean(self.cx))),
125                     })),
126                     cfg: None,
127                 });
128             }
129         }
130         impls
131     }
132 }