Skip to content

Commit 30e7682

Browse files
Use SolverRelating in new solver
1 parent 745010b commit 30e7682

File tree

8 files changed

+67
-118
lines changed

8 files changed

+67
-118
lines changed

compiler/rustc_infer/src/infer/at.rs

-66
Original file line numberDiff line numberDiff line change
@@ -187,28 +187,6 @@ impl<'a, 'tcx> At<'a, 'tcx> {
187187
})
188188
}
189189

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

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

compiler/rustc_infer/src/infer/context.rs

+2-22
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

-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use rustc_middle::infer::unify_key::{
3232
use rustc_middle::mir::ConstraintCategory;
3333
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
3434
use rustc_middle::traits::select;
35-
use rustc_middle::traits::solve::{Goal, NoSolution};
3635
pub use rustc_middle::ty::IntVarValue;
3736
use rustc_middle::ty::error::{ExpectedFound, TypeError};
3837
use rustc_middle::ty::fold::{
@@ -347,7 +346,6 @@ pub enum ValuePairs<'tcx> {
347346
PolySigs(ExpectedFound<ty::PolyFnSig<'tcx>>),
348347
ExistentialTraitRef(ExpectedFound<ty::PolyExistentialTraitRef<'tcx>>),
349348
ExistentialProjection(ExpectedFound<ty::PolyExistentialProjection<'tcx>>),
350-
Dummy,
351349
}
352350

353351
impl<'tcx> ValuePairs<'tcx> {
@@ -1674,10 +1672,6 @@ impl<'tcx> TypeTrace<'tcx> {
16741672
values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
16751673
}
16761674
}
1677-
1678-
fn dummy(cause: &ObligationCause<'tcx>) -> TypeTrace<'tcx> {
1679-
TypeTrace { cause: cause.clone(), values: ValuePairs::Dummy }
1680-
}
16811675
}
16821676

16831677
impl<'tcx> SubregionOrigin<'tcx> {

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

+1
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

+1
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

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

+2-17
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

+61-1
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)