1
Fork 0

Make impls UpcastFrom, implement Upcast for UpcastFrom

This commit is contained in:
Michael Goulet 2024-05-15 10:25:59 -04:00
parent 412dc28d6a
commit 2684655602
2 changed files with 92 additions and 90 deletions

View file

@ -544,180 +544,182 @@ impl<'tcx> ToPolyTraitRef<'tcx> for PolyTraitPredicate<'tcx> {
} }
} }
/// An `Into`-like trait that takes `TyCtxt` to perform interner-specific transformations.
pub trait Upcast<'tcx, T> { pub trait Upcast<'tcx, T> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> T; fn upcast(self, tcx: TyCtxt<'tcx>) -> T;
} }
impl<'tcx, T> Upcast<'tcx, T> for T { impl<'tcx, T, U> Upcast<'tcx, U> for T
fn upcast(self, _tcx: TyCtxt<'tcx>) -> T { where
self U: UpcastFrom<'tcx, T>,
{
fn upcast(self, tcx: TyCtxt<'tcx>) -> U {
U::upcast_from(self, tcx)
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for PredicateKind<'tcx> { /// A `From`-like trait that takes `TyCtxt` to perform interner-specific transformations.
#[inline(always)] pub trait UpcastFrom<'tcx, T> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { fn upcast_from(from: T, tcx: TyCtxt<'tcx>) -> Self;
ty::Binder::dummy(self).upcast(tcx) }
impl<'tcx, T> UpcastFrom<'tcx, T> for T {
fn upcast_from(from: T, _tcx: TyCtxt<'tcx>) -> Self {
from
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for Binder<'tcx, PredicateKind<'tcx>> { impl<'tcx> UpcastFrom<'tcx, PredicateKind<'tcx>> for Predicate<'tcx> {
#[inline(always)] fn upcast_from(from: PredicateKind<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { ty::Binder::dummy(from).upcast(tcx)
tcx.mk_predicate(self)
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for ClauseKind<'tcx> { impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, PredicateKind<'tcx>>> for Predicate<'tcx> {
#[inline(always)] fn upcast_from(from: Binder<'tcx, PredicateKind<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { tcx.mk_predicate(from)
tcx.mk_predicate(ty::Binder::dummy(ty::PredicateKind::Clause(self)))
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for Binder<'tcx, ClauseKind<'tcx>> { impl<'tcx> UpcastFrom<'tcx, ClauseKind<'tcx>> for Predicate<'tcx> {
#[inline(always)] fn upcast_from(from: ClauseKind<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { tcx.mk_predicate(ty::Binder::dummy(PredicateKind::Clause(from)))
tcx.mk_predicate(self.map_bound(ty::PredicateKind::Clause))
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for Clause<'tcx> { impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, ClauseKind<'tcx>>> for Predicate<'tcx> {
#[inline(always)] fn upcast_from(from: Binder<'tcx, ClauseKind<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
fn upcast(self, _tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { tcx.mk_predicate(from.map_bound(PredicateKind::Clause))
self.as_predicate()
} }
} }
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for ClauseKind<'tcx> { impl<'tcx> UpcastFrom<'tcx, Clause<'tcx>> for Predicate<'tcx> {
#[inline(always)] fn upcast_from(from: Clause<'tcx>, _tcx: TyCtxt<'tcx>) -> Self {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> { from.as_predicate()
tcx.mk_predicate(Binder::dummy(ty::PredicateKind::Clause(self))).expect_clause()
} }
} }
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for Binder<'tcx, ClauseKind<'tcx>> { impl<'tcx> UpcastFrom<'tcx, ClauseKind<'tcx>> for Clause<'tcx> {
#[inline(always)] fn upcast_from(from: ClauseKind<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> { tcx.mk_predicate(Binder::dummy(PredicateKind::Clause(from))).expect_clause()
tcx.mk_predicate(self.map_bound(|clause| ty::PredicateKind::Clause(clause))).expect_clause()
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for TraitRef<'tcx> { impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, ClauseKind<'tcx>>> for Clause<'tcx> {
#[inline(always)] fn upcast_from(from: Binder<'tcx, ClauseKind<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { tcx.mk_predicate(from.map_bound(|clause| PredicateKind::Clause(clause))).expect_clause()
ty::Binder::dummy(self).upcast(tcx)
} }
} }
impl<'tcx> Upcast<'tcx, TraitPredicate<'tcx>> for TraitRef<'tcx> { impl<'tcx> UpcastFrom<'tcx, TraitRef<'tcx>> for Predicate<'tcx> {
#[inline(always)] fn upcast_from(from: TraitRef<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
fn upcast(self, _tcx: TyCtxt<'tcx>) -> TraitPredicate<'tcx> { ty::Binder::dummy(from).upcast(tcx)
TraitPredicate { trait_ref: self, polarity: PredicatePolarity::Positive }
} }
} }
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for TraitRef<'tcx> { impl<'tcx> UpcastFrom<'tcx, TraitRef<'tcx>> for TraitPredicate<'tcx> {
#[inline(always)] fn upcast_from(from: TraitRef<'tcx>, _tcx: TyCtxt<'tcx>) -> Self {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> { TraitPredicate { trait_ref: from, polarity: PredicatePolarity::Positive }
let p: Predicate<'tcx> = self.upcast(tcx); }
}
impl<'tcx> UpcastFrom<'tcx, TraitRef<'tcx>> for Clause<'tcx> {
fn upcast_from(from: TraitRef<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
let p: Predicate<'tcx> = from.upcast(tcx);
p.expect_clause() p.expect_clause()
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for Binder<'tcx, TraitRef<'tcx>> { impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, TraitRef<'tcx>>> for Predicate<'tcx> {
#[inline(always)] fn upcast_from(from: Binder<'tcx, TraitRef<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { let pred: PolyTraitPredicate<'tcx> = from.upcast(tcx);
let pred: PolyTraitPredicate<'tcx> = self.upcast(tcx);
pred.upcast(tcx) pred.upcast(tcx)
} }
} }
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for Binder<'tcx, TraitRef<'tcx>> { impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, TraitRef<'tcx>>> for Clause<'tcx> {
#[inline(always)] fn upcast_from(from: Binder<'tcx, TraitRef<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> { let pred: PolyTraitPredicate<'tcx> = from.upcast(tcx);
let pred: PolyTraitPredicate<'tcx> = self.upcast(tcx);
pred.upcast(tcx) pred.upcast(tcx)
} }
} }
impl<'tcx> Upcast<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef<'tcx>> { impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, TraitRef<'tcx>>> for PolyTraitPredicate<'tcx> {
#[inline(always)] fn upcast_from(from: Binder<'tcx, TraitRef<'tcx>>, _tcx: TyCtxt<'tcx>) -> Self {
fn upcast(self, _: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> { from.map_bound(|trait_ref| TraitPredicate {
self.map_bound(|trait_ref| TraitPredicate {
trait_ref, trait_ref,
polarity: ty::PredicatePolarity::Positive, polarity: PredicatePolarity::Positive,
}) })
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for TraitPredicate<'tcx> { impl<'tcx> UpcastFrom<'tcx, TraitPredicate<'tcx>> for Predicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { fn upcast_from(from: TraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
PredicateKind::Clause(ClauseKind::Trait(self)).upcast(tcx) PredicateKind::Clause(ClauseKind::Trait(from)).upcast(tcx)
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for PolyTraitPredicate<'tcx> { impl<'tcx> UpcastFrom<'tcx, PolyTraitPredicate<'tcx>> for Predicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { fn upcast_from(from: PolyTraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
self.map_bound(|p| PredicateKind::Clause(ClauseKind::Trait(p))).upcast(tcx) from.map_bound(|p| PredicateKind::Clause(ClauseKind::Trait(p))).upcast(tcx)
} }
} }
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for TraitPredicate<'tcx> { impl<'tcx> UpcastFrom<'tcx, TraitPredicate<'tcx>> for Clause<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> { fn upcast_from(from: TraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
let p: Predicate<'tcx> = self.upcast(tcx); let p: Predicate<'tcx> = from.upcast(tcx);
p.expect_clause() p.expect_clause()
} }
} }
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for PolyTraitPredicate<'tcx> { impl<'tcx> UpcastFrom<'tcx, PolyTraitPredicate<'tcx>> for Clause<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> { fn upcast_from(from: PolyTraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
let p: Predicate<'tcx> = self.upcast(tcx); let p: Predicate<'tcx> = from.upcast(tcx);
p.expect_clause() p.expect_clause()
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for PolyRegionOutlivesPredicate<'tcx> { impl<'tcx> UpcastFrom<'tcx, PolyRegionOutlivesPredicate<'tcx>> for Predicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { fn upcast_from(from: PolyRegionOutlivesPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
self.map_bound(|p| PredicateKind::Clause(ClauseKind::RegionOutlives(p))).upcast(tcx) from.map_bound(|p| PredicateKind::Clause(ClauseKind::RegionOutlives(p))).upcast(tcx)
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> { impl<'tcx> UpcastFrom<'tcx, OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>> for Predicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { fn upcast_from(from: OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
ty::Binder::dummy(PredicateKind::Clause(ClauseKind::TypeOutlives(self))).upcast(tcx) ty::Binder::dummy(PredicateKind::Clause(ClauseKind::TypeOutlives(from))).upcast(tcx)
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for ProjectionPredicate<'tcx> { impl<'tcx> UpcastFrom<'tcx, ProjectionPredicate<'tcx>> for Predicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { fn upcast_from(from: ProjectionPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
ty::Binder::dummy(PredicateKind::Clause(ClauseKind::Projection(self))).upcast(tcx) ty::Binder::dummy(PredicateKind::Clause(ClauseKind::Projection(from))).upcast(tcx)
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for PolyProjectionPredicate<'tcx> { impl<'tcx> UpcastFrom<'tcx, PolyProjectionPredicate<'tcx>> for Predicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { fn upcast_from(from: PolyProjectionPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
self.map_bound(|p| PredicateKind::Clause(ClauseKind::Projection(p))).upcast(tcx) from.map_bound(|p| PredicateKind::Clause(ClauseKind::Projection(p))).upcast(tcx)
} }
} }
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for ProjectionPredicate<'tcx> { impl<'tcx> UpcastFrom<'tcx, ProjectionPredicate<'tcx>> for Clause<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> { fn upcast_from(from: ProjectionPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
let p: Predicate<'tcx> = self.upcast(tcx); let p: Predicate<'tcx> = from.upcast(tcx);
p.expect_clause() p.expect_clause()
} }
} }
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for PolyProjectionPredicate<'tcx> { impl<'tcx> UpcastFrom<'tcx, PolyProjectionPredicate<'tcx>> for Clause<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> { fn upcast_from(from: PolyProjectionPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
let p: Predicate<'tcx> = self.upcast(tcx); let p: Predicate<'tcx> = from.upcast(tcx);
p.expect_clause() p.expect_clause()
} }
} }
impl<'tcx> Upcast<'tcx, ty::Predicate<'tcx>> for NormalizesTo<'tcx> { impl<'tcx> UpcastFrom<'tcx, NormalizesTo<'tcx>> for Predicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { fn upcast_from(from: NormalizesTo<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
PredicateKind::NormalizesTo(self).upcast(tcx) PredicateKind::NormalizesTo(from).upcast(tcx)
} }
} }

View file

@ -286,7 +286,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
return self.forced_ambiguity(MaybeCause::Ambiguity).into_iter().collect(); return self.forced_ambiguity(MaybeCause::Ambiguity).into_iter().collect();
} }
let goal = let goal: Goal<'tcx, G> =
goal.with(self.tcx(), goal.predicate.with_self_ty(self.tcx(), normalized_self_ty)); goal.with(self.tcx(), goal.predicate.with_self_ty(self.tcx(), normalized_self_ty));
// Vars that show up in the rest of the goal substs may have been constrained by // Vars that show up in the rest of the goal substs may have been constrained by
// normalizing the self type as well, since type variables are not uniquified. // normalizing the self type as well, since type variables are not uniquified.