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 rustc_middle::ty::relate::{Relate, TypeRelation};
31 use rustc_middle::ty::Const;
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>,
39 pub struct Trace<'a, 'tcx> {
42 trace: TypeTrace<'tcx>,
45 impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
49 cause: &'a ObligationCause<'tcx>,
50 param_env: ty::ParamEnv<'tcx>,
52 At { infcx: self, cause, param_env }
56 pub trait ToTrace<'tcx>: Relate<'tcx> + Copy {
59 cause: &ObligationCause<'tcx>,
66 impl<'a, 'tcx> At<'a, 'tcx> {
67 /// Hacky routine for equating two impl headers in coherence.
68 pub fn eq_impl_headers(
70 expected: &ty::ImplHeader<'tcx>,
71 actual: &ty::ImplHeader<'tcx>,
72 ) -> InferResult<'tcx, ()> {
73 debug!("eq_impl_header({:?} = {:?})", expected, actual);
74 match (expected.trait_ref, actual.trait_ref) {
75 (Some(a_ref), Some(b_ref)) => self.eq(a_ref, b_ref),
76 (None, None) => 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, a_is_expected: bool, a: T, b: T) -> InferResult<'tcx, ()>
86 self.trace_exp(a_is_expected, a, b).sub(a, b)
89 /// Makes `actual <: expected`. For example, if type-checking a
90 /// call like `foo(x)`, where `foo: fn(i32)`, you might have
91 /// `sup(i32, x)`, since the "expected" type is the type that
92 /// appears in the signature.
93 pub fn sup<T>(self, expected: T, actual: T) -> InferResult<'tcx, ()>
97 self.sub_exp(false, actual, expected)
100 /// Makes `expected <: actual`.
101 pub fn sub<T>(self, expected: T, actual: T) -> InferResult<'tcx, ()>
105 self.sub_exp(true, expected, actual)
108 /// Makes `expected <: actual`.
109 pub fn eq_exp<T>(self, a_is_expected: bool, a: T, b: T) -> InferResult<'tcx, ()>
113 self.trace_exp(a_is_expected, a, b).eq(a, b)
116 /// Makes `expected <: actual`.
117 pub fn eq<T>(self, expected: T, actual: T) -> InferResult<'tcx, ()>
121 self.trace(expected, actual).eq(expected, actual)
124 pub fn relate<T>(self, expected: T, variance: ty::Variance, actual: T) -> InferResult<'tcx, ()>
129 ty::Variance::Covariant => self.sub(expected, actual),
130 ty::Variance::Invariant => self.eq(expected, actual),
131 ty::Variance::Contravariant => self.sup(expected, actual),
133 // We could make this make sense but it's not readily
134 // exposed and I don't feel like dealing with it. Note
135 // that bivariance in general does a bit more than just
136 // *nothing*, it checks that the types are the same
137 // "modulo variance" basically.
138 ty::Variance::Bivariant => panic!("Bivariant given to `relate()`"),
142 /// Computes the least-upper-bound, or mutual supertype, of two
143 /// values. The order of the arguments doesn't matter, but since
144 /// this can result in an error (e.g., if asked to compute LUB of
145 /// u32 and i32), it is meaningful to call one of them the
147 pub fn lub<T>(self, expected: T, actual: T) -> InferResult<'tcx, T>
151 self.trace(expected, actual).lub(expected, actual)
154 /// Computes the greatest-lower-bound, or mutual subtype, of two
155 /// values. As with `lub` order doesn't matter, except for error
157 pub fn glb<T>(self, expected: T, actual: T) -> InferResult<'tcx, T>
161 self.trace(expected, actual).glb(expected, actual)
164 /// Sets the "trace" values that will be used for
165 /// error-reporting, but doesn't actually perform any operation
166 /// yet (this is useful when you want to set the trace using
167 /// distinct values from those you wish to operate upon).
168 pub fn trace<T>(self, expected: T, actual: T) -> Trace<'a, 'tcx>
172 self.trace_exp(true, expected, actual)
175 /// Like `trace`, but the expected value is determined by the
176 /// boolean argument (if true, then the first argument `a` is the
177 /// "expected" value).
178 pub fn trace_exp<T>(self, a_is_expected: bool, a: T, b: T) -> Trace<'a, 'tcx>
182 let trace = ToTrace::to_trace(self.infcx.tcx, self.cause, a_is_expected, a, b);
183 Trace { at: self, trace, a_is_expected }
187 impl<'a, 'tcx> Trace<'a, 'tcx> {
188 /// Makes `a <: b` where `a` may or may not be expected (if
189 /// `a_is_expected` is true, then `a` is expected).
190 pub fn sub<T>(self, a: T, b: T) -> InferResult<'tcx, ()>
194 debug!("sub({:?} <: {:?})", a, b);
195 let Trace { at, trace, a_is_expected } = self;
196 at.infcx.commit_if_ok(|_| {
197 let mut fields = at.infcx.combine_fields(trace, at.param_env);
201 .map(move |_| InferOk { value: (), obligations: fields.obligations })
205 /// Makes `a == b`; the expectation is set by the call to
207 pub fn eq<T>(self, a: T, b: T) -> InferResult<'tcx, ()>
211 debug!("eq({:?} == {:?})", a, b);
212 let Trace { at, trace, a_is_expected } = self;
213 at.infcx.commit_if_ok(|_| {
214 let mut fields = at.infcx.combine_fields(trace, at.param_env);
216 .equate(a_is_expected)
218 .map(move |_| InferOk { value: (), obligations: fields.obligations })
222 pub fn lub<T>(self, a: T, b: T) -> InferResult<'tcx, T>
226 debug!("lub({:?} \\/ {:?})", a, b);
227 let Trace { at, trace, a_is_expected } = self;
228 at.infcx.commit_if_ok(|_| {
229 let mut fields = at.infcx.combine_fields(trace, at.param_env);
233 .map(move |t| InferOk { value: t, obligations: fields.obligations })
237 pub fn glb<T>(self, a: T, b: T) -> InferResult<'tcx, T>
241 debug!("glb({:?} /\\ {:?})", a, b);
242 let Trace { at, trace, a_is_expected } = self;
243 at.infcx.commit_if_ok(|_| {
244 let mut fields = at.infcx.combine_fields(trace, at.param_env);
248 .map(move |t| InferOk { value: t, obligations: fields.obligations })
253 impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
256 cause: &ObligationCause<'tcx>,
260 ) -> TypeTrace<'tcx> {
261 TypeTrace { cause: cause.clone(), values: Types(ExpectedFound::new(a_is_expected, a, b)) }
265 impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
268 cause: &ObligationCause<'tcx>,
272 ) -> TypeTrace<'tcx> {
273 TypeTrace { cause: cause.clone(), values: Regions(ExpectedFound::new(a_is_expected, a, b)) }
277 impl<'tcx> ToTrace<'tcx> for &'tcx Const<'tcx> {
280 cause: &ObligationCause<'tcx>,
284 ) -> TypeTrace<'tcx> {
285 TypeTrace { cause: cause.clone(), values: Consts(ExpectedFound::new(a_is_expected, a, b)) }
289 impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
292 cause: &ObligationCause<'tcx>,
296 ) -> TypeTrace<'tcx> {
298 cause: cause.clone(),
299 values: TraitRefs(ExpectedFound::new(a_is_expected, a, b)),
304 impl<'tcx> ToTrace<'tcx> for ty::PolyTraitRef<'tcx> {
307 cause: &ObligationCause<'tcx>,
311 ) -> TypeTrace<'tcx> {
313 cause: cause.clone(),
314 values: PolyTraitRefs(ExpectedFound::new(a_is_expected, a, b)),
319 impl<'tcx> ToTrace<'tcx> for ty::ProjectionTy<'tcx> {
322 cause: &ObligationCause<'tcx>,
326 ) -> TypeTrace<'tcx> {
327 let a_ty = tcx.mk_projection(a.item_def_id, a.substs);
328 let b_ty = tcx.mk_projection(b.item_def_id, b.substs);
330 cause: cause.clone(),
331 values: Types(ExpectedFound::new(a_is_expected, a_ty, b_ty)),