]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/clean/blanket_impl.rs
Auto merge of #89293 - TaKO8Ki:fix-confusing-error-for-path-separator-to-refer-to...
[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(
68                                 ty::Binder::dummy(trait_ref)
69                                     .without_const()
70                                     .to_predicate(infcx.tcx),
71                             ));
72                         for predicate in predicates {
73                             debug!("testing predicate {:?}", predicate);
74                             let obligation = traits::Obligation::new(
75                                 traits::ObligationCause::dummy(),
76                                 param_env,
77                                 predicate,
78                             );
79                             match infcx.evaluate_obligation(&obligation) {
80                                 Ok(eval_result) if eval_result.may_apply() => {}
81                                 Err(traits::OverflowError) => {}
82                                 _ => {
83                                     return false;
84                                 }
85                             }
86                         }
87                         true
88                     } else {
89                         false
90                     }
91                 });
92                 debug!(
93                     "get_blanket_impls: found applicable impl: {} for trait_ref={:?}, ty={:?}",
94                     may_apply, trait_ref, ty
95                 );
96                 if !may_apply {
97                     continue;
98                 }
99
100                 self.cx.generated_synthetics.insert((ty, trait_def_id));
101
102                 impls.push(Item {
103                     name: None,
104                     attrs: Default::default(),
105                     visibility: Inherited,
106                     def_id: ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
107                     kind: box ImplItem(Impl {
108                         span: Span::new(self.cx.tcx.def_span(impl_def_id)),
109                         unsafety: hir::Unsafety::Normal,
110                         generics: (
111                             self.cx.tcx.generics_of(impl_def_id),
112                             self.cx.tcx.explicit_predicates_of(impl_def_id),
113                         )
114                             .clean(self.cx),
115                         // FIXME(eddyb) compute both `trait_` and `for_` from
116                         // the post-inference `trait_ref`, as it's more accurate.
117                         trait_: Some(trait_ref.clean(self.cx).get_trait_type().unwrap()),
118                         for_: ty.clean(self.cx),
119                         items: self
120                             .cx
121                             .tcx
122                             .associated_items(impl_def_id)
123                             .in_definition_order()
124                             .collect::<Vec<_>>()
125                             .clean(self.cx),
126                         negative_polarity: false,
127                         synthetic: false,
128                         blanket_impl: Some(box trait_ref.self_ty().clean(self.cx)),
129                     }),
130                     cfg: None,
131                 });
132             }
133         }
134         impls
135     }
136 }