Skip to content

Commit 5fa0570

Browse files
Use SolverRelating in favor of TypeRelating in the old solver where possible
1 parent e43376a commit 5fa0570

File tree

3 files changed

+96
-42
lines changed

3 files changed

+96
-42
lines changed

compiler/rustc_infer/src/infer/at.rs

+77-33
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
//! things. (That system should probably be refactored.)
2727
2828
use rustc_middle::bug;
29+
use rustc_middle::ty::relate::solver_relating::RelateExt as NextSolverRelate;
2930
use rustc_middle::ty::{Const, ImplSubject};
3031

3132
use super::*;
3233
use crate::infer::relate::{Relate, TypeRelation};
3334
use crate::traits::Obligation;
35+
use crate::traits::solve::Goal;
3436

3537
/// Whether we should define opaque types or just treat them opaquely.
3638
///
@@ -108,14 +110,25 @@ impl<'a, 'tcx> At<'a, 'tcx> {
108110
where
109111
T: ToTrace<'tcx>,
110112
{
111-
let mut fields = CombineFields::new(
112-
self.infcx,
113-
ToTrace::to_trace(self.cause, expected, actual),
114-
self.param_env,
115-
define_opaque_types,
116-
);
117-
fields.sup().relate(expected, actual)?;
118-
Ok(InferOk { value: (), obligations: fields.into_obligations() })
113+
if self.infcx.next_trait_solver() {
114+
NextSolverRelate::relate(
115+
self.infcx,
116+
self.param_env,
117+
expected,
118+
ty::Contravariant,
119+
actual,
120+
)
121+
.map(|goals| self.goals_to_obligations(goals))
122+
} else {
123+
let mut fields = CombineFields::new(
124+
self.infcx,
125+
ToTrace::to_trace(self.cause, expected, actual),
126+
self.param_env,
127+
define_opaque_types,
128+
);
129+
fields.sup().relate(expected, actual)?;
130+
Ok(InferOk { value: (), obligations: fields.into_obligations() })
131+
}
119132
}
120133

121134
/// Makes `expected <: actual`.
@@ -128,14 +141,19 @@ impl<'a, 'tcx> At<'a, 'tcx> {
128141
where
129142
T: ToTrace<'tcx>,
130143
{
131-
let mut fields = CombineFields::new(
132-
self.infcx,
133-
ToTrace::to_trace(self.cause, expected, actual),
134-
self.param_env,
135-
define_opaque_types,
136-
);
137-
fields.sub().relate(expected, actual)?;
138-
Ok(InferOk { value: (), obligations: fields.into_obligations() })
144+
if self.infcx.next_trait_solver() {
145+
NextSolverRelate::relate(self.infcx, self.param_env, expected, ty::Covariant, actual)
146+
.map(|goals| self.goals_to_obligations(goals))
147+
} else {
148+
let mut fields = CombineFields::new(
149+
self.infcx,
150+
ToTrace::to_trace(self.cause, expected, actual),
151+
self.param_env,
152+
define_opaque_types,
153+
);
154+
fields.sub().relate(expected, actual)?;
155+
Ok(InferOk { value: (), obligations: fields.into_obligations() })
156+
}
139157
}
140158

141159
/// Makes `expected == actual`.
@@ -167,23 +185,29 @@ impl<'a, 'tcx> At<'a, 'tcx> {
167185
where
168186
T: Relate<TyCtxt<'tcx>>,
169187
{
170-
let mut fields = CombineFields::new(self.infcx, trace, self.param_env, define_opaque_types);
171-
fields.equate().relate(expected, actual)?;
172-
Ok(InferOk {
173-
value: (),
174-
obligations: fields
175-
.goals
176-
.into_iter()
177-
.map(|goal| {
178-
Obligation::new(
179-
self.infcx.tcx,
180-
fields.trace.cause.clone(),
181-
goal.param_env,
182-
goal.predicate,
183-
)
184-
})
185-
.collect(),
186-
})
188+
if self.infcx.next_trait_solver() {
189+
NextSolverRelate::relate(self.infcx, self.param_env, expected, ty::Invariant, actual)
190+
.map(|goals| self.goals_to_obligations(goals))
191+
} else {
192+
let mut fields =
193+
CombineFields::new(self.infcx, trace, self.param_env, define_opaque_types);
194+
fields.equate().relate(expected, actual)?;
195+
Ok(InferOk {
196+
value: (),
197+
obligations: fields
198+
.goals
199+
.into_iter()
200+
.map(|goal| {
201+
Obligation::new(
202+
self.infcx.tcx,
203+
fields.trace.cause.clone(),
204+
goal.param_env,
205+
goal.predicate,
206+
)
207+
})
208+
.collect(),
209+
})
210+
}
187211
}
188212

189213
pub fn relate<T>(
@@ -255,6 +279,26 @@ impl<'a, 'tcx> At<'a, 'tcx> {
255279
let value = fields.glb().relate(expected, actual)?;
256280
Ok(InferOk { value, obligations: fields.into_obligations() })
257281
}
282+
283+
fn goals_to_obligations(
284+
&self,
285+
goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
286+
) -> InferOk<'tcx, ()> {
287+
InferOk {
288+
value: (),
289+
obligations: goals
290+
.into_iter()
291+
.map(|goal| {
292+
Obligation::new(
293+
self.infcx.tcx,
294+
self.cause.clone(),
295+
goal.param_env,
296+
goal.predicate,
297+
)
298+
})
299+
.collect(),
300+
}
301+
}
258302
}
259303

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

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>(

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

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

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

2531
impl<Infcx: InferCtxtLike> RelateExt for Infcx {
@@ -29,8 +35,10 @@ impl<Infcx: InferCtxtLike> RelateExt for Infcx {
2935
lhs: T,
3036
variance: ty::Variance,
3137
rhs: T,
32-
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>
33-
{
38+
) -> Result<
39+
Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>,
40+
TypeError<Self::Interner>,
41+
> {
3442
let mut relate =
3543
SolverRelating::new(self, StructurallyRelateAliases::No, variance, param_env);
3644
relate.relate(lhs, rhs)?;
@@ -42,8 +50,10 @@ impl<Infcx: InferCtxtLike> RelateExt for Infcx {
4250
param_env: <Self::Interner as Interner>::ParamEnv,
4351
lhs: T,
4452
rhs: T,
45-
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>
46-
{
53+
) -> Result<
54+
Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>,
55+
TypeError<Self::Interner>,
56+
> {
4757
let mut relate =
4858
SolverRelating::new(self, StructurallyRelateAliases::Yes, ty::Invariant, param_env);
4959
relate.relate(lhs, rhs)?;
@@ -66,7 +76,7 @@ where
6676
Infcx: InferCtxtLike<Interner = I>,
6777
I: Interner,
6878
{
69-
fn new(
79+
pub fn new(
7080
infcx: &'infcx Infcx,
7181
structurally_relate_aliases: StructurallyRelateAliases,
7282
ambient_variance: ty::Variance,

0 commit comments

Comments
 (0)