Skip to content

Commit 1794eec

Browse files
Use SolverRelating in new solver
1 parent dd2a6e8 commit 1794eec

File tree

8 files changed

+67
-118
lines changed

8 files changed

+67
-118
lines changed

compiler/rustc_infer/src/infer/at.rs

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -186,28 +186,6 @@ impl<'a, 'tcx> At<'a, 'tcx> {
186186
})
187187
}
188188

189-
/// Equates `expected` and `found` while structurally relating aliases.
190-
/// This should only be used inside of the next generation trait solver
191-
/// when relating rigid aliases.
192-
pub fn eq_structurally_relating_aliases<T>(
193-
self,
194-
expected: T,
195-
actual: T,
196-
) -> InferResult<'tcx, ()>
197-
where
198-
T: ToTrace<'tcx>,
199-
{
200-
assert!(self.infcx.next_trait_solver());
201-
let mut fields = CombineFields::new(
202-
self.infcx,
203-
ToTrace::to_trace(self.cause, expected, actual),
204-
self.param_env,
205-
DefineOpaqueTypes::Yes,
206-
);
207-
fields.equate(StructurallyRelateAliases::Yes).relate(expected, actual)?;
208-
Ok(InferOk { value: (), obligations: fields.into_obligations() })
209-
}
210-
211189
pub fn relate<T>(
212190
self,
213191
define_opaque_types: DefineOpaqueTypes,
@@ -232,50 +210,6 @@ impl<'a, 'tcx> At<'a, 'tcx> {
232210
}
233211
}
234212

