Rename ToPredicate for Upcast

This commit is contained in:
Michael Goulet
2024-05-15 10:05:25 -04:00
parent 97bf25c8cf
commit 11ec3eca74
44 changed files with 214 additions and 220 deletions

View File

@@ -7,8 +7,8 @@ use crate::infer::canonical::{CanonicalVarValues, QueryRegionConstraints};
use crate::traits::query::NoSolution;
use crate::traits::Canonical;
use crate::ty::{
self, FallibleTypeFolder, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeVisitable,
TypeVisitor,
self, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeVisitable, TypeVisitor,
Upcast,
};
use super::BuiltinImplSource;
@@ -33,14 +33,14 @@ impl<'tcx, P> Goal<'tcx, P> {
pub fn new(
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
predicate: impl ToPredicate<'tcx, P>,
predicate: impl Upcast<'tcx, P>,
) -> Goal<'tcx, P> {
Goal { param_env, predicate: predicate.to_predicate(tcx) }
Goal { param_env, predicate: predicate.upcast(tcx) }
}
/// Updates the goal to one with a different `predicate` but the same `param_env`.
pub fn with<Q>(self, tcx: TyCtxt<'tcx>, predicate: impl ToPredicate<'tcx, Q>) -> Goal<'tcx, Q> {
Goal { param_env: self.param_env, predicate: predicate.to_predicate(tcx) }
pub fn with<Q>(self, tcx: TyCtxt<'tcx>, predicate: impl Upcast<'tcx, Q>) -> Goal<'tcx, Q> {
Goal { param_env: self.param_env, predicate: predicate.upcast(tcx) }
}
}

View File

@@ -1,6 +1,6 @@
use rustc_data_structures::fx::FxHashSet;
use crate::ty::{Clause, PolyTraitRef, ToPolyTraitRef, ToPredicate, TyCtxt};
use crate::ty::{Clause, PolyTraitRef, ToPolyTraitRef, TyCtxt, Upcast};
/// Given a [`PolyTraitRef`], get the [`Clause`]s implied by the trait's definition.
///
@@ -11,7 +11,7 @@ pub fn super_predicates_for_pretty_printing<'tcx>(
tcx: TyCtxt<'tcx>,
trait_ref: PolyTraitRef<'tcx>,
) -> impl Iterator<Item = Clause<'tcx>> {
let clause = trait_ref.to_predicate(tcx);
let clause = trait_ref.upcast(tcx);
Elaborator { tcx, visited: FxHashSet::from_iter([clause]), stack: vec![clause] }
}

View File

