Skip to content

Commit 3da257a

Browse files
Use SolverRelating in new solver
1 parent efb1c23 commit 3da257a

File tree

9 files changed

+68
-97
lines changed

9 files changed

+68
-97
lines changed

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

-43
Original file line numberDiff line numberDiff line change
@@ -208,49 +208,6 @@ impl<'a, 'tcx> At<'a, 'tcx> {
208208
}
209209
}
210210

211-
/// Used in the new solver since we don't care about tracking an `ObligationCause`.
212-
pub fn relate_no_trace<T>(
213-
self,
214-
expected: T,
215-
variance: ty::Variance,
216-
actual: T,
217-
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution>
218-
where
219-
T: Relate<TyCtxt<'tcx>>,
220-
{
221-
let mut op = TypeRelating::new(
222-
self.infcx,
223-
TypeTrace::dummy(self.cause),
224-
self.param_env,
225-
DefineOpaqueTypes::Yes,
226-
StructurallyRelateAliases::No,
227-
variance,
228-
);
229-
op.relate(expected, actual)?;
230-
Ok(op.into_obligations().into_iter().map(|o| o.into()).collect())
231-
}
232-
233-
/// Used in the new solver since we don't care about tracking an `ObligationCause`.
234-
pub fn eq_structurally_relating_aliases_no_trace<T>(
235-
self,
236-
expected: T,
237-
actual: T,
238-
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution>
239-
where
240-
T: Relate<TyCtxt<'tcx>>,
241-
{
242-
let mut op = TypeRelating::new(
243-
self.infcx,
244-
TypeTrace::dummy(self.cause),
245-
self.param_env,
246-
DefineOpaqueTypes::Yes,
247-
StructurallyRelateAliases::Yes,
248-
ty::Invariant,
249-
);
250-
op.relate(expected, actual)?;
251-
Ok(op.into_obligations().into_iter().map(|o| o.into()).collect())
252-
}
253-
254211
/// Computes the least-upper-bound, or mutual supertype, of two
255212
/// values. The order of the arguments doesn't matter, but since
256213
/// this can result in an error (e.g., if asked to compute LUB of

Diff for: 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
}

Diff for: 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::{
@@ -340,7 +339,6 @@ pub enum ValuePairs<'tcx> {
340339
PolySigs(ExpectedFound<ty::PolyFnSig<'tcx>>),
341340
ExistentialTraitRef(ExpectedFound<ty::PolyExistentialTraitRef<'tcx>>),
342341
ExistentialProjection(ExpectedFound<ty::PolyExistentialProjection<'tcx>>),
343-
Dummy,
344342
}
345343

346344
impl<'tcx> ValuePairs<'tcx> {
@@ -1638,10 +1636,6 @@ impl<'tcx> TypeTrace<'tcx> {
16381636
values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
16391637
}
16401638
}
1641-
1642-
fn dummy(cause: &ObligationCause<'tcx>) -> TypeTrace<'tcx> {
1643-
TypeTrace { cause: cause.clone(), values: ValuePairs::Dummy }
1644-
}
16451639
}
16461640

16471641
impl<'tcx> SubregionOrigin<'tcx> {

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

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ pub use rustc_middle::ty::relate::RelateResult;
66
pub use rustc_type_ir::relate::combine::PredicateEmittingRelation;
77
pub use rustc_type_ir::relate::*;
88

9-
#[allow(hidden_glob_reexports)]
109
mod generalize;
1110
mod higher_ranked;
1211
pub(super) mod lattice;

Diff for: 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

Diff for: 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};

Diff for: 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

Diff for: 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,

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

+62-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,57 @@
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
use crate::data_structures::DelayedSet;
88

9-
#[allow(unused)]
9+
pub trait RelateExt: InferCtxtLike {
10+
fn relate<T: Relate<Self::Interner>>(
11+
&self,
12+
param_env: <Self::Interner as Interner>::ParamEnv,
13+
lhs: T,
14+
variance: ty::Variance,
15+
rhs: T,
16+
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>;
17+
18+
fn eq_structurally_relating_aliases<T: Relate<Self::Interner>>(
19+
&self,
20+
param_env: <Self::Interner as Interner>::ParamEnv,
21+
lhs: T,
22+
rhs: T,
23+
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>;
24+
}
25+
26+
impl<Infcx: InferCtxtLike> RelateExt for Infcx {
27+
fn relate<T: Relate<Self::Interner>>(
28+
&self,
29+
param_env: <Self::Interner as Interner>::ParamEnv,
30+
lhs: T,
31+
variance: ty::Variance,
32+
rhs: T,
33+
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>
34+
{
35+
let mut relate =
36+
SolverRelating::new(self, StructurallyRelateAliases::No, variance, param_env);
37+
relate.relate(lhs, rhs)?;
38+
Ok(relate.goals)
39+
}
40+
41+
fn eq_structurally_relating_aliases<T: Relate<Self::Interner>>(
42+
&self,
43+
param_env: <Self::Interner as Interner>::ParamEnv,
44+
lhs: T,
45+
rhs: T,
46+
) -> Result<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>, NoSolution>
47+
{
48+
let mut relate =
49+
SolverRelating::new(self, StructurallyRelateAliases::Yes, ty::Invariant, param_env);
50+
relate.relate(lhs, rhs)?;
51+
Ok(relate.goals)
52+
}
53+
}
54+
1055
/// Enforce that `a` is equal to or a subtype of `b`.
1156
pub struct SolverRelating<'infcx, Infcx, I: Interner> {
1257
infcx: &'infcx Infcx,
@@ -46,6 +91,21 @@ where
4691
Infcx: InferCtxtLike<Interner = I>,
4792
I: Interner,
4893
{
94+
fn new(
95+
infcx: &'infcx Infcx,
96+
structurally_relate_aliases: StructurallyRelateAliases,
97+
ambient_variance: ty::Variance,
98+
param_env: I::ParamEnv,
99+
) -> Self {
100+
SolverRelating {
101+
infcx,
102+
structurally_relate_aliases,
103+
ambient_variance,
104+
param_env,
105+
goals: vec![],
106+
cache: Default::default(),
107+
}
108+
}
49109
}
50110

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

0 commit comments

Comments
 (0)