Skip to content

Commit a4cd220

Browse files
Use SolverRelating in favor of TypeRelating in the old solver where possible
1 parent 30a2ecd commit a4cd220

File tree

4 files changed

+92
-39
lines changed

4 files changed

+92
-39
lines changed

Diff for: compiler/rustc_infer/src/infer/at.rs

+71-27
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
2727
2828
use relate::lattice::{LatticeOp, LatticeOpKind};
2929
use rustc_middle::bug;
30+
use rustc_middle::ty::relate::solver_relating::RelateExt as NextSolverRelate;
3031
use rustc_middle::ty::{Const, ImplSubject};
3132

3233
use super::*;
3334
use crate::infer::relate::type_relating::TypeRelating;
3435
use crate::infer::relate::{Relate, TypeRelation};
36+
use crate::traits::Obligation;
37+
use crate::traits::solve::Goal;
3538

3639
/// Whether we should define opaque types or just treat them opaquely.
3740
///
@@ -109,15 +112,26 @@ impl<'a, 'tcx> At<'a, 'tcx> {
109112
where
110113
T: ToTrace<'tcx>,
111114
{
112-
let mut op = TypeRelating::new(
113-
self.infcx,
114-
ToTrace::to_trace(self.cause, expected, actual),
115-
self.param_env,
116-
define_opaque_types,
117-
ty::Contravariant,
118-
);
119-
op.relate(expected, actual)?;
120-
Ok(InferOk { value: (), obligations: op.into_obligations() })
115+
if self.infcx.next_trait_solver {
116+
NextSolverRelate::relate(
117+
self.infcx,
118+
self.param_env,
119+
expected,
120+
ty::Contravariant,
121+
actual,
122+
)
123+
.map(|goals| self.goals_to_obligations(goals))
124+
} else {
125+
let mut op = TypeRelating::new(
126+
self.infcx,
127+
ToTrace::to_trace(self.cause, expected, actual),
128+
self.param_env,
129+
define_opaque_types,
130+
ty::Contravariant,
131+
);
132+
op.relate(expected, actual)?;
133+
Ok(InferOk { value: (), obligations: op.into_obligations() })
134+
}
121135
}
122136

123137
/// Makes `expected <: actual`.
@@ -130,15 +144,20 @@ impl<'a, 'tcx> At<'a, 'tcx> {
130144
where
131145
T: ToTrace<'tcx>,
132146
{
133-
let mut op = TypeRelating::new(
134-
self.infcx,
135-
ToTrace::to_trace(self.cause, expected, actual),
136-
self.param_env,
137-
define_opaque_types,
138-
ty::Covariant,
139-
);
140-
op.relate(expected, actual)?;
141-
Ok(InferOk { value: (), obligations: op.into_obligations() })
147+
if self.infcx.next_trait_solver {
148+
NextSolverRelate::relate(self.infcx, self.param_env, expected, ty::Covariant, actual)
149+
.map(|goals| self.goals_to_obligations(goals))
150+
} else {
151+
let mut op = TypeRelating::new(
152+
self.infcx,
153+
ToTrace::to_trace(self.cause, expected, actual),
154+
self.param_env,
155+
define_opaque_types,
156+
ty::Covariant,
157+
);
158+
op.relate(expected, actual)?;
159+
Ok(InferOk { value: (), obligations: op.into_obligations() })
160+
}
142161
}
143162

144163
/// Makes `expected == actual`.
@@ -170,15 +189,20 @@ impl<'a, 'tcx> At<'a, 'tcx> {
170189
where
171190
T: Relate<TyCtxt<'tcx>>,
172191
{
173-
let mut op = TypeRelating::new(
174-
self.infcx,
175-
trace,
176-
self.param_env,
177-
define_opaque_types,
178-
ty::Invariant,
179-
);
180-
op.relate(expected, actual)?;
181-
Ok(InferOk { value: (), obligations: op.into_obligations() })
192+
if self.infcx.next_trait_solver {
193+
NextSolverRelate::relate(self.infcx, self.param_env, expected, ty::Invariant, actual)
194+
.map(|goals| self.goals_to_obligations(goals))
195+
} else {
196+
let mut op = TypeRelating::new(
197+
self.infcx,
198+
trace,
199+
self.param_env,
200+
define_opaque_types,
201+
ty::Invariant,
202+
);
203+
op.relate(expected, actual)?;
204+
Ok(InferOk { value: (), obligations: op.into_obligations() })
205+
}
182206
}
183207

184208
pub fn relate<T>(
@@ -223,6 +247,26 @@ impl<'a, 'tcx> At<'a, 'tcx> {
223247
let value = op.relate(expected, actual)?;
224248
Ok(InferOk { value, obligations: op.into_obligations() })
225249
}
250+
251+
fn goals_to_obligations(
252+
&self,
253+
goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
254+
) -> InferOk<'tcx, ()> {
255+
InferOk {
256+
value: (),
257+
obligations: goals
258+
.into_iter()
259+
.map(|goal| {
260+
Obligation::new(
261+
self.infcx.tcx,
262+
self.cause.clone(),
263+
goal.param_env,
264+
goal.predicate,
265+
)
266+
})
267+
.collect(),
268+
}
269+
}
226270
}
227271

228272
impl<'tcx> ToTrace<'tcx> for ImplSubject<'tcx> {

Diff for: compiler/rustc_infer/src/infer/relate/type_relating.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ impl<'infcx, 'tcx> TypeRelating<'infcx, 'tcx> {
5858
define_opaque_types: DefineOpaqueTypes,
5959
ambient_variance: ty::Variance,
6060
) -> TypeRelating<'infcx, 'tcx> {
61+
assert!(!infcx.next_trait_solver);
6162
TypeRelating {
6263
infcx,
6364
trace,
@@ -190,9 +191,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for TypeRelating<'_, 'tcx> {
190191

191192
(&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _)
192193
| (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }))
193-
if self.define_opaque_types == DefineOpaqueTypes::Yes
194-
&& def_id.is_local()
195-
&& !infcx.next_trait_solver() =>
194+
if self.define_opaque_types == DefineOpaqueTypes::Yes && def_id.is_local() =>
196195
{
197196
self.register_goals(infcx.handle_opaque_type(
198197
a,

Diff for: compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ where
836836
lhs: T,
837837
rhs: T,
838838
) -> Result<Vec<Goal<I, I::Predicate>>, NoSolution> {
839-
self.delegate.relate(param_env, lhs, ty::Variance::Invariant, rhs)
839+
Ok(self.delegate.relate(param_env, lhs, ty::Variance::Invariant, rhs)?)
840840
}
841841

842842
pub(super) fn instantiate_binder_with_infer<T: TypeFoldable<I> + Copy>(

Diff for: compiler/rustc_type_ir/src/relate/solver_relating.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub use rustc_type_ir::relate::*;
2-
use rustc_type_ir::solve::{Goal, NoSolution};
2+
use rustc_type_ir::solve::Goal;
33
use rustc_type_ir::{self as ty, InferCtxtLike, Interner};
44
use tracing::{debug, instrument};
55

@@ -13,14 +13,20 @@ pub trait RelateExt: InferCtxtLike {
1313
lhs: T,
1414
variance: ty::Variance,
1515
rhs: T,
16-
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>;
16+
) -> Result<
17+
Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>,
18+
TypeError<Self::Interner>,
19+
>;
1720

1821
fn eq_structurally_relating_aliases<T: Relate<Self::Interner>>(
1922
&self,
2023
param_env: <Self::Interner as Interner>::ParamEnv,
2124
lhs: T,
2225
rhs: T,
23-
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>;
26+
) -> Result<
27+
Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>,
28+
TypeError<Self::Interner>,
29+
>;
2430
}
2531

2632
impl<Infcx: InferCtxtLike> RelateExt for Infcx {
@@ -30,8 +36,10 @@ impl<Infcx: InferCtxtLike> RelateExt for Infcx {
3036
lhs: T,
3137
variance: ty::Variance,
3238
rhs: T,
33-
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>
34-
{
39+
) -> Result<
40+
Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>,
41+
TypeError<Self::Interner>,
42+
> {
3543
let mut relate =
3644
SolverRelating::new(self, StructurallyRelateAliases::No, variance, param_env);
3745
relate.relate(lhs, rhs)?;
@@ -43,8 +51,10 @@ impl<Infcx: InferCtxtLike> RelateExt for Infcx {
4351
param_env: <Self::Interner as Interner>::ParamEnv,
4452
lhs: T,
4553
rhs: T,
46-
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>
47-
{
54+
) -> Result<
55+
Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>,
56+
TypeError<Self::Interner>,
57+
> {
4858
let mut relate =
4959
SolverRelating::new(self, StructurallyRelateAliases::Yes, ty::Invariant, param_env);
5060
relate.relate(lhs, rhs)?;
@@ -91,7 +101,7 @@ where
91101
Infcx: InferCtxtLike<Interner = I>,
92102
I: Interner,
93103
{
94-
fn new(
104+
pub fn new(
95105
infcx: &'infcx Infcx,
96106
structurally_relate_aliases: StructurallyRelateAliases,
97107
ambient_variance: ty::Variance,

0 commit comments

Comments
 (0)