235-
/// Used in the new solver since we don't care about tracking an `ObligationCause`.
236-
pub fn relate_no_trace<T>(
237-
self,
238-
expected: T,
239-
variance: ty::Variance,
240-
actual: T,
241-
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution>
242-
where
243-
T: Relate<TyCtxt<'tcx>>,
244-
{
245-
let mut fields = CombineFields::new(
246-
self.infcx,
247-
TypeTrace::dummy(self.cause),
248-
self.param_env,
249-
DefineOpaqueTypes::Yes,
250-
);
251-
fields.sub().relate_with_variance(
252-
variance,
253-
ty::VarianceDiagInfo::default(),
254-
expected,
255-
actual,
256-
)?;
257-
Ok(fields.goals)
258-
}
259-
260-
/// Used in the new solver since we don't care about tracking an `ObligationCause`.
261-
pub fn eq_structurally_relating_aliases_no_trace<T>(
262-
self,
263-
expected: T,
264-
actual: T,
265-
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution>
266-
where
267-
T: Relate<TyCtxt<'tcx>>,
268-
{
269-
let mut fields = CombineFields::new(
270-
self.infcx,
271-
TypeTrace::dummy(self.cause),
272-
self.param_env,
273-
DefineOpaqueTypes::Yes,
274-
);
275-
fields.equate(StructurallyRelateAliases::Yes).relate(expected, actual)?;
276-
Ok(fields.goals)
277-
}
278-
279213
/// Computes the least-upper-bound, or mutual supertype, of two
280214
/// values. The order of the arguments doesn't matter, but since
281215
/// this can result in an error (e.g., if asked to compute LUB of

compiler/rustc_infer/src/infer/context.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
use rustc_hir::def_id::{DefId, LocalDefId};
33
use rustc_middle::infer::unify_key::EffectVarValue;
44
use rustc_middle::traits::ObligationCause;
5-
use rustc_middle::traits::solve::{Goal, NoSolution, SolverMode};
5+
use rustc_middle::traits::solve::SolverMode;
66
use rustc_middle::ty::fold::TypeFoldable;
7+
use rustc_middle::ty::relate::RelateResult;
78
use rustc_middle::ty::relate::combine::PredicateEmittingRelation;
8-
use rustc_middle::ty::relate::{Relate, RelateResult};
99
use rustc_middle::ty::{self, Ty, TyCtxt};
1010
use rustc_span::{DUMMY_SP, ErrorGuaranteed};
1111

@@ -210,26 +210,6 @@ impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
210210
self.set_tainted_by_errors(e)
211211
}
212212

213-
fn relate<T: Relate<TyCtxt<'tcx>>>(
214-
&self,
215-
param_env: ty::ParamEnv<'tcx>,
216-
lhs: T,
217-
variance: ty::Variance,
218-
rhs: T,
219-
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> {
220-
self.at(&ObligationCause::dummy(), param_env).relate_no_trace(lhs, variance, rhs)
221-
}
222-
223-
fn eq_structurally_relating_aliases<T: Relate<TyCtxt<'tcx>>>(
224-
&self,
225-
param_env: ty::ParamEnv<'tcx>,
226-
lhs: T,
227-
rhs: T,
228-
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> {
229-
self.at(&ObligationCause::dummy(), param_env)
230-
.eq_structurally_relating_aliases_no_trace(lhs, rhs)
231-
}
232-
233213
fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
234214
self.shallow_resolve(ty)
235215
}

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use rustc_middle::infer::unify_key::{
3333
use rustc_middle::mir::ConstraintCategory;
3434
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
3535
use rustc_middle::traits::select;
36-
use rustc_middle::traits::solve::{Goal, NoSolution};
3736
pub use rustc_middle::ty::IntVarValue;
3837
use rustc_middle::ty::error::{ExpectedFound, TypeError};
3938
use rustc_middle::ty::fold::{
@@ -333,7 +332,6 @@ pub enum ValuePairs<'tcx> {
333332
PolySigs(ExpectedFound<ty::PolyFnSig<'tcx>>),
334333
ExistentialTraitRef(ExpectedFound<ty::PolyExistentialTraitRef<'tcx>>),
335334
ExistentialProjection(ExpectedFound<ty::PolyExistentialProjection<'tcx>>),
336-
Dummy,
337335
}
338336

339337
impl<'tcx> ValuePairs<'tcx> {
@@ -1635,10 +1633,6 @@ impl<'tcx> TypeTrace<'tcx> {
16351633
values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
16361634
}
16371635
}
1638-
1639-
fn dummy(cause: &ObligationCause<'tcx>) -> TypeTrace<'tcx> {
1640-
TypeTrace { cause: cause.clone(), values: ValuePairs::Dummy }
1641-
}
16421636
}
16431637

16441638
impl<'tcx> SubregionOrigin<'tcx> {

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::iter;
1414
use rustc_index::IndexVec;
1515
use rustc_type_ir::fold::TypeFoldable;
1616
use rustc_type_ir::inherent::*;
17+
use rustc_type_ir::relate::solver_relating::RelateExt;
1718
use rustc_type_ir::{self as ty, Canonical, CanonicalVarValues, InferCtxtLike, Interner};
1819
use tracing::{instrument, trace};
1920

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_type_ir::data_structures::{HashMap, HashSet, ensure_sufficient_stack};
77
use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
88
use rustc_type_ir::inherent::*;
99
use rustc_type_ir::relate::Relate;
10+
use rustc_type_ir::relate::solver_relating::RelateExt;
1011
use rustc_type_ir::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
1112
use rustc_type_ir::{self as ty, CanonicalVarValues, InferCtxtLike, Interner};
1213
use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic};

compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,9 +1285,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
12851285
ValuePairs::ExistentialProjection(_) => {
12861286
(false, Mismatch::Fixed("existential projection"))
12871287
}
1288-
ValuePairs::Dummy => {
1289-
bug!("do not expect to report a type error from a ValuePairs::Dummy")
1290-
}
12911288
};
12921289
let Some(vals) = self.values_str(values) else {
12931290
// Derived error. Cancel the emitter.
@@ -1853,9 +1850,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
18531850
let (exp, fnd) = self.cmp_fn_sig(&exp_found.expected, &exp_found.found);
18541851
Some((exp, fnd, None))
18551852
}
1856-
ValuePairs::Dummy => {
1857-
bug!("do not expect to report a type error from a ValuePairs::Dummy")
1858-
}
18591853
}
18601854
}
18611855

compiler/rustc_type_ir/src/infer_ctxt.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::fold::TypeFoldable;
2+
use crate::relate::RelateResult;
23
use crate::relate::combine::PredicateEmittingRelation;
3-
use crate::relate::{Relate, RelateResult};
4-
use crate::solve::{Goal, NoSolution, SolverMode};
4+
use crate::solve::SolverMode;
55
use crate::{self as ty, Interner};
66

77
pub trait InferCtxtLike: Sized {
@@ -98,21 +98,6 @@ pub trait InferCtxtLike: Sized {
9898

9999
fn set_tainted_by_errors(&self, e: <Self::Interner as Interner>::ErrorGuaranteed);
100100

101-
fn relate<T: Relate<Self::Interner>>(
102-
&self,
103-
param_env: <Self::Interner as Interner>::ParamEnv,
104-
lhs: T,
105-
variance: ty::Variance,
106-
rhs: T,
107-
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>;
108-
109-
fn eq_structurally_relating_aliases<T: Relate<Self::Interner>>(
110-
&self,
111-
param_env: <Self::Interner as Interner>::ParamEnv,
112-
lhs: T,
113-
rhs: T,
114-
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>;
115-
116101
fn shallow_resolve(
117102
&self,
118103
ty: <Self::Interner as Interner>::Ty,

compiler/rustc_type_ir/src/relate/solver_relating.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,56 @@
11
pub use rustc_type_ir::relate::*;
2-
use rustc_type_ir::solve::Goal;
2+
use rustc_type_ir::solve::{Goal, NoSolution};
33
use rustc_type_ir::{self as ty, InferCtxtLike, Interner};
44
use tracing::{debug, instrument};
55

66
use self::combine::{InferCtxtCombineExt, PredicateEmittingRelation};
77

8+
pub trait RelateExt: InferCtxtLike {
9+
fn relate<T: Relate<Self::Interner>>(
10+
&self,
11+
param_env: <Self::Interner as Interner>::ParamEnv,
12+
lhs: T,
13+
variance: ty::Variance,
14+
rhs: T,
15+
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>;
16+
17+
fn eq_structurally_relating_aliases<T: Relate<Self::Interner>>(
18+
&self,
19+
param_env: <Self::Interner as Interner>::ParamEnv,
20+
lhs: T,
21+
rhs: T,
22+
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>;
23+
}
24+
25+
impl<Infcx: InferCtxtLike> RelateExt for Infcx {
26+
fn relate<T: Relate<Self::Interner>>(
27+
&self,
28+
param_env: <Self::Interner as Interner>::ParamEnv,
29+
lhs: T,
30+
variance: ty::Variance,
31+
rhs: T,
32+
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>
33+
{
34+
let mut relate =
35+
SolverRelating::new(self, StructurallyRelateAliases::No, variance, param_env);
36+
relate.relate(lhs, rhs)?;
37+
Ok(relate.goals)
38+
}
39+
40+
fn eq_structurally_relating_aliases<T: Relate<Self::Interner>>(
41+
&self,
42+
param_env: <Self::Interner as Interner>::ParamEnv,
43+
lhs: T,
44+
rhs: T,
45+
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>
46+
{
47+
let mut relate =
48+
SolverRelating::new(self, StructurallyRelateAliases::Yes, ty::Invariant, param_env);
49+
relate.relate(lhs, rhs)?;
50+
Ok(relate.goals)
51+
}
52+
}
53+
854
#[allow(unused)]
955
/// Enforce that `a` is equal to or a subtype of `b`.
1056
pub struct SolverRelating<'infcx, Infcx, I: Interner> {
@@ -20,6 +66,20 @@ where
2066
Infcx: InferCtxtLike<Interner = I>,
2167
I: Interner,
2268
{
69+
fn new(
70+
infcx: &'infcx Infcx,
71+
structurally_relate_aliases: StructurallyRelateAliases,
72+
ambient_variance: ty::Variance,
73+
param_env: I::ParamEnv,
74+
) -> Self {
75+
SolverRelating {
76+
infcx,
77+
structurally_relate_aliases,
78+
ambient_variance,
79+
param_env,
80+
goals: vec![],
81+
}
82+
}
2383
}
2484

2585
impl<Infcx, I> TypeRelation<I> for SolverRelating<'_, Infcx, I>

0 commit comments

Comments
 (0)