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