]>
Commit | Line | Data |
---|---|---|
dfeec247 | 1 | use rustc_hir as hir; |
353b0b11 | 2 | use rustc_infer::infer::{DefineOpaqueTypes, InferOk, TyCtxtInferExt}; |
74b04a01 | 3 | use rustc_infer::traits; |
31ef2f64 | 4 | use rustc_middle::ty::{self, Upcast}; |
e8be2606 | 5 | use rustc_span::def_id::DefId; |
dfeec247 | 6 | use rustc_span::DUMMY_SP; |
e8be2606 | 7 | use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt; |
b7449926 | 8 | |
e8be2606 | 9 | use thin_vec::ThinVec; |
b7449926 | 10 | |
e8be2606 FG |
11 | use crate::clean; |
12 | use crate::clean::{ | |
31ef2f64 | 13 | clean_middle_assoc_item, clean_middle_ty, clean_trait_ref_with_constraints, clean_ty_generics, |
e8be2606 FG |
14 | }; |
15 | use crate::core::DocContext; | |
16 | ||
17 | #[instrument(level = "debug", skip(cx))] | |
18 | pub(crate) fn synthesize_blanket_impls( | |
19 | cx: &mut DocContext<'_>, | |
20 | item_def_id: DefId, | |
21 | ) -> Vec<clean::Item> { | |
22 | let tcx = cx.tcx; | |
23 | let ty = tcx.type_of(item_def_id); | |
b7449926 | 24 | |
e8be2606 FG |
25 | let mut blanket_impls = Vec::new(); |
26 | for trait_def_id in tcx.all_traits() { | |
27 | if !cx.cache.effective_visibilities.is_reachable(tcx, trait_def_id) | |
28 | || cx.generated_synthetics.get(&(ty.skip_binder(), trait_def_id)).is_some() | |
29 | { | |
30 | continue; | |
31 | } | |
32 | // NOTE: doesn't use `for_each_relevant_impl` to avoid looking at anything besides blanket impls | |
33 | let trait_impls = tcx.trait_impls_of(trait_def_id); | |
34 | 'blanket_impls: for &impl_def_id in trait_impls.blanket_impls() { | |
35 | trace!("considering impl `{impl_def_id:?}` for trait `{trait_def_id:?}`"); | |
48663c56 | 36 | |
e8be2606 FG |
37 | let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap(); |
38 | if !matches!(trait_ref.skip_binder().self_ty().kind(), ty::Param(_)) { | |
2b03887a FG |
39 | continue; |
40 | } | |
e8be2606 FG |
41 | let infcx = tcx.infer_ctxt().build(); |
42 | let args = infcx.fresh_args_for_item(DUMMY_SP, item_def_id); | |
43 | let impl_ty = ty.instantiate(tcx, args); | |
44 | let param_env = ty::ParamEnv::empty(); | |
b7449926 | 45 | |
e8be2606 FG |
46 | let impl_args = infcx.fresh_args_for_item(DUMMY_SP, impl_def_id); |
47 | let impl_trait_ref = trait_ref.instantiate(tcx, impl_args); | |
b7449926 | 48 | |
e8be2606 FG |
49 | // Require the type the impl is implemented on to match |
50 | // our type, and ignore the impl if there was a mismatch. | |
51 | let Ok(eq_result) = infcx.at(&traits::ObligationCause::dummy(), param_env).eq( | |
52 | DefineOpaqueTypes::Yes, | |
53 | impl_trait_ref.self_ty(), | |
54 | impl_ty, | |
55 | ) else { | |
56 | continue; | |
57 | }; | |
58 | let InferOk { value: (), obligations } = eq_result; | |
59 | // FIXME(eddyb) ignoring `obligations` might cause false positives. | |
60 | drop(obligations); | |
b7449926 | 61 | |
e8be2606 FG |
62 | let predicates = tcx |
63 | .predicates_of(impl_def_id) | |
64 | .instantiate(tcx, impl_args) | |
65 | .predicates | |
66 | .into_iter() | |
31ef2f64 | 67 | .chain(Some(impl_trait_ref.upcast(tcx))); |
e8be2606 FG |
68 | for predicate in predicates { |
69 | let obligation = traits::Obligation::new( | |
70 | tcx, | |
71 | traits::ObligationCause::dummy(), | |
2b03887a | 72 | param_env, |
e8be2606 | 73 | predicate, |
2b03887a | 74 | ); |
e8be2606 FG |
75 | match infcx.evaluate_obligation(&obligation) { |
76 | Ok(eval_result) if eval_result.may_apply() => {} | |
77 | Err(traits::OverflowError::Canonical) => {} | |
78 | _ => continue 'blanket_impls, | |
2b03887a | 79 | } |
e8be2606 FG |
80 | } |
81 | debug!("found applicable impl for trait ref {trait_ref:?}"); | |
48663c56 | 82 | |
e8be2606 | 83 | cx.generated_synthetics.insert((ty.skip_binder(), trait_def_id)); |
48663c56 | 84 | |
e8be2606 FG |
85 | blanket_impls.push(clean::Item { |
86 | name: None, | |
87 | attrs: Default::default(), | |
88 | item_id: clean::ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id }, | |
89 | kind: Box::new(clean::ImplItem(Box::new(clean::Impl { | |
31ef2f64 | 90 | safety: hir::Safety::Safe, |
e8be2606 FG |
91 | generics: clean_ty_generics( |
92 | cx, | |
93 | tcx.generics_of(impl_def_id), | |
94 | tcx.explicit_predicates_of(impl_def_id), | |
95 | ), | |
96 | // FIXME(eddyb) compute both `trait_` and `for_` from | |
97 | // the post-inference `trait_ref`, as it's more accurate. | |
31ef2f64 | 98 | trait_: Some(clean_trait_ref_with_constraints( |
e8be2606 FG |
99 | cx, |
100 | ty::Binder::dummy(trait_ref.instantiate_identity()), | |
101 | ThinVec::new(), | |
102 | )), | |
103 | for_: clean_middle_ty( | |
104 | ty::Binder::dummy(ty.instantiate_identity()), | |
105 | cx, | |
106 | None, | |
107 | None, | |
108 | ), | |
109 | items: tcx | |
110 | .associated_items(impl_def_id) | |
111 | .in_definition_order() | |
112 | .filter(|item| !item.is_impl_trait_in_trait()) | |
113 | .map(|item| clean_middle_assoc_item(item, cx)) | |
114 | .collect(), | |
115 | polarity: ty::ImplPolarity::Positive, | |
116 | kind: clean::ImplKind::Blanket(Box::new(clean_middle_ty( | |
117 | ty::Binder::dummy(trait_ref.instantiate_identity().self_ty()), | |
118 | cx, | |
119 | None, | |
120 | None, | |
121 | ))), | |
122 | }))), | |
123 | cfg: None, | |
124 | inline_stmt_id: None, | |
125 | }); | |
2b03887a | 126 | } |
b7449926 | 127 | } |
e8be2606 FG |
128 | |
129 | blanket_impls | |
b7449926 | 130 | } |