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.
10 //! infcx.at(cause, param_env).sub(a, b)
11 //! // requires that `a <: b`, with `a` considered the "expected" type
13 //! infcx.at(cause, param_env).sup(a, b)
14 //! // requires that `b <: a`, with `a` considered the "expected" type
16 //! infcx.at(cause, param_env).eq(a, b)
17 //! // requires that `a == b`, with `a` considered the "expected" type
19 //! For finer-grained control, you can also do use `trace`:
21 //! infcx.at(...).trace(a, b).sub(&c, &d)
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.)
30 use crate::ty::relate::{Relate, TypeRelation};
32 pub struct At<'a, 'gcx: 'tcx, 'tcx: 'a> {
33 pub infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
34 pub cause: &'a ObligationCause<'tcx>,
35 pub param_env: ty::ParamEnv<'tcx>,
38 pub struct Trace<'a, 'gcx: 'tcx, 'tcx: 'a> {
39 at: At<'a, 'gcx, 'tcx>,
41 trace: TypeTrace<'tcx>,
44 impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
47 cause: &'a ObligationCause<'tcx>,
48 param_env: ty::ParamEnv<'tcx>)
51 At { infcx: self, cause, param_env }
55 pub trait ToTrace<'tcx>: Relate<'tcx> + Copy {
56 fn to_trace(cause: &ObligationCause<'tcx>,
63 impl<'a, 'gcx, 'tcx> At<'a, 'gcx, 'tcx> {
64 /// Hacky routine for equating two impl headers in coherence.
65 pub fn eq_impl_headers(self,
66 expected: &ty::ImplHeader<'tcx>,
67 actual: &ty::ImplHeader<'tcx>)
68 -> InferResult<'tcx, ()>
70 debug!("eq_impl_header({:?} = {:?})", expected, actual);
71 match (expected.trait_ref, actual.trait_ref) {
72 (Some(a_ref), Some(b_ref)) =>
73 self.eq(a_ref, b_ref),
75 self.eq(expected.self_ty, actual.self_ty),
77 bug!("mk_eq_impl_headers given mismatched impl kinds"),
81 /// Makes `a <: b`, where `a` may or may not be expected.
82 pub fn sub_exp<T>(self,
86 -> InferResult<'tcx, ()>
87 where T: ToTrace<'tcx>
89 self.trace_exp(a_is_expected, a, b).sub(&a, &b)
92 /// Makes `actual <: expected`. For example, if type-checking a
93 /// call like `foo(x)`, where `foo: fn(i32)`, you might have
94 /// `sup(i32, x)`, since the "expected" type is the type that
95 /// appears in the signature.
99 -> InferResult<'tcx, ()>
100 where T: ToTrace<'tcx>
102 self.sub_exp(false, actual, expected)
105 /// Makes `expected <: actual`.
109 -> InferResult<'tcx, ()>
110 where T: ToTrace<'tcx>
112 self.sub_exp(true, expected, actual)
115 /// Makes `expected <: actual`.
116 pub fn eq_exp<T>(self,
120 -> InferResult<'tcx, ()>
121 where T: ToTrace<'tcx>
123 self.trace_exp(a_is_expected, a, b).eq(&a, &b)
126 /// Makes `expected <: actual`.
130 -> InferResult<'tcx, ()>
131 where T: ToTrace<'tcx>
133 self.trace(expected, actual).eq(&expected, &actual)
139 variance: ty::Variance,
141 ) -> InferResult<'tcx, ()>
142 where T: ToTrace<'tcx>
145 ty::Variance::Covariant => self.sub(expected, actual),
146 ty::Variance::Invariant => self.eq(expected, actual),
147 ty::Variance::Contravariant => self.sup(expected, actual),
149 // We could make this make sense but it's not readily
150 // exposed and I don't feel like dealing with it. Note
151 // that bivariance in general does a bit more than just
152 // *nothing*, it checks that the types are the same
153 // "modulo variance" basically.
154 ty::Variance::Bivariant => panic!("Bivariant given to `relate()`"),
158 /// Computes the least-upper-bound, or mutual supertype, of two
159 /// values. The order of the arguments doesn't matter, but since
160 /// this can result in an error (e.g., if asked to compute LUB of
161 /// u32 and i32), it is meaningful to call one of them the
166 -> InferResult<'tcx, T>
167 where T: ToTrace<'tcx>
169 self.trace(expected, actual).lub(&expected, &actual)
172 /// Computes the greatest-lower-bound, or mutual subtype, of two
173 /// values. As with `lub` order doesn't matter, except for error
178 -> InferResult<'tcx, T>
179 where T: ToTrace<'tcx>
181 self.trace(expected, actual).glb(&expected, &actual)
184 /// Sets the "trace" values that will be used for
185 /// error-reporting, but doesn't actually perform any operation
186 /// yet (this is useful when you want to set the trace using
187 /// distinct values from those you wish to operate upon).
188 pub fn trace<T>(self,
191 -> Trace<'a, 'gcx, 'tcx>
192 where T: ToTrace<'tcx>
194 self.trace_exp(true, expected, actual)
197 /// Like `trace`, but the expected value is determined by the
198 /// boolean argument (if true, then the first argument `a` is the
199 /// "expected" value).
200 pub fn trace_exp<T>(self,
204 -> Trace<'a, 'gcx, 'tcx>
205 where T: ToTrace<'tcx>
207 let trace = ToTrace::to_trace(self.cause, a_is_expected, a, b);
208 Trace { at: self, trace: trace, a_is_expected }
212 impl<'a, 'gcx, 'tcx> Trace<'a, 'gcx, 'tcx> {
213 /// Makes `a <: b` where `a` may or may not be expected (if
214 /// `a_is_expected` is true, then `a` is expected).
215 /// Makes `expected <: actual`.
219 -> InferResult<'tcx, ()>
220 where T: Relate<'tcx>
222 debug!("sub({:?} <: {:?})", a, b);
223 let Trace { at, trace, a_is_expected } = self;
224 at.infcx.commit_if_ok(|_| {
225 let mut fields = at.infcx.combine_fields(trace, at.param_env);
226 fields.sub(a_is_expected)
228 .map(move |_| InferOk { value: (), obligations: fields.obligations })
232 /// Makes `a == b`; the expectation is set by the call to
237 -> InferResult<'tcx, ()>
238 where T: Relate<'tcx>
240 debug!("eq({:?} == {:?})", a, b);
241 let Trace { at, trace, a_is_expected } = self;
242 at.infcx.commit_if_ok(|_| {
243 let mut fields = at.infcx.combine_fields(trace, at.param_env);
244 fields.equate(a_is_expected)
246 .map(move |_| InferOk { value: (), obligations: fields.obligations })
253 -> InferResult<'tcx, T>
254 where T: Relate<'tcx>
256 debug!("lub({:?} \\/ {:?})", a, b);
257 let Trace { at, trace, a_is_expected } = self;
258 at.infcx.commit_if_ok(|_| {
259 let mut fields = at.infcx.combine_fields(trace, at.param_env);
260 fields.lub(a_is_expected)
262 .map(move |t| InferOk { value: t, obligations: fields.obligations })
269 -> InferResult<'tcx, T>
270 where T: Relate<'tcx>
272 debug!("glb({:?} /\\ {:?})", a, b);
273 let Trace { at, trace, a_is_expected } = self;
274 at.infcx.commit_if_ok(|_| {
275 let mut fields = at.infcx.combine_fields(trace, at.param_env);
276 fields.glb(a_is_expected)
278 .map(move |t| InferOk { value: t, obligations: fields.obligations })
283 impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
284 fn to_trace(cause: &ObligationCause<'tcx>,
291 cause: cause.clone(),
292 values: Types(ExpectedFound::new(a_is_expected, a, b))
297 impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
298 fn to_trace(cause: &ObligationCause<'tcx>,
305 cause: cause.clone(),
306 values: Regions(ExpectedFound::new(a_is_expected, a, b))
311 impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
312 fn to_trace(cause: &ObligationCause<'tcx>,
319 cause: cause.clone(),
320 values: TraitRefs(ExpectedFound::new(a_is_expected, a, b))
325 impl<'tcx> ToTrace<'tcx> for ty::PolyTraitRef<'tcx> {
326 fn to_trace(cause: &ObligationCause<'tcx>,
333 cause: cause.clone(),
334 values: PolyTraitRefs(ExpectedFound::new(a_is_expected, a, b))