1 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
2 use crate::traits::{self, ObligationCtxt};
4 use rustc_hir::def_id::DefId;
5 use rustc_hir::lang_items::LangItem;
6 use rustc_middle::arena::ArenaAllocatable;
7 use rustc_middle::infer::canonical::{Canonical, CanonicalizedQueryResponse, QueryResponse};
8 use rustc_middle::traits::query::Fallible;
9 use rustc_middle::ty::{self, Ty, TypeFoldable, TypeVisitable};
10 use rustc_middle::ty::{GenericArg, ToPredicate};
11 use rustc_span::{Span, DUMMY_SP};
15 pub use rustc_infer::infer::*;
17 pub trait InferCtxtExt<'tcx> {
18 fn type_is_copy_modulo_regions(
20 param_env: ty::ParamEnv<'tcx>,
25 fn type_is_sized_modulo_regions(
27 param_env: ty::ParamEnv<'tcx>,
32 /// Check whether a `ty` implements given trait(trait_def_id).
35 /// - the def-id of the trait
36 /// - the type parameters of the trait, including the self-type
37 /// - the parameter environment
39 /// Invokes `evaluate_obligation`, so in the event that evaluating
40 /// `Ty: Trait` causes overflow, EvaluatedToRecur (or EvaluatedToUnknown)
42 fn type_implements_trait(
45 params: impl IntoIterator<Item = impl Into<GenericArg<'tcx>>>,
46 param_env: ty::ParamEnv<'tcx>,
47 ) -> traits::EvaluationResult;
49 impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
50 fn type_is_copy_modulo_regions(
52 param_env: ty::ParamEnv<'tcx>,
56 let ty = self.resolve_vars_if_possible(ty);
58 if !(param_env, ty).needs_infer() {
59 return ty.is_copy_modulo_regions(self.tcx, param_env);
62 let copy_def_id = self.tcx.require_lang_item(LangItem::Copy, None);
64 // This can get called from typeck (by euv), and `moves_by_default`
65 // rightly refuses to work with inference variables, but
66 // moves_by_default has a cache, which we want to use in other
68 traits::type_known_to_meet_bound_modulo_regions(self, param_env, ty, copy_def_id, span)
71 fn type_is_sized_modulo_regions(
73 param_env: ty::ParamEnv<'tcx>,
77 let lang_item = self.tcx.require_lang_item(LangItem::Sized, None);
78 traits::type_known_to_meet_bound_modulo_regions(self, param_env, ty, lang_item, span)
81 #[instrument(level = "debug", skip(self, params), ret)]
82 fn type_implements_trait(
85 params: impl IntoIterator<Item = impl Into<GenericArg<'tcx>>>,
86 param_env: ty::ParamEnv<'tcx>,
87 ) -> traits::EvaluationResult {
88 let trait_ref = self.tcx.mk_trait_ref(trait_def_id, params);
90 let obligation = traits::Obligation {
91 cause: traits::ObligationCause::dummy(),
94 predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(self.tcx),
96 self.evaluate_obligation(&obligation).unwrap_or(traits::EvaluationResult::EvaluatedToErr)
100 pub trait InferCtxtBuilderExt<'tcx> {
101 fn enter_canonical_trait_query<K, R>(
103 canonical_key: &Canonical<'tcx, K>,
104 operation: impl FnOnce(&ObligationCtxt<'_, 'tcx>, K) -> Fallible<R>,
105 ) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
107 K: TypeFoldable<'tcx>,
108 R: Debug + TypeFoldable<'tcx>,
109 Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>;
112 impl<'tcx> InferCtxtBuilderExt<'tcx> for InferCtxtBuilder<'tcx> {
113 /// The "main method" for a canonicalized trait query. Given the
114 /// canonical key `canonical_key`, this method will create a new
115 /// inference context, instantiate the key, and run your operation
116 /// `op`. The operation should yield up a result (of type `R`) as
117 /// well as a set of trait obligations that must be fully
118 /// satisfied. These obligations will be processed and the
119 /// canonical result created.
121 /// Returns `NoSolution` in the event of any error.
123 /// (It might be mildly nicer to implement this on `TyCtxt`, and
124 /// not `InferCtxtBuilder`, but that is a bit tricky right now.
125 /// In part because we would need a `for<'tcx>` sort of
126 /// bound for the closure and in part because it is convenient to
127 /// have `'tcx` be free on this function so that we can talk about
128 /// `K: TypeFoldable<'tcx>`.)
129 fn enter_canonical_trait_query<K, R>(
131 canonical_key: &Canonical<'tcx, K>,
132 operation: impl FnOnce(&ObligationCtxt<'_, 'tcx>, K) -> Fallible<R>,
133 ) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
135 K: TypeFoldable<'tcx>,
136 R: Debug + TypeFoldable<'tcx>,
137 Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>,
139 let (infcx, key, canonical_inference_vars) =
140 self.build_with_canonical(DUMMY_SP, canonical_key);
141 let ocx = ObligationCtxt::new(&infcx);
142 let value = operation(&ocx, key)?;
143 ocx.make_canonicalized_query_response(canonical_inference_vars, value)