rustc_trait_selection/
infer.rs
1use std::fmt::Debug;
2
3use rustc_hir::def_id::DefId;
4use rustc_hir::lang_items::LangItem;
5pub use rustc_infer::infer::*;
6use rustc_macros::extension;
7use rustc_middle::arena::ArenaAllocatable;
8use rustc_middle::infer::canonical::{
9 Canonical, CanonicalQueryInput, CanonicalQueryResponse, QueryResponse,
10};
11use rustc_middle::traits::query::NoSolution;
12use rustc_middle::ty::{self, GenericArg, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, Upcast};
13use rustc_span::DUMMY_SP;
14use tracing::instrument;
15
16use crate::infer::at::ToTrace;
17use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
18use crate::traits::{self, Obligation, ObligationCause, ObligationCtxt, SelectionContext};
19
20#[extension(pub trait InferCtxtExt<'tcx>)]
21impl<'tcx> InferCtxt<'tcx> {
22 fn can_eq<T: ToTrace<'tcx>>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool {
23 self.probe(|_| {
24 let ocx = ObligationCtxt::new(self);
25 let Ok(()) = ocx.eq(&ObligationCause::dummy(), param_env, a, b) else {
26 return false;
27 };
28 ocx.select_where_possible().is_empty()
29 })
30 }
31
32 fn type_is_copy_modulo_regions(&self, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> bool {
33 let ty = self.resolve_vars_if_possible(ty);
34
35 if !(param_env, ty).has_infer() {
38 return self.tcx.type_is_copy_modulo_regions(self.typing_env(param_env), ty);
39 }
40
41 let copy_def_id = self.tcx.require_lang_item(LangItem::Copy, None);
42
43 traits::type_known_to_meet_bound_modulo_regions(self, param_env, ty, copy_def_id)
48 }
49
50 fn type_is_clone_modulo_regions(&self, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> bool {
51 let ty = self.resolve_vars_if_possible(ty);
52 let clone_def_id = self.tcx.require_lang_item(LangItem::Clone, None);
53 traits::type_known_to_meet_bound_modulo_regions(self, param_env, ty, clone_def_id)
54 }
55
56 fn type_is_sized_modulo_regions(&self, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> bool {
57 let lang_item = self.tcx.require_lang_item(LangItem::Sized, None);
58 traits::type_known_to_meet_bound_modulo_regions(self, param_env, ty, lang_item)
59 }
60
61 #[instrument(level = "debug", skip(self, params), ret)]
90 fn type_implements_trait(
91 &self,
92 trait_def_id: DefId,
93 params: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
94 param_env: ty::ParamEnv<'tcx>,
95 ) -> traits::EvaluationResult {
96 let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, params);
97
98 let obligation = traits::Obligation {
99 cause: traits::ObligationCause::dummy(),
100 param_env,
101 recursion_depth: 0,
102 predicate: trait_ref.upcast(self.tcx),
103 };
104 self.evaluate_obligation(&obligation).unwrap_or(traits::EvaluationResult::EvaluatedToErr)
105 }
106
107 fn type_implements_trait_shallow(
119 &self,
120 trait_def_id: DefId,
121 ty: Ty<'tcx>,
122 param_env: ty::ParamEnv<'tcx>,
123 ) -> Option<Vec<traits::FulfillmentError<'tcx>>> {
124 self.probe(|_snapshot| {
125 let mut selcx = SelectionContext::new(self);
126 match selcx.select(&Obligation::new(
127 self.tcx,
128 ObligationCause::dummy(),
129 param_env,
130 ty::TraitRef::new(self.tcx, trait_def_id, [ty]),
131 )) {
132 Ok(Some(selection)) => {
133 let ocx = ObligationCtxt::new_with_diagnostics(self);
134 ocx.register_obligations(selection.nested_obligations());
135 Some(ocx.select_all_or_error())
136 }
137 Ok(None) | Err(_) => None,
138 }
139 })
140 }
141}
142
143#[extension(pub trait InferCtxtBuilderExt<'tcx>)]
144impl<'tcx> InferCtxtBuilder<'tcx> {
145 fn enter_canonical_trait_query<K, R>(
162 self,
163 canonical_key: &CanonicalQueryInput<'tcx, K>,
164 operation: impl FnOnce(&ObligationCtxt<'_, 'tcx>, K) -> Result<R, NoSolution>,
165 ) -> Result<CanonicalQueryResponse<'tcx, R>, NoSolution>
166 where
167 K: TypeFoldable<TyCtxt<'tcx>>,
168 R: Debug + TypeFoldable<TyCtxt<'tcx>>,
169 Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>,
170 {
171 let (infcx, key, canonical_inference_vars) =
172 self.build_with_canonical(DUMMY_SP, canonical_key);
173 let ocx = ObligationCtxt::new(&infcx);
174 let value = operation(&ocx, key)?;
175 ocx.make_canonicalized_query_response(canonical_inference_vars, value)
176 }
177}