@@ -102,7 +102,7 @@ pub use self::predicate::{
PolyExistentialTraitRef, PolyProjectionPredicate, PolyRegionOutlivesPredicate,
PolySubtypePredicate, PolyTraitPredicate, PolyTraitRef, PolyTypeOutlivesPredicate, Predicate,
PredicateKind, ProjectionPredicate, RegionOutlivesPredicate, SubtypePredicate, ToPolyTraitRef,
ToPredicate, TraitPredicate, TraitRef, TypeOutlivesPredicate,
TraitPredicate, TraitRef, TypeOutlivesPredicate, Upcast,
};
pub use self::region::{
BoundRegion, BoundRegionKind, BoundRegionKind::*, EarlyParamRegion, LateParamRegion, Region,

View File

@@ -234,10 +234,10 @@ impl<'tcx> PolyExistentialPredicate<'tcx> {
pub fn with_self_ty(&self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> ty::Clause<'tcx> {
match self.skip_binder() {
ExistentialPredicate::Trait(tr) => {
self.rebind(tr).with_self_ty(tcx, self_ty).to_predicate(tcx)
self.rebind(tr).with_self_ty(tcx, self_ty).upcast(tcx)
}
ExistentialPredicate::Projection(p) => {
self.rebind(p.with_self_ty(tcx, self_ty)).to_predicate(tcx)
self.rebind(p.with_self_ty(tcx, self_ty)).upcast(tcx)
}
ExistentialPredicate::AutoTrait(did) => {
let generics = tcx.generics_of(did);
@@ -249,7 +249,7 @@ impl<'tcx> PolyExistentialPredicate<'tcx> {
let err_args = ty::GenericArgs::extend_with_error(tcx, did, &[self_ty.into()]);
ty::TraitRef::new(tcx, did, err_args)
};
self.rebind(trait_ref).to_predicate(tcx)
self.rebind(trait_ref).upcast(tcx)
}
}
}
@@ -544,106 +544,106 @@ impl<'tcx> ToPolyTraitRef<'tcx> for PolyTraitPredicate<'tcx> {
}
}
pub trait ToPredicate<'tcx, P = Predicate<'tcx>> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> P;
pub trait Upcast<'tcx, P = Predicate<'tcx>> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> P;
}
impl<'tcx, T> ToPredicate<'tcx, T> for T {
fn to_predicate(self, _tcx: TyCtxt<'tcx>) -> T {
impl<'tcx, T> Upcast<'tcx, T> for T {
fn upcast(self, _tcx: TyCtxt<'tcx>) -> T {
self
}
}
impl<'tcx> ToPredicate<'tcx> for PredicateKind<'tcx> {
impl<'tcx> Upcast<'tcx> for PredicateKind<'tcx> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
ty::Binder::dummy(self).to_predicate(tcx)
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
ty::Binder::dummy(self).upcast(tcx)
}
}
impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, PredicateKind<'tcx>> {
impl<'tcx> Upcast<'tcx> for Binder<'tcx, PredicateKind<'tcx>> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
tcx.mk_predicate(self)
}
}
impl<'tcx> ToPredicate<'tcx> for ClauseKind<'tcx> {
impl<'tcx> Upcast<'tcx> for ClauseKind<'tcx> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
tcx.mk_predicate(ty::Binder::dummy(ty::PredicateKind::Clause(self)))
}
}
impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, ClauseKind<'tcx>> {
impl<'tcx> Upcast<'tcx> for Binder<'tcx, ClauseKind<'tcx>> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
tcx.mk_predicate(self.map_bound(ty::PredicateKind::Clause))
}
}
impl<'tcx> ToPredicate<'tcx> for Clause<'tcx> {
impl<'tcx> Upcast<'tcx> for Clause<'tcx> {
#[inline(always)]
fn to_predicate(self, _tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
fn upcast(self, _tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
self.as_predicate()
}
}
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for ClauseKind<'tcx> {
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for ClauseKind<'tcx> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
tcx.mk_predicate(Binder::dummy(ty::PredicateKind::Clause(self))).expect_clause()
}
}
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for Binder<'tcx, ClauseKind<'tcx>> {
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for Binder<'tcx, ClauseKind<'tcx>> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
tcx.mk_predicate(self.map_bound(|clause| ty::PredicateKind::Clause(clause))).expect_clause()
}
}
impl<'tcx> ToPredicate<'tcx> for TraitRef<'tcx> {
impl<'tcx> Upcast<'tcx> for TraitRef<'tcx> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
ty::Binder::dummy(self).to_predicate(tcx)
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
ty::Binder::dummy(self).upcast(tcx)
}
}
impl<'tcx> ToPredicate<'tcx, TraitPredicate<'tcx>> for TraitRef<'tcx> {
impl<'tcx> Upcast<'tcx, TraitPredicate<'tcx>> for TraitRef<'tcx> {
#[inline(always)]
fn to_predicate(self, _tcx: TyCtxt<'tcx>) -> TraitPredicate<'tcx> {
fn upcast(self, _tcx: TyCtxt<'tcx>) -> TraitPredicate<'tcx> {
TraitPredicate { trait_ref: self, polarity: PredicatePolarity::Positive }
}
}
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for TraitRef<'tcx> {
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for TraitRef<'tcx> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
let p: Predicate<'tcx> = self.to_predicate(tcx);
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
let p: Predicate<'tcx> = self.upcast(tcx);
p.expect_clause()
}
}
impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, TraitRef<'tcx>> {
impl<'tcx> Upcast<'tcx> for Binder<'tcx, TraitRef<'tcx>> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
let pred: PolyTraitPredicate<'tcx> = self.to_predicate(tcx);
pred.to_predicate(tcx)
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
let pred: PolyTraitPredicate<'tcx> = self.upcast(tcx);
pred.upcast(tcx)
}
}
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for Binder<'tcx, TraitRef<'tcx>> {
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for Binder<'tcx, TraitRef<'tcx>> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
let pred: PolyTraitPredicate<'tcx> = self.to_predicate(tcx);
pred.to_predicate(tcx)
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
let pred: PolyTraitPredicate<'tcx> = self.upcast(tcx);
pred.upcast(tcx)
}
}
impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef<'tcx>> {
impl<'tcx> Upcast<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef<'tcx>> {
#[inline(always)]
fn to_predicate(self, _: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> {
fn upcast(self, _: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> {
self.map_bound(|trait_ref| TraitPredicate {
trait_ref,
polarity: ty::PredicatePolarity::Positive,
@@ -651,73 +651,73 @@ impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef
}
}
impl<'tcx> ToPredicate<'tcx> for TraitPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
PredicateKind::Clause(ClauseKind::Trait(self)).to_predicate(tcx)
impl<'tcx> Upcast<'tcx> for TraitPredicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
PredicateKind::Clause(ClauseKind::Trait(self)).upcast(tcx)
}
}
impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
self.map_bound(|p| PredicateKind::Clause(ClauseKind::Trait(p))).to_predicate(tcx)
impl<'tcx> Upcast<'tcx> for PolyTraitPredicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
self.map_bound(|p| PredicateKind::Clause(ClauseKind::Trait(p))).upcast(tcx)
}
}
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for TraitPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
let p: Predicate<'tcx> = self.to_predicate(tcx);
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for TraitPredicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
let p: Predicate<'tcx> = self.upcast(tcx);
p.expect_clause()
}
}
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for PolyTraitPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
let p: Predicate<'tcx> = self.to_predicate(tcx);
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for PolyTraitPredicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
let p: Predicate<'tcx> = self.upcast(tcx);
p.expect_clause()
}
}
impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
self.map_bound(|p| PredicateKind::Clause(ClauseKind::RegionOutlives(p))).to_predicate(tcx)
impl<'tcx> Upcast<'tcx> for PolyRegionOutlivesPredicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
self.map_bound(|p| PredicateKind::Clause(ClauseKind::RegionOutlives(p))).upcast(tcx)
}
}
impl<'tcx> ToPredicate<'tcx> for OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
ty::Binder::dummy(PredicateKind::Clause(ClauseKind::TypeOutlives(self))).to_predicate(tcx)
impl<'tcx> Upcast<'tcx> for OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
ty::Binder::dummy(PredicateKind::Clause(ClauseKind::TypeOutlives(self))).upcast(tcx)
}
}
impl<'tcx> ToPredicate<'tcx> for ProjectionPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
ty::Binder::dummy(PredicateKind::Clause(ClauseKind::Projection(self))).to_predicate(tcx)
impl<'tcx> Upcast<'tcx> for ProjectionPredicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
ty::Binder::dummy(PredicateKind::Clause(ClauseKind::Projection(self))).upcast(tcx)
}
}
impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
self.map_bound(|p| PredicateKind::Clause(ClauseKind::Projection(p))).to_predicate(tcx)
impl<'tcx> Upcast<'tcx> for PolyProjectionPredicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
self.map_bound(|p| PredicateKind::Clause(ClauseKind::Projection(p))).upcast(tcx)
}
}
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for ProjectionPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
let p: Predicate<'tcx> = self.to_predicate(tcx);
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for ProjectionPredicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
let p: Predicate<'tcx> = self.upcast(tcx);
p.expect_clause()
}
}
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for PolyProjectionPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
let p: Predicate<'tcx> = self.to_predicate(tcx);
impl<'tcx> Upcast<'tcx, Clause<'tcx>> for PolyProjectionPredicate<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
let p: Predicate<'tcx> = self.upcast(tcx);
p.expect_clause()
}
}
impl<'tcx> ToPredicate<'tcx> for NormalizesTo<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
PredicateKind::NormalizesTo(self).to_predicate(tcx)
impl<'tcx> Upcast<'tcx> for NormalizesTo<'tcx> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
PredicateKind::NormalizesTo(self).upcast(tcx)
}
}

View File

@@ -4,8 +4,8 @@ use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use crate::query::{IntoQueryParam, Providers};
use crate::ty::layout::{FloatExt, IntegerExt};
use crate::ty::{
self, FallibleTypeFolder, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
TypeVisitableExt,
self, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
TypeVisitableExt, Upcast,
};
use crate::ty::{GenericArgKind, GenericArgsRef};
use rustc_apfloat::Float as _;
@@ -1094,7 +1094,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for OpaqueTypeExpander<'tcx> {
// anything that requires `ParamEnv::with_reveal_all_normalized`.
term: projection_pred.term,
})
.to_predicate(self.tcx)
.upcast(self.tcx)
} else {
p.super_fold_with(self)
}