1
Fork 0

Uplift NormalizesTo, CoercePredicate, and SubtypePredicate

This commit is contained in:
Michael Goulet 2024-05-11 13:51:25 -04:00
parent 0d4dca2b82
commit 0a8f33830c
10 changed files with 128 additions and 87 deletions

View file

@ -149,7 +149,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
fn mk_args(self, args: &[Self::GenericArg]) -> Self::GenericArgs {
self.mk_args(args)
}
fn check_and_mk_args(
self,
def_id: DefId,
@ -157,7 +157,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
) -> ty::GenericArgsRef<'tcx> {
self.check_and_mk_args(def_id, args)
}
fn parent(self, def_id: Self::DefId) -> Self::DefId {
self.parent(def_id)
}

View file

@ -3,17 +3,20 @@ use rustc_data_structures::intern::Interned;
use rustc_hir::def_id::DefId;
use rustc_macros::{HashStable, Lift, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
use rustc_type_ir::ClauseKind as IrClauseKind;
use rustc_type_ir::CoercePredicate as IrCoercePredicate;
use rustc_type_ir::ExistentialProjection as IrExistentialProjection;
use rustc_type_ir::ExistentialTraitRef as IrExistentialTraitRef;
use rustc_type_ir::NormalizesTo as IrNormalizesTo;
use rustc_type_ir::PredicateKind as IrPredicateKind;
use rustc_type_ir::ProjectionPredicate as IrProjectionPredicate;
use rustc_type_ir::SubtypePredicate as IrSubtypePredicate;
use rustc_type_ir::TraitPredicate as IrTraitPredicate;
use rustc_type_ir::TraitRef as IrTraitRef;
use rustc_type_ir::ProjectionPredicate as IrProjectionPredicate;
use rustc_type_ir::ExistentialTraitRef as IrExistentialTraitRef;
use rustc_type_ir::ExistentialProjection as IrExistentialProjection;
use std::cmp::Ordering;
use crate::ty::{
self, AliasTy, Binder, DebruijnIndex, DebugWithInfcx, EarlyBinder,
PredicatePolarity, Term, Ty, TyCtxt, TypeFlags, WithCachedTypeInfo,
self, Binder, DebruijnIndex, DebugWithInfcx, EarlyBinder, PredicatePolarity, Term, Ty, TyCtxt,
TypeFlags, WithCachedTypeInfo,
};
pub type TraitRef<'tcx> = IrTraitRef<TyCtxt<'tcx>>;
@ -23,6 +26,9 @@ pub type ExistentialProjection<'tcx> = IrExistentialProjection<TyCtxt<'tcx>>;
pub type TraitPredicate<'tcx> = IrTraitPredicate<TyCtxt<'tcx>>;
pub type ClauseKind<'tcx> = IrClauseKind<TyCtxt<'tcx>>;
pub type PredicateKind<'tcx> = IrPredicateKind<TyCtxt<'tcx>>;
pub type NormalizesTo<'tcx> = IrNormalizesTo<TyCtxt<'tcx>>;
pub type CoercePredicate<'tcx> = IrCoercePredicate<TyCtxt<'tcx>>;
pub type SubtypePredicate<'tcx> = IrSubtypePredicate<TyCtxt<'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,
@ -511,25 +517,8 @@ pub type TypeOutlivesPredicate<'tcx> = OutlivesPredicate<Ty<'tcx>, ty::Region<'t
pub type PolyRegionOutlivesPredicate<'tcx> = ty::Binder<'tcx, RegionOutlivesPredicate<'tcx>>;
pub type PolyTypeOutlivesPredicate<'tcx> = ty::Binder<'tcx, TypeOutlivesPredicate<'tcx>>;
/// Encodes that `a` must be a subtype of `b`. The `a_is_expected` flag indicates
/// whether the `a` type is the type that we should label as "expected" when
/// presenting user diagnostics.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
pub struct SubtypePredicate<'tcx> {
pub a_is_expected: bool,
pub a: Ty<'tcx>,
pub b: Ty<'tcx>,
}
pub type PolySubtypePredicate<'tcx> = ty::Binder<'tcx, SubtypePredicate<'tcx>>;
/// Encodes that we have to coerce *from* the `a` type to the `b` type.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
pub struct CoercePredicate<'tcx> {
pub a: Ty<'tcx>,
pub b: Ty<'tcx>,
}
pub type PolyCoercePredicate<'tcx> = ty::Binder<'tcx, CoercePredicate<'tcx>>;
pub type PolyProjectionPredicate<'tcx> = Binder<'tcx, ProjectionPredicate<'tcx>>;
@ -568,33 +557,6 @@ impl<'tcx> PolyProjectionPredicate<'tcx> {
}
}
/// Used by the new solver. Unlike a `ProjectionPredicate` this can only be
/// proven by actually normalizing `alias`.
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
pub struct NormalizesTo<'tcx> {
pub alias: AliasTy<'tcx>,
pub term: Term<'tcx>,
}
impl<'tcx> NormalizesTo<'tcx> {
pub fn self_ty(self) -> Ty<'tcx> {
self.alias.self_ty()
}
pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> NormalizesTo<'tcx> {
Self { alias: self.alias.with_self_ty(tcx, self_ty), ..self }
}
pub fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId {
self.alias.trait_def_id(tcx)
}
pub fn def_id(self) -> DefId {
self.alias.def_id
}
}
pub trait ToPolyTraitRef<'tcx> {
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx>;
}

View file

@ -3105,6 +3105,24 @@ define_print! {
cx.reset_type_limit();
p!(print(self.term))
}
ty::SubtypePredicate<'tcx> {
p!(print(self.a), " <: ");
cx.reset_type_limit();
p!(print(self.b))
}
ty::CoercePredicate<'tcx> {
p!(print(self.a), " -> ");
cx.reset_type_limit();
p!(print(self.b))
}
ty::NormalizesTo<'tcx> {
p!(print(self.alias), " normalizes-to ");
cx.reset_type_limit();
p!(print(self.term))
}
}
define_print_and_forward_display! {
@ -3180,24 +3198,6 @@ define_print_and_forward_display! {
p!(write("{}", self.name))
}
ty::SubtypePredicate<'tcx> {
p!(print(self.a), " <: ");
cx.reset_type_limit();
p!(print(self.b))
}
ty::CoercePredicate<'tcx> {
p!(print(self.a), " -> ");
cx.reset_type_limit();
p!(print(self.b))
}
ty::NormalizesTo<'tcx> {
p!(print(self.alias), " normalizes-to ");
cx.reset_type_limit();
p!(print(self.term))
}
ty::Term<'tcx> {
match self.unpack() {
ty::TermKind::Ty(ty) => p!(print(ty)),

View file

@ -152,12 +152,6 @@ impl fmt::Debug for ty::ParamConst {
}
}
impl<'tcx> fmt::Debug for ty::NormalizesTo<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "NormalizesTo({:?}, {:?})", self.alias, self.term)
}
}
impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.kind())