Uplift OutlivesPredicate, remove a bunch of unnecessary associated types from Interner

This commit is contained in:
Michael Goulet 2024-05-21 11:07:54 -04:00
parent 28ce588321
commit 1c8230ea3c
15 changed files with 79 additions and 75 deletions

View File

@ -136,7 +136,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
fn convert(
&mut self,
predicate: ty::OutlivesPredicate<ty::GenericArg<'tcx>, ty::Region<'tcx>>,
predicate: ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>,
constraint_category: ConstraintCategory<'tcx>,
) {
debug!("generate: constraints at: {:#?}", self.locations);
@ -276,7 +276,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
&self,
ty: Ty<'tcx>,
next_outlives_predicates: &mut Vec<(
ty::OutlivesPredicate<ty::GenericArg<'tcx>, ty::Region<'tcx>>,
ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>,
ConstraintCategory<'tcx>,
)>,
) -> Ty<'tcx> {

View File

@ -9,7 +9,7 @@ use smallvec::smallvec;
/// Tracks the `T: 'a` or `'a: 'a` predicates that we have inferred
/// must be added to the struct header.
pub(crate) type RequiredPredicates<'tcx> =
FxIndexMap<ty::OutlivesPredicate<GenericArg<'tcx>, ty::Region<'tcx>>, Span>;
FxIndexMap<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>, Span>;
/// Given a requirement `T: 'a` or `'b: 'a`, deduce the
/// outlives_component and add it to `required_predicates`

View File

@ -64,8 +64,7 @@ struct OutlivesEnvironmentBuilder<'tcx> {
/// "Region-bound pairs" tracks outlives relations that are known to
/// be true, either because of explicit where-clauses like `T: 'a` or
/// because of implied bounds.
pub type RegionBoundPairs<'tcx> =
FxIndexSet<ty::OutlivesPredicate<GenericKind<'tcx>, Region<'tcx>>>;
pub type RegionBoundPairs<'tcx> = FxIndexSet<ty::OutlivesPredicate<'tcx, GenericKind<'tcx>>>;
impl<'tcx> OutlivesEnvironment<'tcx> {
/// Create a builder using `ParamEnv` and add explicit outlives bounds into it.

View File

@ -94,7 +94,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
pub fn approx_declared_bounds_from_env(
&self,
alias_ty: ty::AliasTy<'tcx>,
) -> Vec<ty::Binder<'tcx, ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>>> {
) -> Vec<ty::PolyTypeOutlivesPredicate<'tcx>> {
let erased_alias_ty = self.tcx.erase_regions(alias_ty.to_ty(self.tcx));
self.declared_generic_bounds_from_env_for_erased_ty(erased_alias_ty)
}
@ -193,7 +193,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
fn declared_generic_bounds_from_env(
&self,
generic_ty: Ty<'tcx>,
) -> Vec<ty::Binder<'tcx, ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>>> {
) -> Vec<ty::PolyTypeOutlivesPredicate<'tcx>> {
assert!(matches!(generic_ty.kind(), ty::Param(_) | ty::Placeholder(_)));
self.declared_generic_bounds_from_env_for_erased_ty(generic_ty)
}
@ -213,7 +213,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
fn declared_generic_bounds_from_env_for_erased_ty(
&self,
erased_ty: Ty<'tcx>,
) -> Vec<ty::Binder<'tcx, ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>>> {
) -> Vec<ty::PolyTypeOutlivesPredicate<'tcx>> {
let tcx = self.tcx;
// To start, collect bounds from user environment. Note that

View File

