Skip to content

Commit 1c5aed1

Browse files
Uplift PredicateEmittingRelation
1 parent 27588d1 commit 1c5aed1

File tree

16 files changed

+60
-68
lines changed

16 files changed

+60
-68
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ impl<'bccx, 'tcx> TypeRelation<TyCtxt<'tcx>> for NllTypeRelating<'_, 'bccx, 'tcx
522522
}
523523
}
524524

525-
impl<'bccx, 'tcx> PredicateEmittingRelation<'tcx> for NllTypeRelating<'_, 'bccx, 'tcx> {
525+
impl<'bccx, 'tcx> PredicateEmittingRelation<TyCtxt<'tcx>> for NllTypeRelating<'_, 'bccx, 'tcx> {
526526
fn span(&self) -> Span {
527527
self.locations.span(self.type_checker.body)
528528
}

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ pub use at::DefineOpaqueTypes;
22
pub use freshen::TypeFreshener;
33
pub use lexical_region_resolve::RegionResolutionError;
44
pub use relate::combine::CombineFields;
5-
pub use relate::combine::PredicateEmittingRelation;
65
pub use relate::StructurallyRelateAliases;
76
use rustc_errors::DiagCtxtHandle;
87
pub use rustc_macros::{TypeFoldable, TypeVisitable};

compiler/rustc_infer/src/infer/relate/combine.rs

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
use super::glb::Glb;
2222
use super::lub::Lub;
2323
use super::type_relating::TypeRelating;
24-
use super::StructurallyRelateAliases;
25-
use super::{RelateResult, TypeRelation};
26-
use crate::infer::relate;
24+
use crate::infer::relate::{
25+
self, PredicateEmittingRelation, RelateResult, StructurallyRelateAliases,
26+
};
2727
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace};
2828
use crate::traits::{Obligation, PredicateObligation};
2929
use rustc_middle::bug;
@@ -32,7 +32,6 @@ use rustc_middle::traits::solve::Goal;
3232
use rustc_middle::ty::error::{ExpectedFound, TypeError};
3333
use rustc_middle::ty::{self, InferConst, Ty, TyCtxt, TypeVisitableExt, Upcast};
3434
use rustc_middle::ty::{IntType, UintType};
35-
use rustc_span::Span;
3635

3736
#[derive(Clone)]
3837
pub struct CombineFields<'infcx, 'tcx> {
@@ -76,7 +75,7 @@ impl<'tcx> InferCtxt<'tcx> {
7675
b: Ty<'tcx>,
7776
) -> RelateResult<'tcx, Ty<'tcx>>
7877
where
79-
R: PredicateEmittingRelation<'tcx>,
78+
R: PredicateEmittingRelation<TyCtxt<'tcx>>,
8079
{
8180
debug_assert!(!a.has_escaping_bound_vars());
8281
debug_assert!(!b.has_escaping_bound_vars());
@@ -171,7 +170,7 @@ impl<'tcx> InferCtxt<'tcx> {
171170
b: ty::Const<'tcx>,
172171
) -> RelateResult<'tcx, ty::Const<'tcx>>
173172
where
174-
R: PredicateEmittingRelation<'tcx>,
173+
R: PredicateEmittingRelation<TyCtxt<'tcx>>,
175174
{
176175
debug!("{}.consts({:?}, {:?})", relation.tag(), a, b);
177176
debug_assert!(!a.has_escaping_bound_vars());
@@ -323,30 +322,3 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
323322
)
324323
}
325324
}
326-
327-
pub trait PredicateEmittingRelation<'tcx>: TypeRelation<TyCtxt<'tcx>> {
328-
fn span(&self) -> Span;
329-
330-
fn param_env(&self) -> ty::ParamEnv<'tcx>;
331-
332-
/// Whether aliases should be related structurally. This is pretty much
333-
/// always `No` unless you're equating in some specific locations of the
334-
/// new solver. See the comments in these use-cases for more details.
335-
fn structurally_relate_aliases(&self) -> StructurallyRelateAliases;
336-
337-
/// Register obligations that must hold in order for this relation to hold
338-
fn register_goals(
339-
&mut self,
340-
obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
341-
);
342-
343-
/// Register predicates that must hold in order for this relation to hold.
344-
/// This uses the default `param_env` of the obligation.
345-
fn register_predicates(
346-
&mut self,
347-
obligations: impl IntoIterator<Item: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
348-
);
349-
350-
/// Register `AliasRelate` obligation(s) that both types must be related to each other.
351-
fn register_alias_relate_predicate(&mut self, a: Ty<'tcx>, b: Ty<'tcx>);
352-
}

compiler/rustc_infer/src/infer/relate/generalize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'tcx> InferCtxt<'tcx> {
3030
/// `TypeRelation`. Do not use this, and instead please use `At::eq`, for all
3131
/// other usecases (i.e. setting the value of a type var).
3232
#[instrument(level = "debug", skip(self, relation))]
33-
pub fn instantiate_ty_var<R: PredicateEmittingRelation<'tcx>>(
33+
pub fn instantiate_ty_var<R: PredicateEmittingRelation<TyCtxt<'tcx>>>(
3434
&self,
3535
relation: &mut R,
3636
target_is_expected: bool,
@@ -178,7 +178,7 @@ impl<'tcx> InferCtxt<'tcx> {
178178
///
179179
/// See `tests/ui/const-generics/occurs-check/` for more examples where this is relevant.
180180
#[instrument(level = "debug", skip(self, relation))]
181-
pub(super) fn instantiate_const_var<R: PredicateEmittingRelation<'tcx>>(
181+
pub(super) fn instantiate_const_var<R: PredicateEmittingRelation<TyCtxt<'tcx>>>(
182182
&self,
183183
relation: &mut R,
184184
target_is_expected: bool,

compiler/rustc_infer/src/infer/relate/glb.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Greatest lower bound. See [`lattice`].
22
33
use rustc_middle::traits::solve::Goal;
4-
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
4+
use rustc_middle::ty::relate::{PredicateEmittingRelation, Relate, RelateResult, TypeRelation};
55
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
66
use rustc_span::Span;
77

8-
use super::combine::{CombineFields, PredicateEmittingRelation};
8+
use super::combine::CombineFields;
99
use super::lattice::{self, LatticeDir};
1010
use super::StructurallyRelateAliases;
1111
use crate::infer::{DefineOpaqueTypes, InferCtxt, SubregionOrigin};
@@ -123,7 +123,7 @@ impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Glb<'combine, 'infcx,
123123
}
124124
}
125125

126-
impl<'tcx> PredicateEmittingRelation<'tcx> for Glb<'_, '_, 'tcx> {
126+
impl<'tcx> PredicateEmittingRelation<TyCtxt<'tcx>> for Glb<'_, '_, 'tcx> {
127127
fn span(&self) -> Span {
128128
self.fields.trace.span()
129129
}

compiler/rustc_infer/src/infer/relate/lattice.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@
1717
//!
1818
//! [lattices]: https://en.wikipedia.org/wiki/Lattice_(order)
1919
20-
use super::combine::PredicateEmittingRelation;
2120
use crate::infer::{DefineOpaqueTypes, InferCtxt};
2221
use crate::traits::ObligationCause;
2322

24-
use rustc_middle::ty::relate::RelateResult;
23+
use rustc_middle::ty::relate::{PredicateEmittingRelation, RelateResult};
2524
use rustc_middle::ty::TyVar;
26-
use rustc_middle::ty::{self, Ty};
25+
use rustc_middle::ty::{self, Ty, TyCtxt};
2726

2827
/// Trait for returning data about a lattice, and for abstracting
2928
/// over the "direction" of the lattice operation (LUB/GLB).
3029
///
3130
/// GLB moves "down" the lattice (to smaller values); LUB moves
3231
/// "up" the lattice (to bigger values).
33-
pub trait LatticeDir<'f, 'tcx>: PredicateEmittingRelation<'tcx> {
32+
pub trait LatticeDir<'f, 'tcx>: PredicateEmittingRelation<TyCtxt<'tcx>> {
3433
fn infcx(&self) -> &'f InferCtxt<'tcx>;
3534

3635
fn cause(&self) -> &ObligationCause<'tcx>;

compiler/rustc_infer/src/infer/relate/lub.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Least upper bound. See [`lattice`].
22
3-
use super::combine::{CombineFields, PredicateEmittingRelation};
3+
use super::combine::CombineFields;
44
use super::lattice::{self, LatticeDir};
55
use super::StructurallyRelateAliases;
66
use crate::infer::{DefineOpaqueTypes, InferCtxt, SubregionOrigin};
77
use crate::traits::ObligationCause;
88

99
use rustc_middle::traits::solve::Goal;
10-
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
10+
use rustc_middle::ty::relate::{PredicateEmittingRelation, Relate, RelateResult, TypeRelation};
1111
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
1212
use rustc_span::Span;
1313

@@ -123,7 +123,7 @@ impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Lub<'combine, 'infcx,
123123
}
124124
}
125125

