Skip to content

Commit 5a6bffb

Browse files
Copy over some methods from At to InferCtxt
1 parent b542750 commit 5a6bffb

1 file changed

Lines changed: 139 additions & 1 deletion

File tree

  • compiler/rustc_infer/src/infer

‎compiler/rustc_infer/src/infer/at.rs‎

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
2828
use relate::lattice::{LatticeOp, LatticeOpKind};
2929
use rustc_middle::ty::relate::solver_relating::RelateExt as NextSolverRelate;
30-
use rustc_middle::ty::{Const, TypingMode};
30+
use rustc_middle::ty::{Const, ParamEnv, TypingMode};
3131
use rustc_span::bug;
3232

3333
use super::*;
@@ -124,6 +124,144 @@ impl<'tcx> InferCtxt<'tcx> {
124124
forked.inner.borrow_mut().projection_cache().clear();
125125
forked
126126
}
127+
128+
/// Makes `actual <: expected`. For example, if type-checking a
129+
/// call like `foo(x)`, where `foo: fn(i32)`, you might have
130+
/// `sup(i32, x)`, since the "expected" type is the type that
131+
/// appears in the signature.
132+
pub fn sup<T>(
133+
self,
134+
cause: &ObligationCause<'tcx>,
135+
param_env: ParamEnv<'tcx>,
136+
define_opaque_types: DefineOpaqueTypes,
137+
expected: T,
138+
actual: T,
139+
) -> InferResult<'tcx, ()>
140+
where
141+
T: ToTrace<'tcx>,
142+
{
143+
if self.next_trait_solver {
144+
NextSolverRelate::relate(
145+
&self,
146+
param_env,
147+
expected,
148+
ty::Contravariant,
149+
actual,
150+
cause.span,
151+
)
152+
.map(|goals| self.at(&cause, param_env).goals_to_obligations(goals))
153+
} else {
154+
let mut op = TypeRelating::new(
155+
&self,
156+
ToTrace::to_trace(&cause, expected, actual),
157+
param_env,
158+
define_opaque_types,
159+
ty::Contravariant,
160+
);
161+
op.relate(expected, actual)?;
162+
Ok(InferOk { value: (), obligations: op.into_obligations() })
163+
}
164+
}
165+
166+
/// Makes `expected <: actual`.
167+
pub fn sub<T>(
168+
self,
169+
cause: &ObligationCause<'tcx>,
170+
param_env: ParamEnv<'tcx>,
171+
define_opaque_types: DefineOpaqueTypes,
172+
expected: T,
173+
actual: T,
174+
) -> InferResult<'tcx, ()>
175+
where
176+
T: ToTrace<'tcx>,
177+
{
178+
if self.next_trait_solver {
179+
NextSolverRelate::relate(&self, param_env, expected, ty::Covariant, actual, cause.span)
180+
.map(|goals| self.at(&cause, param_env).goals_to_obligations(goals))
181+
} else {
182+
let mut op = TypeRelating::new(
183+
&self,
184+
ToTrace::to_trace(&cause, expected, actual),
185+
param_env,
186+
define_opaque_types,
187+
ty::Covariant,
188+
);
189+
op.relate(expected, actual)?;
190+
Ok(InferOk { value: (), obligations: op.into_obligations() })
191+
}
192+
}
193+
194+
/// Makes `expected == actual`.
195+
pub fn eq<T>(
196+
self,
197+
cause: &ObligationCause<'tcx>,
198+
param_env: ParamEnv<'tcx>,
199+
define_opaque_types: DefineOpaqueTypes,
200+
expected: T,
201+
actual: T,
202+
) -> InferResult<'tcx, ()>
203+
where
204+
T: ToTrace<'tcx>,
205+
{
206+
self.eq_trace(
207+
cause,
208+
param_env,
209+
define_opaque_types,
210+
ToTrace::to_trace(cause, expected, actual),
211+
expected,
212+
actual,
213+
)
214+
}
215+
216+
/// Makes `expected == actual`.
217+
pub fn eq_trace<T>(
218+
self,
219+
cause: &ObligationCause<'tcx>,
220+
param_env: ParamEnv<'tcx>,
221+
define_opaque_types: DefineOpaqueTypes,
222+
trace: TypeTrace<'tcx>,
223+
expected: T,
224+
actual: T,
225+
) -> InferResult<'tcx, ()>
226+
where
227+
T: Relate<TyCtxt<'tcx>>,
228+
{
229+
if self.next_trait_solver {
230+
NextSolverRelate::relate(&self, param_env, expected, ty::Invariant, actual, cause.span)
231+
.map(|goals| self.at(&cause, param_env).goals_to_obligations(goals))
232+
} else {
233+
let mut op =
234+
TypeRelating::new(&self, trace, param_env, define_opaque_types, ty::Invariant);
235+
op.relate(expected, actual)?;
236+
Ok(InferOk { value: (), obligations: op.into_obligations() })
237+
}
238+
}
239+
240+
pub fn relate<T>(
241+
self,
242+
cause: &ObligationCause<'tcx>,
243+
param_env: ParamEnv<'tcx>,
244+
define_opaque_types: DefineOpaqueTypes,
245+
expected: T,
246+
variance: ty::Variance,
247+
actual: T,
248+
) -> InferResult<'tcx, ()>
249+
where
250+
T: ToTrace<'tcx>,
251+
{
252+
match variance {
253+
ty::Covariant => self.sub(cause, param_env, define_opaque_types, expected, actual),
254+
ty::Invariant => self.eq(cause, param_env, define_opaque_types, expected, actual),
255+
ty::Contravariant => self.sup(cause, param_env, define_opaque_types, expected, actual),
256+
257+
// We could make this make sense but it's not readily
258+
// exposed and I don't feel like dealing with it. Note
259+
// that bivariance in general does a bit more than just
260+
// *nothing*, it checks that the types are the same
261+
// "modulo variance" basically.
262+
ty::Bivariant => panic!("Bivariant given to `relate()`"),
263+
}
264+
}
127265
}
128266

129267
pub trait ToTrace<'tcx>: Relate<TyCtxt<'tcx>> + Copy {

0 commit comments

Comments
 (0)