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 }
55 /// Forks the inference context, creating a new inference context with the same inference
56 /// variables in the same state. This can be used to "branch off" many tests from the same
57 /// common state. Used in coherence.
58 pub fn fork(&self) -> Self {
60 tcx
: self.tcx
.clone(),
61 defining_use_anchor
: self.defining_use_anchor
.clone(),
62 in_progress_typeck_results
: self.in_progress_typeck_results
.clone(),
63 inner
: self.inner
.clone(),
64 skip_leak_check
: self.skip_leak_check
.clone(),
65 lexical_region_resolutions
: self.lexical_region_resolutions
.clone(),
66 selection_cache
: self.selection_cache
.clone(),
67 evaluation_cache
: self.evaluation_cache
.clone(),
68 reported_trait_errors
: self.reported_trait_errors
.clone(),
69 reported_closure_mismatch
: self.reported_closure_mismatch
.clone(),
70 tainted_by_errors_flag
: self.tainted_by_errors_flag
.clone(),
71 err_count_on_creation
: self.err_count_on_creation
,
72 in_snapshot
: self.in_snapshot
.clone(),
73 universe
: self.universe
.clone(),
78 pub trait ToTrace
<'tcx
>: Relate
<'tcx
> + Copy
{
81 cause
: &ObligationCause
<'tcx
>,
88 impl<'a
, 'tcx
> At
<'a
, 'tcx
> {
89 /// Hacky routine for equating two impl headers in coherence.
90 pub fn eq_impl_headers(
92 expected
: &ty
::ImplHeader
<'tcx
>,
93 actual
: &ty
::ImplHeader
<'tcx
>,
94 ) -> InferResult
<'tcx
, ()> {
95 debug
!("eq_impl_header({:?} = {:?})", expected
, actual
);
96 match (expected
.trait_ref
, actual
.trait_ref
) {
97 (Some(a_ref
), Some(b_ref
)) => self.eq(a_ref
, b_ref
),
98 (None
, None
) => self.eq(expected
.self_ty
, actual
.self_ty
),
99 _
=> bug
!("mk_eq_impl_headers given mismatched impl kinds"),
103 /// Makes `a <: b`, where `a` may or may not be expected.
104 pub fn sub_exp
<T
>(self, a_is_expected
: bool
, a
: T
, b
: T
) -> InferResult
<'tcx
, ()>
108 self.trace_exp(a_is_expected
, a
, b
).sub(a
, b
)
111 /// Makes `actual <: expected`. For example, if type-checking a
112 /// call like `foo(x)`, where `foo: fn(i32)`, you might have
113 /// `sup(i32, x)`, since the "expected" type is the type that
114 /// appears in the signature.
115 pub fn sup
<T
>(self, expected
: T
, actual
: T
) -> InferResult
<'tcx
, ()>
119 self.sub_exp(false, actual
, expected
)
122 /// Makes `expected <: actual`.
123 pub fn sub
<T
>(self, expected
: T
, actual
: T
) -> InferResult
<'tcx
, ()>
127 self.sub_exp(true, expected
, actual
)
130 /// Makes `expected <: actual`.
131 pub fn eq_exp
<T
>(self, a_is_expected
: bool
, a
: T
, b
: T
) -> InferResult
<'tcx
, ()>
135 self.trace_exp(a_is_expected
, a
, b
).eq(a
, b
)
138 /// Makes `expected <: actual`.
139 pub fn eq
<T
>(self, expected
: T
, actual
: T
) -> InferResult
<'tcx
, ()>
143 self.trace(expected
, actual
).eq(expected
, actual
)
146 pub fn relate
<T
>(self, expected
: T
, variance
: ty
::Variance
, actual
: T
) -> InferResult
<'tcx
, ()>
151 ty
::Variance
::Covariant
=> self.sub(expected
, actual
),
152 ty
::Variance
::Invariant
=> self.eq(expected
, actual
),
153 ty
::Variance
::Contravariant
=> self.sup(expected
, actual
),
155 // We could make this make sense but it's not readily
156 // exposed and I don't feel like dealing with it. Note
157 // that bivariance in general does a bit more than just
158 // *nothing*, it checks that the types are the same
159 // "modulo variance" basically.
160 ty
::Variance
::Bivariant
=> panic
!("Bivariant given to `relate()`"),
164 /// Computes the least-upper-bound, or mutual supertype, of two
165 /// values. The order of the arguments doesn't matter, but since
166 /// this can result in an error (e.g., if asked to compute LUB of
167 /// u32 and i32), it is meaningful to call one of them the
169 pub fn lub
<T
>(self, expected
: T
, actual
: T
) -> InferResult
<'tcx
, T
>
173 self.trace(expected
, actual
).lub(expected
, actual
)
176 /// Computes the greatest-lower-bound, or mutual subtype, of two
177 /// values. As with `lub` order doesn't matter, except for error
179 pub fn glb
<T
>(self, expected
: T
, actual
: T
) -> InferResult
<'tcx
, T
>
183 self.trace(expected
, actual
).glb(expected
, actual
)
186 /// Sets the "trace" values that will be used for
187 /// error-reporting, but doesn't actually perform any operation
188 /// yet (this is useful when you want to set the trace using
189 /// distinct values from those you wish to operate upon).
190 pub fn trace
<T
>(self, expected
: T
, actual
: T
) -> Trace
<'a
, '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, a_is_expected
: bool
, a
: T
, b
: T
) -> Trace
<'a
, 'tcx
>
204 let trace
= ToTrace
::to_trace(self.infcx
.tcx
, self.cause
, a_is_expected
, a
, b
);
205 Trace { at: self, trace, a_is_expected }
209 impl<'a
, 'tcx
> Trace
<'a
, 'tcx
> {
210 /// Makes `a <: b` where `a` may or may not be expected (if
211 /// `a_is_expected` is true, then `a` is expected).
212 #[instrument(skip(self), level = "debug")]
213 pub fn sub
<T
>(self, a
: T
, b
: T
) -> InferResult
<'tcx
, ()>
217 let Trace { at, trace, a_is_expected }
= self;
218 at
.infcx
.commit_if_ok(|_
| {
219 let mut fields
= at
.infcx
.combine_fields(trace
, at
.param_env
);
223 .map(move |_
| InferOk { value: (), obligations: fields.obligations }
)
227 /// Makes `a == b`; the expectation is set by the call to
229 #[instrument(skip(self), level = "debug")]
230 pub fn eq
<T
>(self, a
: T
, b
: T
) -> InferResult
<'tcx
, ()>
234 let Trace { at, trace, a_is_expected }
= self;
235 at
.infcx
.commit_if_ok(|_
| {
236 let mut fields
= at
.infcx
.combine_fields(trace
, at
.param_env
);
238 .equate(a_is_expected
)
240 .map(move |_
| InferOk { value: (), obligations: fields.obligations }
)
244 #[instrument(skip(self), level = "debug")]
245 pub fn lub
<T
>(self, a
: T
, b
: T
) -> InferResult
<'tcx
, T
>
249 let Trace { at, trace, a_is_expected }
= self;
250 at
.infcx
.commit_if_ok(|_
| {
251 let mut fields
= at
.infcx
.combine_fields(trace
, at
.param_env
);
255 .map(move |t
| InferOk { value: t, obligations: fields.obligations }
)
259 #[instrument(skip(self), level = "debug")]
260 pub fn glb
<T
>(self, a
: T
, b
: T
) -> InferResult
<'tcx
, T
>
264 let Trace { at, trace, a_is_expected }
= self;
265 at
.infcx
.commit_if_ok(|_
| {
266 let mut fields
= at
.infcx
.combine_fields(trace
, at
.param_env
);
270 .map(move |t
| InferOk { value: t, obligations: fields.obligations }
)
275 impl<'tcx
> ToTrace
<'tcx
> for Ty
<'tcx
> {
278 cause
: &ObligationCause
<'tcx
>,
282 ) -> TypeTrace
<'tcx
> {
284 cause
: cause
.clone(),
285 values
: Terms(ExpectedFound
::new(a_is_expected
, a
.into(), b
.into())),
290 impl<'tcx
> ToTrace
<'tcx
> for ty
::Region
<'tcx
> {
293 cause
: &ObligationCause
<'tcx
>,
297 ) -> TypeTrace
<'tcx
> {
298 TypeTrace { cause: cause.clone(), values: Regions(ExpectedFound::new(a_is_expected, a, b)) }
302 impl<'tcx
> ToTrace
<'tcx
> for Const
<'tcx
> {
305 cause
: &ObligationCause
<'tcx
>,
309 ) -> TypeTrace
<'tcx
> {
311 cause
: cause
.clone(),
312 values
: Terms(ExpectedFound
::new(a_is_expected
, a
.into(), b
.into())),
317 impl<'tcx
> ToTrace
<'tcx
> for ty
::Term
<'tcx
> {
320 cause
: &ObligationCause
<'tcx
>,
324 ) -> TypeTrace
<'tcx
> {
325 TypeTrace { cause: cause.clone(), values: Terms(ExpectedFound::new(a_is_expected, a, b)) }
329 impl<'tcx
> ToTrace
<'tcx
> for ty
::TraitRef
<'tcx
> {
332 cause
: &ObligationCause
<'tcx
>,
336 ) -> TypeTrace
<'tcx
> {
338 cause
: cause
.clone(),
339 values
: TraitRefs(ExpectedFound
::new(a_is_expected
, a
, b
)),
344 impl<'tcx
> ToTrace
<'tcx
> for ty
::PolyTraitRef
<'tcx
> {
347 cause
: &ObligationCause
<'tcx
>,
351 ) -> TypeTrace
<'tcx
> {
353 cause
: cause
.clone(),
354 values
: PolyTraitRefs(ExpectedFound
::new(a_is_expected
, a
, b
)),
359 impl<'tcx
> ToTrace
<'tcx
> for ty
::ProjectionTy
<'tcx
> {
362 cause
: &ObligationCause
<'tcx
>,
366 ) -> TypeTrace
<'tcx
> {
367 let a_ty
= tcx
.mk_projection(a
.item_def_id
, a
.substs
);
368 let b_ty
= tcx
.mk_projection(b
.item_def_id
, b
.substs
);
370 cause
: cause
.clone(),
371 values
: Terms(ExpectedFound
::new(a_is_expected
, a_ty
.into(), b_ty
.into())),