126-
impl<'tcx> PredicateEmittingRelation<'tcx> for Lub<'_, '_, 'tcx> {
126+
impl<'tcx> PredicateEmittingRelation<TyCtxt<'tcx>> for Lub<'_, '_, 'tcx> {
127127
fn span(&self) -> Span {
128128
self.fields.trace.span()
129129
}

compiler/rustc_infer/src/infer/relate/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
pub use rustc_middle::ty::relate::*;
66

77
pub use self::combine::CombineFields;
8-
pub use self::combine::PredicateEmittingRelation;
98

109
pub(super) mod combine;
1110
mod generalize;

compiler/rustc_infer/src/infer/relate/type_relating.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for TypeRelating<'_, '_, 'tcx> {
296296
}
297297
}
298298

299-
impl<'tcx> PredicateEmittingRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
299+
impl<'tcx> PredicateEmittingRelation<TyCtxt<'tcx>> for TypeRelating<'_, '_, 'tcx> {
300300
fn span(&self) -> Span {
301301
self.fields.trace.span()
302302
}

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
9393
type DefId = DefId;
9494
type LocalDefId = LocalDefId;
9595
type GenericArgs = ty::GenericArgsRef<'tcx>;
96+
type Span = Span;
9697

9798
type GenericArgsSlice = &'tcx [ty::GenericArg<'tcx>];
9899
type GenericArg = ty::GenericArg<'tcx>;

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ use crate::ty::{self as ty, Ty, TyCtxt};
1010

