]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/clean/blanket_impl.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / librustdoc / clean / blanket_impl.rs
CommitLineData
ba9703b0 1use crate::rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
dfeec247 2use rustc_hir as hir;
74b04a01
XL
3use rustc_infer::infer::{InferOk, TyCtxtInferExt};
4use rustc_infer::traits;
ba9703b0 5use rustc_middle::ty::subst::Subst;
a2a8927a 6use rustc_middle::ty::ToPredicate;
dfeec247 7use rustc_span::DUMMY_SP;
b7449926 8
b7449926
XL
9use super::*;
10
923072b8
FG
11pub(crate) struct BlanketImplFinder<'a, 'tcx> {
12 pub(crate) cx: &'a mut core::DocContext<'tcx>,
b7449926
XL
13}
14
532ac7d7 15impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
923072b8 16 pub(crate) fn get_blanket_impls(&mut self, item_def_id: DefId) -> Vec<Item> {
6a06907d 17 let param_env = self.cx.tcx.param_env(item_def_id);
04454e1e 18 let ty = self.cx.tcx.bound_type_of(item_def_id);
48663c56 19
c295e0f8 20 trace!("get_blanket_impls({:?})", ty);
b7449926 21 let mut impls = Vec::new();
5099ac24
FG
22 self.cx.with_all_traits(|cx, all_traits| {
23 for &trait_def_id in all_traits {
24 if !cx.cache.access_levels.is_public(trait_def_id)
04454e1e 25 || cx.generated_synthetics.get(&(ty.0, trait_def_id)).is_some()
5099ac24
FG
26 {
27 continue;
28 }
29 // NOTE: doesn't use `for_each_relevant_impl` to avoid looking at anything besides blanket impls
30 let trait_impls = cx.tcx.trait_impls_of(trait_def_id);
31 for &impl_def_id in trait_impls.blanket_impls() {
32 trace!(
33 "get_blanket_impls: Considering impl for trait '{:?}' {:?}",
34 trait_def_id,
35 impl_def_id
36 );
04454e1e
FG
37 let trait_ref = cx.tcx.bound_impl_trait_ref(impl_def_id).unwrap();
38 let is_param = matches!(trait_ref.0.self_ty().kind(), ty::Param(_));
5099ac24
FG
39 let may_apply = is_param && cx.tcx.infer_ctxt().enter(|infcx| {
40 let substs = infcx.fresh_substs_for_item(DUMMY_SP, item_def_id);
41 let ty = ty.subst(infcx.tcx, substs);
04454e1e 42 let param_env = EarlyBinder(param_env).subst(infcx.tcx, substs);
b7449926 43
5099ac24
FG
44 let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
45 let trait_ref = trait_ref.subst(infcx.tcx, impl_substs);
b7449926 46
5099ac24
FG
47 // Require the type the impl is implemented on to match
48 // our type, and ignore the impl if there was a mismatch.
49 let cause = traits::ObligationCause::dummy();
50 let eq_result = infcx.at(&cause, param_env).eq(trait_ref.self_ty(), ty);
51 if let Ok(InferOk { value: (), obligations }) = eq_result {
52 // FIXME(eddyb) ignoring `obligations` might cause false positives.
53 drop(obligations);
b7449926 54
5099ac24
FG
55 trace!(
56 "invoking predicate_may_hold: param_env={:?}, trait_ref={:?}, ty={:?}",
fc512014 57 param_env,
5099ac24
FG
58 trait_ref,
59 ty
fc512014 60 );
5099ac24
FG
61 let predicates = cx
62 .tcx
63 .predicates_of(impl_def_id)
64 .instantiate(cx.tcx, impl_substs)
65 .predicates
66 .into_iter()
67 .chain(Some(
68 ty::Binder::dummy(trait_ref)
69 .to_poly_trait_predicate()
70 .map_bound(ty::PredicateKind::Trait)
71 .to_predicate(infcx.tcx),
72 ));
73 for predicate in predicates {
74 debug!("testing predicate {:?}", predicate);
75 let obligation = traits::Obligation::new(
76 traits::ObligationCause::dummy(),
77 param_env,
78 predicate,
79 );
80 match infcx.evaluate_obligation(&obligation) {
81 Ok(eval_result) if eval_result.may_apply() => {}
82 Err(traits::OverflowError::Canonical) => {}
83 Err(traits::OverflowError::ErrorReporting) => {}
84 _ => {
85 return false;
86 }
fc512014
XL
87 }
88 }
5099ac24
FG
89 true
90 } else {
91 false
b7449926 92 }
5099ac24
FG
93 });
94 debug!(
95 "get_blanket_impls: found applicable impl: {} for trait_ref={:?}, ty={:?}",
96 may_apply, trait_ref, ty
97 );
98 if !may_apply {
99 continue;
b7449926 100 }
48663c56 101
04454e1e 102 cx.generated_synthetics.insert((ty.0, trait_def_id));
48663c56 103
5099ac24
FG
104 impls.push(Item {
105 name: None,
106 attrs: Default::default(),
107 visibility: Inherited,
04454e1e 108 item_id: ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
5099ac24
FG
109 kind: box ImplItem(Impl {
110 unsafety: hir::Unsafety::Normal,
111 generics: clean_ty_generics(
112 cx,
113 cx.tcx.generics_of(impl_def_id),
114 cx.tcx.explicit_predicates_of(impl_def_id),
115 ),
116 // FIXME(eddyb) compute both `trait_` and `for_` from
117 // the post-inference `trait_ref`, as it's more accurate.
04454e1e
FG
118 trait_: Some(trait_ref.0.clean(cx)),
119 for_: ty.0.clean(cx),
5e7ed085
FG
120 items: cx.tcx
121 .associated_items(impl_def_id)
122 .in_definition_order()
123 .map(|x| x.clean(cx))
124 .collect::<Vec<_>>(),
5099ac24 125 polarity: ty::ImplPolarity::Positive,
04454e1e 126 kind: ImplKind::Blanket(box trait_ref.0.self_ty().clean(cx)),
5099ac24
FG
127 }),
128 cfg: None,
129 });
130 }
cdc7bbd5 131 }
5099ac24
FG
132 });
133
b7449926
XL
134 impls
135 }
136}