]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/clean/blanket_impl.rs
Rollup merge of #106641 - chenyukang:yukang/fix-105761-segguest-this, r=estebank
[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::ToPredicate;
6 use rustc_span::DUMMY_SP;
7
8 use super::*;
9
10 pub(crate) struct BlanketImplFinder<'a, 'tcx> {
11     pub(crate) cx: &'a mut core::DocContext<'tcx>,
12 }
13
14 impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
15     pub(crate) fn get_blanket_impls(&mut self, item_def_id: DefId) -> Vec<Item> {
16         let cx = &mut self.cx;
17         let param_env = cx.tcx.param_env(item_def_id);
18         let ty = cx.tcx.bound_type_of(item_def_id);
19
20         trace!("get_blanket_impls({:?})", ty);
21         let mut impls = Vec::new();
22         for trait_def_id in cx.tcx.all_traits() {
23             if !cx.cache.effective_visibilities.is_directly_public(cx.tcx, trait_def_id)
24                 || cx.generated_synthetics.get(&(ty.0, 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 = cx.tcx.trait_impls_of(trait_def_id);
30             'blanket_impls: 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 = cx.tcx.bound_impl_trait_ref(impl_def_id).unwrap();
37                 if !matches!(trait_ref.0.self_ty().kind(), ty::Param(_)) {
38                     continue;
39                 }
40                 let infcx = cx.tcx.infer_ctxt().build();
41                 let substs = infcx.fresh_substs_for_item(DUMMY_SP, item_def_id);
42                 let impl_ty = ty.subst(infcx.tcx, substs);
43                 let param_env = EarlyBinder(param_env).subst(infcx.tcx, substs);
44
45                 let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
46                 let impl_trait_ref = trait_ref.subst(infcx.tcx, impl_substs);
47
48                 // Require the type the impl is implemented on to match
49                 // our type, and ignore the impl if there was a mismatch.
50                 let cause = traits::ObligationCause::dummy();
51                 let Ok(eq_result) = infcx.at(&cause, param_env).eq(impl_trait_ref.self_ty(), impl_ty) else {
52                         continue
53                     };
54                 let InferOk { value: (), obligations } = eq_result;
55                 // FIXME(eddyb) ignoring `obligations` might cause false positives.
56                 drop(obligations);
57
58                 trace!(
59                     "invoking predicate_may_hold: param_env={:?}, impl_trait_ref={:?}, impl_ty={:?}",
60                     param_env,
61                     impl_trait_ref,
62                     impl_ty
63                 );
64                 let predicates = cx
65                     .tcx
66                     .predicates_of(impl_def_id)
67                     .instantiate(cx.tcx, impl_substs)
68                     .predicates
69                     .into_iter()
70                     .chain(Some(ty::Binder::dummy(impl_trait_ref).to_predicate(infcx.tcx)));
71                 for predicate in predicates {
72                     debug!("testing predicate {:?}", predicate);
73                     let obligation = traits::Obligation::new(
74                         infcx.tcx,
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::Canonical) => {}
82                         Err(traits::OverflowError::ErrorReporting) => {}
83                         _ => continue 'blanket_impls,
84                     }
85                 }
86                 debug!(
87                     "get_blanket_impls: found applicable impl for trait_ref={:?}, ty={:?}",
88                     trait_ref, ty
89                 );
90
91                 cx.generated_synthetics.insert((ty.0, trait_def_id));
92
93                 impls.push(Item {
94                     name: None,
95                     attrs: Default::default(),
96                     item_id: ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
97                     kind: Box::new(ImplItem(Box::new(Impl {
98                         unsafety: hir::Unsafety::Normal,
99                         generics: clean_ty_generics(
100                             cx,
101                             cx.tcx.generics_of(impl_def_id),
102                             cx.tcx.explicit_predicates_of(impl_def_id),
103                         ),
104                         // FIXME(eddyb) compute both `trait_` and `for_` from
105                         // the post-inference `trait_ref`, as it's more accurate.
106                         trait_: Some(clean_trait_ref_with_bindings(
107                             cx,
108                             ty::Binder::dummy(trait_ref.0),
109                             ThinVec::new(),
110                         )),
111                         for_: clean_middle_ty(ty::Binder::dummy(ty.0), cx, None),
112                         items: cx
113                             .tcx
114                             .associated_items(impl_def_id)
115                             .in_definition_order()
116                             .map(|x| clean_middle_assoc_item(x, cx))
117                             .collect::<Vec<_>>(),
118                         polarity: ty::ImplPolarity::Positive,
119                         kind: ImplKind::Blanket(Box::new(clean_middle_ty(
120                             ty::Binder::dummy(trait_ref.0.self_ty()),
121                             cx,
122                             None,
123                         ))),
124                     }))),
125                     cfg: None,
126                     inline_stmt_id: None,
127                 });
128             }
129         }
130
131         impls
132     }
133 }