1111
pub type RelateResult<'tcx, T> = rustc_type_ir::relate::RelateResult<TyCtxt<'tcx>, T>;
1212

13-
/// Whether aliases should be related structurally or not. Used
14-
/// to adjust the behavior of generalization and combine.
15-
///
16-
/// This should always be `No` unless in a few special-cases when
17-
/// instantiating canonical responses and in the new solver. Each
18-
/// such case should have a comment explaining why it is used.
19-
#[derive(Debug, Copy, Clone)]
20-
pub enum StructurallyRelateAliases {
21-
Yes,
22-
No,
23-
}
24-
2513
impl<'tcx> Relate<TyCtxt<'tcx>> for ty::ImplSubject<'tcx> {
2614
#[inline]
2715
fn relate<R: TypeRelation<TyCtxt<'tcx>>>(

compiler/rustc_next_trait_solver/src/delegate.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ pub trait SolverDelegate:
1313
(**self).cx()
1414
}
1515

16-
type Span: Copy;
17-
1816
fn build_with_canonical<V>(
1917
cx: Self::Interner,
2018
solver_mode: SolverMode,
@@ -26,7 +24,7 @@ pub trait SolverDelegate:
2624
fn fresh_var_for_kind_with_span(
2725
&self,
2826
arg: <Self::Interner as Interner>::GenericArg,
29-
span: Self::Span,
27+
span: <Self::Interner as Interner>::Span,
3028
) -> <Self::Interner as Interner>::GenericArg;
3129

3230
// FIXME: Uplift the leak check into this crate.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ where
403403
// `rustc_trait_selection::solve::inspect::analyse`.
404404
pub fn instantiate_canonical_state<D, I, T: TypeFoldable<I>>(
405405
delegate: &D,
406-
span: D::Span,
406+
span: I::Span,
407407
param_env: I::ParamEnv,
408408
orig_values: &mut Vec<I::GenericArg>,
409409
state: inspect::CanonicalState<I, T>,

compiler/rustc_trait_selection/src/solve/delegate.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
4343
self.0.tcx
4444
}
4545

46-
type Span = Span;
47-
4846
fn build_with_canonical<V>(
4947
interner: TyCtxt<'tcx>,
5048
solver_mode: SolverMode,

compiler/rustc_type_ir/src/interner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub trait Interner:
3232
{
3333
type DefId: DefId<Self>;
3434
type LocalDefId: Copy + Debug + Hash + Eq + Into<Self::DefId> + TypeFoldable<Self>;
35+
type Span: Copy;
3536

3637
type GenericArgs: GenericArgs<Self>;
3738
type GenericArgsSlice: Copy + Debug + Hash + Eq + SliceLike<Item = Self::GenericArg>;

compiler/rustc_type_ir/src/relate.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use tracing::{debug, instrument};
66
use crate::error::{ExpectedFound, TypeError};
77
use crate::fold::TypeFoldable;
88
use crate::inherent::*;
9-
use crate::{self as ty, Interner};
9+
use crate::solve::Goal;
10+
use crate::{self as ty, Interner, Upcast};
1011

1112
pub type RelateResult<I, T> = Result<T, TypeError<I>>;
1213

@@ -665,3 +666,39 @@ impl<I: Interner> Relate<I> for ty::TraitPredicate<I> {
665666
})
666667
}
667668
}
669+
670+
/// Whether aliases should be related structurally or not. Used
671+
/// to adjust the behavior of generalization and combine.
672+
///
673+
/// This should always be `No` unless in a few special-cases when
674+
/// instantiating canonical responses and in the new solver. Each
675+
/// such case should have a comment explaining why it is used.
676+
#[derive(Debug, Copy, Clone)]
677+
pub enum StructurallyRelateAliases {
678+
Yes,
679+
No,
680+
}
681+
682+
pub trait PredicateEmittingRelation<I: Interner>: TypeRelation<I> {
683+
fn span(&self) -> I::Span;
684+
685+
fn param_env(&self) -> I::ParamEnv;
686+
687+
/// Whether aliases should be related structurally. This is pretty much
688+
/// always `No` unless you're equating in some specific locations of the
689+
/// new solver. See the comments in these use-cases for more details.
690+
fn structurally_relate_aliases(&self) -> StructurallyRelateAliases;
691+
692+
/// Register obligations that must hold in order for this relation to hold
693+
fn register_goals(&mut self, obligations: impl IntoIterator<Item = Goal<I, I::Predicate>>);
694+
695+
/// Register predicates that must hold in order for this relation to hold.
696+
/// This uses the default `param_env` of the obligation.
697+
fn register_predicates(
698+
&mut self,
699+
obligations: impl IntoIterator<Item: Upcast<I, I::Predicate>>,
700+
);
701+
702+
/// Register `AliasRelate` obligation(s) that both types must be related to each other.
703+
fn register_alias_relate_predicate(&mut self, a: I::Ty, b: I::Ty);
704+
}

0 commit comments

Comments
 (0)