]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/infer.rs
Rollup merge of #91373 - djkoloski:fuchsia_test_suite, r=Mark-Simulacrum
[rust.git] / compiler / rustc_trait_selection / src / infer.rs
1 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
2 use crate::traits::{self, TraitEngine, TraitEngineExt};
3
4 use rustc_hir::def_id::DefId;
5 use rustc_hir::lang_items::LangItem;
6 use rustc_infer::traits::ObligationCause;
7 use rustc_middle::arena::ArenaAllocatable;
8 use rustc_middle::infer::canonical::{Canonical, CanonicalizedQueryResponse, QueryResponse};
9 use rustc_middle::traits::query::Fallible;
10 use rustc_middle::ty::subst::SubstsRef;
11 use rustc_middle::ty::ToPredicate;
12 use rustc_middle::ty::WithConstness;
13 use rustc_middle::ty::{self, Ty, TypeFoldable};
14 use rustc_span::{Span, DUMMY_SP};
15
16 use std::fmt::Debug;
17
18 pub use rustc_infer::infer::*;
19
20 pub trait InferCtxtExt<'tcx> {
21     fn type_is_copy_modulo_regions(
22         &self,
23         param_env: ty::ParamEnv<'tcx>,
24         ty: Ty<'tcx>,
25         span: Span,
26     ) -> bool;
27
28     fn partially_normalize_associated_types_in<T>(
29         &self,
30         cause: ObligationCause<'tcx>,
31         param_env: ty::ParamEnv<'tcx>,
32         value: T,
33     ) -> InferOk<'tcx, T>
34     where
35         T: TypeFoldable<'tcx>;
36
37     /// Check whether a `ty` implements given trait(trait_def_id).
38     /// The inputs are:
39     ///
40     /// - the def-id of the trait
41     /// - the self type
42     /// - the *other* type parameters of the trait, excluding the self-type
43     /// - the parameter environment
44     ///
45     /// Invokes `evaluate_obligation`, so in the event that evaluating
46     /// `Ty: Trait` causes overflow, EvaluatedToRecur (or EvaluatedToUnknown)
47     /// will be returned.
48     fn type_implements_trait(
49         &self,
50         trait_def_id: DefId,
51         ty: Ty<'tcx>,
52         params: SubstsRef<'tcx>,
53         param_env: ty::ParamEnv<'tcx>,
54     ) -> traits::EvaluationResult;
55 }
56 impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> {
57     fn type_is_copy_modulo_regions(
58         &self,
59         param_env: ty::ParamEnv<'tcx>,
60         ty: Ty<'tcx>,
61         span: Span,
62     ) -> bool {
63         let ty = self.resolve_vars_if_possible(ty);
64
65         if !(param_env, ty).needs_infer() {
66             return ty.is_copy_modulo_regions(self.tcx.at(span), param_env);
67         }
68
69         let copy_def_id = self.tcx.require_lang_item(LangItem::Copy, None);
70
71         // This can get called from typeck (by euv), and `moves_by_default`
72         // rightly refuses to work with inference variables, but
73         // moves_by_default has a cache, which we want to use in other
74         // cases.
75         traits::type_known_to_meet_bound_modulo_regions(self, param_env, ty, copy_def_id, span)
76     }
77
78     /// Normalizes associated types in `value`, potentially returning
79     /// new obligations that must further be processed.
80     fn partially_normalize_associated_types_in<T>(
81         &self,
82         cause: ObligationCause<'tcx>,
83         param_env: ty::ParamEnv<'tcx>,
84         value: T,
85     ) -> InferOk<'tcx, T>
86     where
87         T: TypeFoldable<'tcx>,
88     {
89         debug!("partially_normalize_associated_types_in(value={:?})", value);
90         let mut selcx = traits::SelectionContext::new(self);
91         let traits::Normalized { value, obligations } =
92             traits::normalize(&mut selcx, param_env, cause, value);
93         debug!(
94             "partially_normalize_associated_types_in: result={:?} predicates={:?}",
95             value, obligations
96         );
97         InferOk { value, obligations }
98     }
99
100     fn type_implements_trait(
101         &self,
102         trait_def_id: DefId,
103         ty: Ty<'tcx>,
104         params: SubstsRef<'tcx>,
105         param_env: ty::ParamEnv<'tcx>,
106     ) -> traits::EvaluationResult {
107         debug!(
108             "type_implements_trait: trait_def_id={:?}, type={:?}, params={:?}, param_env={:?}",
109             trait_def_id, ty, params, param_env
110         );
111
112         let trait_ref =
113             ty::TraitRef { def_id: trait_def_id, substs: self.tcx.mk_substs_trait(ty, params) };
114
115         let obligation = traits::Obligation {
116             cause: traits::ObligationCause::dummy(),
117             param_env,
118             recursion_depth: 0,
119             predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(self.tcx),
120         };
121         self.evaluate_obligation(&obligation).unwrap_or(traits::EvaluationResult::EvaluatedToErr)
122     }
123 }
124
125 pub trait InferCtxtBuilderExt<'tcx> {
126     fn enter_canonical_trait_query<K, R>(
127         &mut self,
128         canonical_key: &Canonical<'tcx, K>,
129         operation: impl FnOnce(&InferCtxt<'_, 'tcx>, &mut dyn TraitEngine<'tcx>, K) -> Fallible<R>,
130     ) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
131     where
132         K: TypeFoldable<'tcx>,
133         R: Debug + TypeFoldable<'tcx>,
134         Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>;
135 }
136
137 impl<'tcx> InferCtxtBuilderExt<'tcx> for InferCtxtBuilder<'tcx> {
138     /// The "main method" for a canonicalized trait query. Given the
139     /// canonical key `canonical_key`, this method will create a new
140     /// inference context, instantiate the key, and run your operation
141     /// `op`. The operation should yield up a result (of type `R`) as
142     /// well as a set of trait obligations that must be fully
143     /// satisfied. These obligations will be processed and the
144     /// canonical result created.
145     ///
146     /// Returns `NoSolution` in the event of any error.
147     ///
148     /// (It might be mildly nicer to implement this on `TyCtxt`, and
149     /// not `InferCtxtBuilder`, but that is a bit tricky right now.
150     /// In part because we would need a `for<'tcx>` sort of
151     /// bound for the closure and in part because it is convenient to
152     /// have `'tcx` be free on this function so that we can talk about
153     /// `K: TypeFoldable<'tcx>`.)
154     fn enter_canonical_trait_query<K, R>(
155         &mut self,
156         canonical_key: &Canonical<'tcx, K>,
157         operation: impl FnOnce(&InferCtxt<'_, 'tcx>, &mut dyn TraitEngine<'tcx>, K) -> Fallible<R>,
158     ) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
159     where
160         K: TypeFoldable<'tcx>,
161         R: Debug + TypeFoldable<'tcx>,
162         Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>,
163     {
164         self.enter_with_canonical(
165             DUMMY_SP,
166             canonical_key,
167             |ref infcx, key, canonical_inference_vars| {
168                 let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
169                 let value = operation(infcx, &mut *fulfill_cx, key)?;
170                 infcx.make_canonicalized_query_response(
171                     canonical_inference_vars,
172                     value,
173                     &mut *fulfill_cx,
174                 )
175             },
176         )
177     }
178 }