]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/infer/at.rs
Auto merge of #93724 - Mark-Simulacrum:drop-query-stats, r=michaelwoerister
[rust.git] / compiler / rustc_infer / src / infer / at.rs
1 //! A nice interface for working with the infcx. The basic idea is to
2 //! do `infcx.at(cause, param_env)`, which sets the "cause" of the
3 //! operation as well as the surrounding parameter environment. Then
4 //! you can do something like `.sub(a, b)` or `.eq(a, b)` to create a
5 //! subtype or equality relationship respectively. The first argument
6 //! is always the "expected" output from the POV of diagnostics.
7 //!
8 //! Examples:
9 //!
10 //!     infcx.at(cause, param_env).sub(a, b)
11 //!     // requires that `a <: b`, with `a` considered the "expected" type
12 //!
13 //!     infcx.at(cause, param_env).sup(a, b)
14 //!     // requires that `b <: a`, with `a` considered the "expected" type
15 //!
16 //!     infcx.at(cause, param_env).eq(a, b)
17 //!     // requires that `a == b`, with `a` considered the "expected" type
18 //!
19 //! For finer-grained control, you can also do use `trace`:
20 //!
21 //!     infcx.at(...).trace(a, b).sub(&c, &d)
22 //!
23 //! This will set `a` and `b` as the "root" values for
24 //! error-reporting, but actually operate on `c` and `d`. This is
25 //! sometimes useful when the types of `c` and `d` are not traceable
26 //! things. (That system should probably be refactored.)
27
28 use super::*;
29
30 use rustc_middle::ty::relate::{Relate, TypeRelation};
31 use rustc_middle::ty::Const;
32
33 pub struct At<'a, 'tcx> {
34     pub infcx: &'a InferCtxt<'a, 'tcx>,
35     pub cause: &'a ObligationCause<'tcx>,
36     pub param_env: ty::ParamEnv<'tcx>,
37     /// Whether we should define opaque types
38     /// or just treat them opaquely.
39     /// Currently only used to prevent predicate
40     /// matching from matching anything against opaque
41     /// types.
42     pub define_opaque_types: bool,
43 }
44
45 pub struct Trace<'a, 'tcx> {
46     at: At<'a, 'tcx>,
47     a_is_expected: bool,
48     trace: TypeTrace<'tcx>,
49 }
50
51 impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
52     #[inline]
53     pub fn at(
54         &'a self,
55         cause: &'a ObligationCause<'tcx>,
56         param_env: ty::ParamEnv<'tcx>,
57     ) -> At<'a, 'tcx> {
58         At { infcx: self, cause, param_env, define_opaque_types: true }
59     }
60 }
61
62 pub trait ToTrace<'tcx>: Relate<'tcx> + Copy {
63     fn to_trace(
64         tcx: TyCtxt<'tcx>,
65         cause: &ObligationCause<'tcx>,
66         a_is_expected: bool,
67         a: Self,
68         b: Self,
69     ) -> TypeTrace<'tcx>;
70 }
71
72 impl<'a, 'tcx> At<'a, 'tcx> {
73     pub fn define_opaque_types(self, define_opaque_types: bool) -> Self {
74         Self { define_opaque_types, ..self }
75     }
76
77     /// Hacky routine for equating two impl headers in coherence.
78     pub fn eq_impl_headers(
79         self,
80         expected: &ty::ImplHeader<'tcx>,
81         actual: &ty::ImplHeader<'tcx>,
82     ) -> InferResult<'tcx, ()> {
83         debug!("eq_impl_header({:?} = {:?})", expected, actual);
84         match (expected.trait_ref, actual.trait_ref) {
85             (Some(a_ref), Some(b_ref)) => self.eq(a_ref, b_ref),
86             (None, None) => self.eq(expected.self_ty, actual.self_ty),
87             _ => bug!("mk_eq_impl_headers given mismatched impl kinds"),
88         }
89     }
90
91     /// Makes `a <: b`, where `a` may or may not be expected.
92     pub fn sub_exp<T>(self, a_is_expected: bool, a: T, b: T) -> InferResult<'tcx, ()>
93     where
94         T: ToTrace<'tcx>,
95     {
96         self.trace_exp(a_is_expected, a, b).sub(a, b)
97     }
98
99     /// Makes `actual <: expected`. For example, if type-checking a
100     /// call like `foo(x)`, where `foo: fn(i32)`, you might have
101     /// `sup(i32, x)`, since the "expected" type is the type that
102     /// appears in the signature.
103     pub fn sup<T>(self, expected: T, actual: T) -> InferResult<'tcx, ()>
104     where
105         T: ToTrace<'tcx>,
106     {
107         self.sub_exp(false, actual, expected)
108     }
109
110     /// Makes `expected <: actual`.
111     pub fn sub<T>(self, expected: T, actual: T) -> InferResult<'tcx, ()>
112     where
113         T: ToTrace<'tcx>,
114     {
115         self.sub_exp(true, expected, actual)
116     }
117
118     /// Makes `expected <: actual`.
119     pub fn eq_exp<T>(self, a_is_expected: bool, a: T, b: T) -> InferResult<'tcx, ()>
120     where
121         T: ToTrace<'tcx>,
122     {
123         self.trace_exp(a_is_expected, a, b).eq(a, b)
124     }
125
126     /// Makes `expected <: actual`.
127     pub fn eq<T>(self, expected: T, actual: T) -> InferResult<'tcx, ()>
128     where
129         T: ToTrace<'tcx>,
130     {
131         self.trace(expected, actual).eq(expected, actual)
132     }
133
134     pub fn relate<T>(self, expected: T, variance: ty::Variance, actual: T) -> InferResult<'tcx, ()>
135     where
136         T: ToTrace<'tcx>,
137     {
138         match variance {
139             ty::Variance::Covariant => self.sub(expected, actual),
140             ty::Variance::Invariant => self.eq(expected, actual),
141             ty::Variance::Contravariant => self.sup(expected, actual),
142
143             // We could make this make sense but it's not readily
144             // exposed and I don't feel like dealing with it. Note
145             // that bivariance in general does a bit more than just
146             // *nothing*, it checks that the types are the same
147             // "modulo variance" basically.
148             ty::Variance::Bivariant => panic!("Bivariant given to `relate()`"),
149         }
150     }
151
152     /// Computes the least-upper-bound, or mutual supertype, of two
153     /// values. The order of the arguments doesn't matter, but since
154     /// this can result in an error (e.g., if asked to compute LUB of
155     /// u32 and i32), it is meaningful to call one of them the
156     /// "expected type".
157     pub fn lub<T>(self, expected: T, actual: T) -> InferResult<'tcx, T>
158     where
159         T: ToTrace<'tcx>,
160     {
161         self.trace(expected, actual).lub(expected, actual)
162     }
163
164     /// Computes the greatest-lower-bound, or mutual subtype, of two
165     /// values. As with `lub` order doesn't matter, except for error
166     /// cases.
167     pub fn glb<T>(self, expected: T, actual: T) -> InferResult<'tcx, T>
168     where
169         T: ToTrace<'tcx>,
170     {
171         self.trace(expected, actual).glb(expected, actual)
172     }
173
174     /// Sets the "trace" values that will be used for
175     /// error-reporting, but doesn't actually perform any operation
176     /// yet (this is useful when you want to set the trace using
177     /// distinct values from those you wish to operate upon).
178     pub fn trace<T>(self, expected: T, actual: T) -> Trace<'a, 'tcx>
179     where
180         T: ToTrace<'tcx>,
181     {
182         self.trace_exp(true, expected, actual)
183     }
184
185     /// Like `trace`, but the expected value is determined by the
186     /// boolean argument (if true, then the first argument `a` is the
187     /// "expected" value).
188     pub fn trace_exp<T>(self, a_is_expected: bool, a: T, b: T) -> Trace<'a, 'tcx>
189     where
190         T: ToTrace<'tcx>,
191     {
192         let trace = ToTrace::to_trace(self.infcx.tcx, self.cause, a_is_expected, a, b);
193         Trace { at: self, trace, a_is_expected }
194     }
195 }
196
197 impl<'a, 'tcx> Trace<'a, 'tcx> {
198     /// Makes `a <: b` where `a` may or may not be expected (if
199     /// `a_is_expected` is true, then `a` is expected).
200     #[instrument(skip(self), level = "debug")]
201     pub fn sub<T>(self, a: T, b: T) -> InferResult<'tcx, ()>
202     where
203         T: Relate<'tcx>,
204     {
205         let Trace { at, trace, a_is_expected } = self;
206         at.infcx.commit_if_ok(|_| {
207             let mut fields = at.infcx.combine_fields(trace, at.param_env, at.define_opaque_types);
208             fields
209                 .sub(a_is_expected)
210                 .relate(a, b)
211                 .map(move |_| InferOk { value: (), obligations: fields.obligations })
212         })
213     }
214
215     /// Makes `a == b`; the expectation is set by the call to
216     /// `trace()`.
217     #[instrument(skip(self), level = "debug")]
218     pub fn eq<T>(self, a: T, b: T) -> InferResult<'tcx, ()>
219     where
220         T: Relate<'tcx>,
221     {
222         let Trace { at, trace, a_is_expected } = self;
223         at.infcx.commit_if_ok(|_| {
224             let mut fields = at.infcx.combine_fields(trace, at.param_env, at.define_opaque_types);
225             fields
226                 .equate(a_is_expected)
227                 .relate(a, b)
228                 .map(move |_| InferOk { value: (), obligations: fields.obligations })
229         })
230     }
231
232     #[instrument(skip(self), level = "debug")]
233     pub fn lub<T>(self, a: T, b: T) -> InferResult<'tcx, T>
234     where
235         T: Relate<'tcx>,
236     {
237         let Trace { at, trace, a_is_expected } = self;
238         at.infcx.commit_if_ok(|_| {
239             let mut fields = at.infcx.combine_fields(trace, at.param_env, at.define_opaque_types);
240             fields
241                 .lub(a_is_expected)
242                 .relate(a, b)
243                 .map(move |t| InferOk { value: t, obligations: fields.obligations })
244         })
245     }
246
247     #[instrument(skip(self), level = "debug")]
248     pub fn glb<T>(self, a: T, b: T) -> InferResult<'tcx, T>
249     where
250         T: Relate<'tcx>,
251     {
252         let Trace { at, trace, a_is_expected } = self;
253         at.infcx.commit_if_ok(|_| {
254             let mut fields = at.infcx.combine_fields(trace, at.param_env, at.define_opaque_types);
255             fields
256                 .glb(a_is_expected)
257                 .relate(a, b)
258                 .map(move |t| InferOk { value: t, obligations: fields.obligations })
259         })
260     }
261 }
262
263 impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
264     fn to_trace(
265         _: TyCtxt<'tcx>,
266         cause: &ObligationCause<'tcx>,
267         a_is_expected: bool,
268         a: Self,
269         b: Self,
270     ) -> TypeTrace<'tcx> {
271         TypeTrace {
272             cause: cause.clone(),
273             values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
274         }
275     }
276 }
277
278 impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
279     fn to_trace(
280         _: TyCtxt<'tcx>,
281         cause: &ObligationCause<'tcx>,
282         a_is_expected: bool,
283         a: Self,
284         b: Self,
285     ) -> TypeTrace<'tcx> {
286         TypeTrace { cause: cause.clone(), values: Regions(ExpectedFound::new(a_is_expected, a, b)) }
287     }
288 }
289
290 impl<'tcx> ToTrace<'tcx> for &'tcx Const<'tcx> {
291     fn to_trace(
292         _: TyCtxt<'tcx>,
293         cause: &ObligationCause<'tcx>,
294         a_is_expected: bool,
295         a: Self,
296         b: Self,
297     ) -> TypeTrace<'tcx> {
298         TypeTrace {
299             cause: cause.clone(),
300             values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
301         }
302     }
303 }
304
305 impl<'tcx> ToTrace<'tcx> for ty::Term<'tcx> {
306     fn to_trace(
307         _: TyCtxt<'tcx>,
308         cause: &ObligationCause<'tcx>,
309         a_is_expected: bool,
310         a: Self,
311         b: Self,
312     ) -> TypeTrace<'tcx> {
313         TypeTrace { cause: cause.clone(), values: Terms(ExpectedFound::new(a_is_expected, a, b)) }
314     }
315 }
316
317 impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
318     fn to_trace(
319         _: TyCtxt<'tcx>,
320         cause: &ObligationCause<'tcx>,
321         a_is_expected: bool,
322         a: Self,
323         b: Self,
324     ) -> TypeTrace<'tcx> {
325         TypeTrace {
326             cause: cause.clone(),
327             values: TraitRefs(ExpectedFound::new(a_is_expected, a, b)),
328         }
329     }
330 }
331
332 impl<'tcx> ToTrace<'tcx> for ty::PolyTraitRef<'tcx> {
333     fn to_trace(
334         _: TyCtxt<'tcx>,
335         cause: &ObligationCause<'tcx>,
336         a_is_expected: bool,
337         a: Self,
338         b: Self,
339     ) -> TypeTrace<'tcx> {
340         TypeTrace {
341             cause: cause.clone(),
342             values: PolyTraitRefs(ExpectedFound::new(a_is_expected, a, b)),
343         }
344     }
345 }
346
347 impl<'tcx> ToTrace<'tcx> for ty::ProjectionTy<'tcx> {
348     fn to_trace(
349         tcx: TyCtxt<'tcx>,
350         cause: &ObligationCause<'tcx>,
351         a_is_expected: bool,
352         a: Self,
353         b: Self,
354     ) -> TypeTrace<'tcx> {
355         let a_ty = tcx.mk_projection(a.item_def_id, a.substs);
356         let b_ty = tcx.mk_projection(b.item_def_id, b.substs);
357         TypeTrace {
358             cause: cause.clone(),
359             values: Terms(ExpectedFound::new(a_is_expected, a_ty.into(), b_ty.into())),
360         }
361     }
362 }