@ -32,7 +32,7 @@ use std::collections::hash_map::Entry;
use crate::infer::MemberConstraint;
use crate::mir::ConstraintCategory;
use crate::ty::GenericArg;
use crate::ty::{self, List, Region, Ty, TyCtxt, TypeFlags, TypeVisitableExt};
use crate::ty::{self, List, Ty, TyCtxt, TypeFlags, TypeVisitableExt};
pub type Canonical<'tcx, V> = ir::Canonical<TyCtxt<'tcx>, V>;
pub type CanonicalVarInfo<'tcx> = ir::CanonicalVarInfo<TyCtxt<'tcx>>;
@ -141,7 +141,7 @@ impl<'tcx, R> QueryResponse<'tcx, R> {
}
pub type QueryOutlivesConstraint<'tcx> =
(ty::OutlivesPredicate<GenericArg<'tcx>, Region<'tcx>>, ConstraintCategory<'tcx>);
(ty::OutlivesPredicate<'tcx, GenericArg<'tcx>>, ConstraintCategory<'tcx>);
TrivialTypeTraversalImpls! {
crate::infer::canonical::Certainty,

View File

@ -121,7 +121,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type Abi = abi::Abi;
type Const = ty::Const<'tcx>;
type AliasConst = ty::UnevaluatedConst<'tcx>;
type PlaceholderConst = ty::PlaceholderConst;
type ParamConst = ty::ParamConst;
type BoundConst = ty::BoundVar;
@ -137,15 +136,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type ParamEnv = ty::ParamEnv<'tcx>;
type Predicate = Predicate<'tcx>;
type Clause = Clause<'tcx>;
type TraitPredicate = ty::TraitPredicate<'tcx>;
type RegionOutlivesPredicate = ty::RegionOutlivesPredicate<'tcx>;
type TypeOutlivesPredicate = ty::TypeOutlivesPredicate<'tcx>;
type ProjectionPredicate = ty::ProjectionPredicate<'tcx>;
type NormalizesTo = ty::NormalizesTo<'tcx>;
type SubtypePredicate = ty::SubtypePredicate<'tcx>;
type CoercePredicate = ty::CoercePredicate<'tcx>;
type ClosureKind = ty::ClosureKind;
type Clauses = ty::Clauses<'tcx>;

View File

@ -1,9 +1,7 @@
use rustc_data_structures::captures::Captures;
use rustc_data_structures::intern::Interned;
use rustc_hir::def_id::DefId;
use rustc_macros::{
extension, HashStable, Lift, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable,
};
use rustc_macros::{extension, HashStable};
use rustc_type_ir as ir;
use std::cmp::Ordering;
@ -24,6 +22,15 @@ pub type PredicateKind<'tcx> = ir::PredicateKind<TyCtxt<'tcx>>;
pub type NormalizesTo<'tcx> = ir::NormalizesTo<TyCtxt<'tcx>>;
pub type CoercePredicate<'tcx> = ir::CoercePredicate<TyCtxt<'tcx>>;
pub type SubtypePredicate<'tcx> = ir::SubtypePredicate<TyCtxt<'tcx>>;
pub type OutlivesPredicate<'tcx, T> = ir::OutlivesPredicate<TyCtxt<'tcx>, T>;
pub type RegionOutlivesPredicate<'tcx> = OutlivesPredicate<'tcx, ty::Region<'tcx>>;
pub type TypeOutlivesPredicate<'tcx> = OutlivesPredicate<'tcx, Ty<'tcx>>;
pub type PolyTraitPredicate<'tcx> = ty::Binder<'tcx, TraitPredicate<'tcx>>;
pub type PolyRegionOutlivesPredicate<'tcx> = ty::Binder<'tcx, RegionOutlivesPredicate<'tcx>>;
pub type PolyTypeOutlivesPredicate<'tcx> = ty::Binder<'tcx, TypeOutlivesPredicate<'tcx>>;
pub type PolySubtypePredicate<'tcx> = ty::Binder<'tcx, SubtypePredicate<'tcx>>;
pub type PolyCoercePredicate<'tcx> = ty::Binder<'tcx, CoercePredicate<'tcx>>;
pub type PolyProjectionPredicate<'tcx> = ty::Binder<'tcx, ProjectionPredicate<'tcx>>;
/// A statement that can be proven by a trait solver. This includes things that may
/// show up in where clauses, such as trait predicates and projection predicates,
@ -405,20 +412,6 @@ impl<'tcx> Clause<'tcx> {
}
}
pub type PolyTraitPredicate<'tcx> = ty::Binder<'tcx, TraitPredicate<'tcx>>;
/// `A: B`
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
pub struct OutlivesPredicate<A, B>(pub A, pub B);
pub type RegionOutlivesPredicate<'tcx> = OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>>;
pub type TypeOutlivesPredicate<'tcx> = OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>;
pub type PolyRegionOutlivesPredicate<'tcx> = ty::Binder<'tcx, RegionOutlivesPredicate<'tcx>>;
pub type PolyTypeOutlivesPredicate<'tcx> = ty::Binder<'tcx, TypeOutlivesPredicate<'tcx>>;
pub type PolySubtypePredicate<'tcx> = ty::Binder<'tcx, SubtypePredicate<'tcx>>;
pub type PolyCoercePredicate<'tcx> = ty::Binder<'tcx, CoercePredicate<'tcx>>;
pub type PolyProjectionPredicate<'tcx> = Binder<'tcx, ProjectionPredicate<'tcx>>;
pub trait ToPolyTraitRef<'tcx> {
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx>;
}
@ -545,10 +538,8 @@ impl<'tcx> UpcastFrom<TyCtxt<'tcx>, PolyRegionOutlivesPredicate<'tcx>> for Predi
}
}
impl<'tcx> UpcastFrom<TyCtxt<'tcx>, OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>>
for Predicate<'tcx>
{
fn upcast_from(from: OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
impl<'tcx> UpcastFrom<TyCtxt<'tcx>, TypeOutlivesPredicate<'tcx>> for Predicate<'tcx> {
fn upcast_from(from: TypeOutlivesPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
ty::Binder::dummy(PredicateKind::Clause(ClauseKind::TypeOutlives(from))).upcast(tcx)
}
}

View File

@ -2860,10 +2860,9 @@ where
}
}
impl<'tcx, T, U, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::OutlivesPredicate<T, U>
impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::OutlivesPredicate<'tcx, T>
where
T: Print<'tcx, P>,
U: Print<'tcx, P>,
{
fn print(&self, cx: &mut P) -> Result<(), PrintError> {
define_scoped_cx!(cx);
@ -3016,10 +3015,7 @@ forward_display_to_print! {
ty::Region<'tcx>,
Ty<'tcx>,
&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
ty::Const<'tcx>,
ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>,
ty::OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>>
ty::Const<'tcx>
}
define_print! {

View File

@ -707,12 +707,11 @@ impl<'tcx> Stable<'tcx> for ty::TraitPredicate<'tcx> {
}
}
impl<'tcx, A, B, U, V> Stable<'tcx> for ty::OutlivesPredicate<A, B>
impl<'tcx, T> Stable<'tcx> for ty::OutlivesPredicate<'tcx, T>
where
A: Stable<'tcx, T = U>,
B: Stable<'tcx, T = V>,
T: Stable<'tcx>,
{
type T = stable_mir::ty::OutlivesPredicate<U, V>;
type T = stable_mir::ty::OutlivesPredicate<T::T, Region>;
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
let ty::OutlivesPredicate(a, b) = self;

View File

@ -162,8 +162,7 @@ pub fn compute_implied_outlives_bounds_compat_inner<'tcx>(
let mut checked_wf_args = rustc_data_structures::fx::FxHashSet::default();
let mut wf_args = vec![ty.into()];
let mut outlives_bounds: Vec<ty::OutlivesPredicate<ty::GenericArg<'tcx>, ty::Region<'tcx>>> =
vec![];
let mut outlives_bounds: Vec<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>> = vec![];
while let Some(arg) = wf_args.pop() {
if !checked_wf_args.insert(arg) {

View File

@ -5,7 +5,7 @@ use rustc_macros::{HashStable_NoContext, TyDecodable, TyEncodable};
use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic};
use std::fmt;
use crate::{DebruijnIndex, DebugWithInfcx, InferCtxtLike, Interner, WithInfcx};
use crate::{self as ty, DebruijnIndex, DebugWithInfcx, InferCtxtLike, Interner, WithInfcx};
use self::ConstKind::*;
@ -29,7 +29,7 @@ pub enum ConstKind<I: Interner> {
/// An unnormalized const item such as an anon const or assoc const or free const item.
/// Right now anything other than anon consts does not actually work properly but this
/// should
Unevaluated(I::AliasConst),
Unevaluated(ty::UnevaluatedConst<I>),
/// Used to hold computed value.
Value(I::ValueConst),

View File

@ -76,7 +76,6 @@ pub trait Interner:
// Kinds of consts
type Const: Const<Self>;
type AliasConst: Copy + DebugWithInfcx<Self> + Hash + Eq;
type PlaceholderConst: PlaceholderLike;
type ParamConst: Copy + Debug + Hash + Eq;
type BoundConst: Copy + Debug + Hash + Eq + BoundVarLike<Self>;
@ -94,14 +93,6 @@ pub trait Interner:
type ParamEnv: Copy + Debug + Hash + Eq;
type Predicate: Predicate<Self>;
type Clause: Clause<Self>;
type TraitPredicate: Copy + Debug + Hash + Eq + TypeVisitable<Self>;
type RegionOutlivesPredicate: Copy + Debug + Hash + Eq + TypeVisitable<Self>;
type TypeOutlivesPredicate: Copy + Debug + Hash + Eq + TypeVisitable<Self>;
type ProjectionPredicate: Copy + Debug + Hash + Eq + TypeVisitable<Self>;
type NormalizesTo: Copy + Debug + Hash + Eq + TypeVisitable<Self>;
type SubtypePredicate: Copy + Debug + Hash + Eq + TypeVisitable<Self>;
type CoercePredicate: Copy + Debug + Hash + Eq + TypeVisitable<Self>;
type ClosureKind: Copy + Debug + Hash + Eq + TypeVisitable<Self>;
type Clauses: Copy + Debug + Hash + Eq + TypeSuperVisitable<Self> + Flags;
fn mk_canonical_var_infos(self, infos: &[CanonicalVarInfo<Self>]) -> Self::CanonicalVars;

View File

@ -2,7 +2,8 @@ use std::fmt;
use crate::{
AliasTerm, AliasTy, Binder, CoercePredicate, ExistentialProjection, ExistentialTraitRef, FnSig,
Interner, NormalizesTo, ProjectionPredicate, SubtypePredicate, TraitPredicate, TraitRef,
Interner, NormalizesTo, OutlivesPredicate, ProjectionPredicate, SubtypePredicate,
TraitPredicate, TraitRef,
};
pub trait IrPrint<T> {
@ -58,3 +59,12 @@ define_display_via_print!(
);
define_debug_via_print!(TraitRef, ExistentialTraitRef, ExistentialProjection);
impl<I: Interner, T> fmt::Display for OutlivesPredicate<I, T>
where
I: IrPrint<OutlivesPredicate<I, T>>,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
<I as IrPrint<OutlivesPredicate<I, T>>>::print(self, fmt)
}
}

View File

@ -6,10 +6,39 @@ use rustc_macros::{Decodable, Encodable, HashStable_NoContext, TyDecodable, TyEn
use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic};
use crate::inherent::*;
use crate::lift::Lift;
use crate::upcast::Upcast;
use crate::visit::TypeVisitableExt as _;
use crate::{self as ty, DebugWithInfcx, InferCtxtLike, Interner, WithInfcx};
/// `A: 'region`
#[derive(derivative::Derivative)]
#[derivative(
Clone(bound = "A: Clone"),
Copy(bound = "A: Copy"),
Hash(bound = "A: Hash"),
PartialEq(bound = "A: PartialEq"),
Eq(bound = "A: Eq"),
Debug(bound = "A: fmt::Debug")
)]
#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
pub struct OutlivesPredicate<I: Interner, A>(pub A, pub I::Region);
// FIXME: We manually derive `Lift` because the `derive(Lift_Generic)` doesn't
// understand how to turn `A` to `A::Lifted` in the output `type Lifted`.
impl<I: Interner, U: Interner, A> Lift<U> for OutlivesPredicate<I, A>
where
A: Lift<U>,
I::Region: Lift<U, Lifted = U::Region>,
{
type Lifted = OutlivesPredicate<U, A::Lifted>;
fn lift_to_tcx(self, tcx: U) -> Option<Self::Lifted> {
Some(OutlivesPredicate(self.0.lift_to_tcx(tcx)?, self.1.lift_to_tcx(tcx)?))
}
}
/// A complete reference to a trait. These take numerous guises in syntax,
/// but perhaps the most recognizable form is in a where-clause:
/// ```ignore (illustrative)

View File

@ -3,7 +3,7 @@ use rustc_macros::{Decodable, Encodable, HashStable_NoContext, TyDecodable, TyEn
use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic};
use std::fmt;
use crate::Interner;
use crate::{self as ty, Interner};
/// A clause is something that can appear in where bounds or be inferred
/// by implied bounds.
@ -15,17 +15,17 @@ pub enum ClauseKind<I: Interner> {
/// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be
/// the `Self` type of the trait reference and `A`, `B`, and `C`
/// would be the type parameters.
Trait(I::TraitPredicate),
Trait(ty::TraitPredicate<I>),
/// `where 'a: 'b`
RegionOutlives(I::RegionOutlivesPredicate),
/// `where 'a: 'r`
RegionOutlives(ty::OutlivesPredicate<I, I::Region>),
/// `where T: 'a`
TypeOutlives(I::TypeOutlivesPredicate),
/// `where T: 'r`
TypeOutlives(ty::OutlivesPredicate<I, I::Ty>),
/// `where <T as TraitRef>::Name == X`, approximately.
/// See the `ProjectionPredicate` struct for details.
Projection(I::ProjectionPredicate),
Projection(ty::ProjectionPredicate<I>),
/// Ensures that a const generic argument to a parameter `const N: u8`
/// is of type `u8`.
@ -75,7 +75,7 @@ pub enum PredicateKind<I: Interner> {
/// This obligation is created most often when we have two
/// unresolved type variables and hence don't have enough
/// information to process the subtyping obligation yet.
Subtype(I::SubtypePredicate),
Subtype(ty::SubtypePredicate<I>),
/// `T1` coerced to `T2`
///
@ -85,7 +85,7 @@ pub enum PredicateKind<I: Interner> {
/// obligation yet. At the moment, we actually process coercions
/// very much like subtyping and don't handle the full coercion
/// logic.
Coerce(I::CoercePredicate),
Coerce(ty::CoercePredicate<I>),
/// Constants must be equal. The first component is the const that is expected.
ConstEquate(I::Const, I::Const),
@ -102,7 +102,7 @@ pub enum PredicateKind<I: Interner> {
/// `T as Trait>::Assoc`, `Projection(<T as Trait>::Assoc, ?x)` constrains `?x`
/// to `<T as Trait>::Assoc` while `NormalizesTo(<T as Trait>::Assoc, ?x)`
/// results in `NoSolution`.
NormalizesTo(I::NormalizesTo),
NormalizesTo(ty::NormalizesTo<I>),
/// Separate from `ClauseKind::Projection` which is used for normalization in new solver.
/// This predicate requires two terms to be equal to eachother.