Uplift Goal to rustc_type_ir

This commit is contained in:
Michael Goulet 2024-05-15 21:57:41 -04:00
parent 2684655602
commit 138881b315
22 changed files with 130 additions and 109 deletions

View file

@ -115,7 +115,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
pub(super) fn prove_predicates( pub(super) fn prove_predicates(
&mut self, &mut self,
predicates: impl IntoIterator<Item: Upcast<'tcx, ty::Predicate<'tcx>> + std::fmt::Debug>, predicates: impl IntoIterator<Item: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>> + std::fmt::Debug>,
locations: Locations, locations: Locations,
category: ConstraintCategory<'tcx>, category: ConstraintCategory<'tcx>,
) { ) {
@ -127,7 +127,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
#[instrument(skip(self), level = "debug")] #[instrument(skip(self), level = "debug")]
pub(super) fn prove_predicate( pub(super) fn prove_predicate(
&mut self, &mut self,
predicate: impl Upcast<'tcx, ty::Predicate<'tcx>> + std::fmt::Debug, predicate: impl Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>> + std::fmt::Debug,
locations: Locations, locations: Locations,
category: ConstraintCategory<'tcx>, category: ConstraintCategory<'tcx>,
) { ) {

View file

@ -548,7 +548,7 @@ impl<'bccx, 'tcx> ObligationEmittingRelation<'tcx> for NllTypeRelating<'_, 'bccx
fn register_predicates( fn register_predicates(
&mut self, &mut self,
obligations: impl IntoIterator<Item: ty::Upcast<'tcx, ty::Predicate<'tcx>>>, obligations: impl IntoIterator<Item: ty::Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
) { ) {
self.register_obligations( self.register_obligations(
obligations obligations

View file

@ -1,7 +1,7 @@
//! A utility module to inspect currently ambiguous obligations in the current context. //! A utility module to inspect currently ambiguous obligations in the current context.
use crate::FnCtxt; use crate::FnCtxt;
use rustc_infer::traits::solve::Goal;
use rustc_infer::traits::{self, ObligationCause}; use rustc_infer::traits::{self, ObligationCause};
use rustc_middle::traits::solve::Goal;
use rustc_middle::ty::{self, Ty, TypeVisitableExt}; use rustc_middle::ty::{self, Ty, TypeVisitableExt};
use rustc_span::Span; use rustc_span::Span;
use rustc_trait_selection::solve::inspect::ProofTreeInferCtxtExt; use rustc_trait_selection::solve::inspect::ProofTreeInferCtxtExt;

View file

@ -339,7 +339,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
pub fn register_predicates( pub fn register_predicates(
&mut self, &mut self,
obligations: impl IntoIterator<Item: Upcast<'tcx, ty::Predicate<'tcx>>>, obligations: impl IntoIterator<Item: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
) { ) {
self.obligations.extend(obligations.into_iter().map(|to_pred| { self.obligations.extend(obligations.into_iter().map(|to_pred| {
Obligation::new(self.infcx.tcx, self.trace.cause.clone(), self.param_env, to_pred) Obligation::new(self.infcx.tcx, self.trace.cause.clone(), self.param_env, to_pred)
@ -365,7 +365,7 @@ pub trait ObligationEmittingRelation<'tcx>: TypeRelation<'tcx> {
/// be used if control over the obligation causes is required. /// be used if control over the obligation causes is required.
fn register_predicates( fn register_predicates(
&mut self, &mut self,
obligations: impl IntoIterator<Item: Upcast<'tcx, ty::Predicate<'tcx>>>, obligations: impl IntoIterator<Item: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
); );
/// Register `AliasRelate` obligation(s) that both types must be related to each other. /// Register `AliasRelate` obligation(s) that both types must be related to each other.

View file

@ -142,7 +142,7 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for Glb<'_, '_, 'tcx> {
fn register_predicates( fn register_predicates(
&mut self, &mut self,
obligations: impl IntoIterator<Item: ty::Upcast<'tcx, ty::Predicate<'tcx>>>, obligations: impl IntoIterator<Item: ty::Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
) { ) {
self.fields.register_predicates(obligations); self.fields.register_predicates(obligations);
} }

View file

@ -142,7 +142,7 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for Lub<'_, '_, 'tcx> {
fn register_predicates( fn register_predicates(
&mut self, &mut self,
obligations: impl IntoIterator<Item: ty::Upcast<'tcx, ty::Predicate<'tcx>>>, obligations: impl IntoIterator<Item: ty::Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
) { ) {
self.fields.register_predicates(obligations); self.fields.register_predicates(obligations);
} }

View file

@ -314,7 +314,7 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
fn register_predicates( fn register_predicates(
&mut self, &mut self,
obligations: impl IntoIterator<Item: ty::Upcast<'tcx, ty::Predicate<'tcx>>>, obligations: impl IntoIterator<Item: ty::Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
) { ) {
self.fields.register_predicates(obligations); self.fields.register_predicates(obligations);
} }

View file

@ -78,9 +78,9 @@ impl<T: Hash> Hash for Obligation<'_, T> {
} }
} }
impl<'tcx, P> From<Obligation<'tcx, P>> for solve::Goal<'tcx, P> { impl<'tcx, P> From<Obligation<'tcx, P>> for ty::Goal<'tcx, P> {
fn from(value: Obligation<'tcx, P>) -> Self { fn from(value: Obligation<'tcx, P>) -> Self {
solve::Goal { param_env: value.param_env, predicate: value.predicate } ty::Goal { param_env: value.param_env, predicate: value.predicate }
} }
} }
@ -155,7 +155,7 @@ impl<'tcx, O> Obligation<'tcx, O> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
cause: ObligationCause<'tcx>, cause: ObligationCause<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
predicate: impl Upcast<'tcx, O>, predicate: impl Upcast<TyCtxt<'tcx>, O>,
) -> Obligation<'tcx, O> { ) -> Obligation<'tcx, O> {
Self::with_depth(tcx, cause, 0, param_env, predicate) Self::with_depth(tcx, cause, 0, param_env, predicate)
} }
@ -173,7 +173,7 @@ impl<'tcx, O> Obligation<'tcx, O> {
cause: ObligationCause<'tcx>, cause: ObligationCause<'tcx>,
recursion_depth: usize, recursion_depth: usize,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
predicate: impl Upcast<'tcx, O>, predicate: impl Upcast<TyCtxt<'tcx>, O>,
) -> Obligation<'tcx, O> { ) -> Obligation<'tcx, O> {
let predicate = predicate.upcast(tcx); let predicate = predicate.upcast(tcx);
Obligation { cause, param_env, recursion_depth, predicate } Obligation { cause, param_env, recursion_depth, predicate }
@ -184,12 +184,16 @@ impl<'tcx, O> Obligation<'tcx, O> {
span: Span, span: Span,
body_id: LocalDefId, body_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
trait_ref: impl Upcast<'tcx, O>, trait_ref: impl Upcast<TyCtxt<'tcx>, O>,
) -> Obligation<'tcx, O> { ) -> Obligation<'tcx, O> {
Obligation::new(tcx, ObligationCause::misc(span, body_id), param_env, trait_ref) Obligation::new(tcx, ObligationCause::misc(span, body_id), param_env, trait_ref)
} }
pub fn with<P>(&self, tcx: TyCtxt<'tcx>, value: impl Upcast<'tcx, P>) -> Obligation<'tcx, P> { pub fn with<P>(
&self,
tcx: TyCtxt<'tcx>,
value: impl Upcast<TyCtxt<'tcx>, P>,
) -> Obligation<'tcx, P> {
Obligation::with_depth(tcx, self.cause.clone(), self.recursion_depth, self.param_env, value) Obligation::with_depth(tcx, self.cause.clone(), self.recursion_depth, self.param_env, value)
} }
} }

View file

@ -8,8 +8,9 @@ use crate::traits::query::NoSolution;
use crate::traits::Canonical; use crate::traits::Canonical;
use crate::ty::{ use crate::ty::{
self, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeVisitable, TypeVisitor, self, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeVisitable, TypeVisitor,
Upcast,
}; };
// FIXME(compiler-errors): remove this import in favor of `use rustc_middle::ty::Goal`.
pub use crate::ty::Goal;
use super::BuiltinImplSource; use super::BuiltinImplSource;
@ -18,32 +19,6 @@ pub mod inspect;
pub use cache::{CacheData, EvaluationCache}; pub use cache::{CacheData, EvaluationCache};
/// A goal is a statement, i.e. `predicate`, we want to prove
/// given some assumptions, i.e. `param_env`.
///
/// Most of the time the `param_env` contains the `where`-bounds of the function
/// we're currently typechecking while the `predicate` is some trait bound.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, HashStable, TypeFoldable, TypeVisitable)]
pub struct Goal<'tcx, P> {
pub predicate: P,
pub param_env: ty::ParamEnv<'tcx>,
}
impl<'tcx, P> Goal<'tcx, P> {
pub fn new(
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
predicate: impl Upcast<'tcx, P>,
) -> Goal<'tcx, P> {
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 Upcast<'tcx, Q>) -> Goal<'tcx, Q> {
Goal { param_env: self.param_env, predicate: predicate.upcast(tcx) }
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, HashStable, TypeFoldable, TypeVisitable)] #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, HashStable, TypeFoldable, TypeVisitable)]
pub struct Response<'tcx> { pub struct Response<'tcx> {
pub certainty: Certainty, pub certainty: Certainty,

View file

@ -132,6 +132,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type InferRegion = ty::RegionVid; type InferRegion = ty::RegionVid;
type PlaceholderRegion = ty::PlaceholderRegion; type PlaceholderRegion = ty::PlaceholderRegion;
type ParamEnv = ty::ParamEnv<'tcx>;
type Predicate = Predicate<'tcx>; type Predicate = Predicate<'tcx>;
type TraitPredicate = ty::TraitPredicate<'tcx>; type TraitPredicate = ty::TraitPredicate<'tcx>;
type RegionOutlivesPredicate = ty::RegionOutlivesPredicate<'tcx>; type RegionOutlivesPredicate = ty::RegionOutlivesPredicate<'tcx>;

View file

@ -97,12 +97,13 @@ pub use self::parameterized::ParameterizedOverTcx;
pub use self::pattern::{Pattern, PatternKind}; pub use self::pattern::{Pattern, PatternKind};
pub use self::predicate::{ pub use self::predicate::{
AliasTerm, Clause, ClauseKind, CoercePredicate, ExistentialPredicate, AliasTerm, Clause, ClauseKind, CoercePredicate, ExistentialPredicate,
ExistentialPredicateStableCmpExt, ExistentialProjection, ExistentialTraitRef, NormalizesTo, ExistentialPredicateStableCmpExt, ExistentialProjection, ExistentialTraitRef, Goal,
OutlivesPredicate, PolyCoercePredicate, PolyExistentialPredicate, PolyExistentialProjection, NormalizesTo, OutlivesPredicate, PolyCoercePredicate, PolyExistentialPredicate,
PolyExistentialTraitRef, PolyProjectionPredicate, PolyRegionOutlivesPredicate, PolyExistentialProjection, PolyExistentialTraitRef, PolyProjectionPredicate,
PolySubtypePredicate, PolyTraitPredicate, PolyTraitRef, PolyTypeOutlivesPredicate, Predicate, PolyRegionOutlivesPredicate, PolySubtypePredicate, PolyTraitPredicate, PolyTraitRef,
PredicateKind, ProjectionPredicate, RegionOutlivesPredicate, SubtypePredicate, ToPolyTraitRef, PolyTypeOutlivesPredicate, Predicate, PredicateKind, ProjectionPredicate,
TraitPredicate, TraitRef, TypeOutlivesPredicate, Upcast, RegionOutlivesPredicate, SubtypePredicate, ToPolyTraitRef, TraitPredicate, TraitRef,
TypeOutlivesPredicate,
}; };
pub use self::region::{ pub use self::region::{
BoundRegion, BoundRegionKind, BoundRegionKind::*, EarlyParamRegion, LateParamRegion, Region, BoundRegion, BoundRegionKind, BoundRegionKind::*, EarlyParamRegion, LateParamRegion, Region,

View file

@ -9,9 +9,10 @@ use std::cmp::Ordering;
use crate::ty::{ use crate::ty::{
self, Binder, DebruijnIndex, EarlyBinder, PredicatePolarity, Term, Ty, TyCtxt, TypeFlags, self, Binder, DebruijnIndex, EarlyBinder, PredicatePolarity, Term, Ty, TyCtxt, TypeFlags,
WithCachedTypeInfo, Upcast, UpcastFrom, WithCachedTypeInfo,
}; };
pub type Goal<'tcx, P> = ir::Goal<TyCtxt<'tcx>, P>;
pub type TraitRef<'tcx> = ir::TraitRef<TyCtxt<'tcx>>; pub type TraitRef<'tcx> = ir::TraitRef<TyCtxt<'tcx>>;
pub type AliasTerm<'tcx> = ir::AliasTerm<TyCtxt<'tcx>>; pub type AliasTerm<'tcx> = ir::AliasTerm<TyCtxt<'tcx>>;
pub type ProjectionPredicate<'tcx> = ir::ProjectionPredicate<TyCtxt<'tcx>>; pub type ProjectionPredicate<'tcx> = ir::ProjectionPredicate<TyCtxt<'tcx>>;
@ -544,107 +545,82 @@ impl<'tcx> ToPolyTraitRef<'tcx> for PolyTraitPredicate<'tcx> {
} }
} }
/// An `Into`-like trait that takes `TyCtxt` to perform interner-specific transformations. impl<'tcx> UpcastFrom<TyCtxt<'tcx>, PredicateKind<'tcx>> for Predicate<'tcx> {
pub trait Upcast<'tcx, T> {
fn upcast(self, tcx: TyCtxt<'tcx>) -> T;
}
impl<'tcx, T, U> Upcast<'tcx, U> for T
where
U: UpcastFrom<'tcx, T>,
{
fn upcast(self, tcx: TyCtxt<'tcx>) -> U {
U::upcast_from(self, tcx)
}
}
/// A `From`-like trait that takes `TyCtxt` to perform interner-specific transformations.
pub trait UpcastFrom<'tcx, T> {
fn upcast_from(from: T, tcx: TyCtxt<'tcx>) -> Self;
}
impl<'tcx, T> UpcastFrom<'tcx, T> for T {
fn upcast_from(from: T, _tcx: TyCtxt<'tcx>) -> Self {
from
}
}
impl<'tcx> UpcastFrom<'tcx, PredicateKind<'tcx>> for Predicate<'tcx> {
fn upcast_from(from: PredicateKind<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: PredicateKind<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
ty::Binder::dummy(from).upcast(tcx) ty::Binder::dummy(from).upcast(tcx)
} }
} }
impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, PredicateKind<'tcx>>> for Predicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, Binder<'tcx, PredicateKind<'tcx>>> for Predicate<'tcx> {
fn upcast_from(from: Binder<'tcx, PredicateKind<'tcx>>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: Binder<'tcx, PredicateKind<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
tcx.mk_predicate(from) tcx.mk_predicate(from)
} }
} }
impl<'tcx> UpcastFrom<'tcx, ClauseKind<'tcx>> for Predicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, ClauseKind<'tcx>> for Predicate<'tcx> {
fn upcast_from(from: ClauseKind<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: ClauseKind<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
tcx.mk_predicate(ty::Binder::dummy(PredicateKind::Clause(from))) tcx.mk_predicate(ty::Binder::dummy(PredicateKind::Clause(from)))
} }
} }
impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, ClauseKind<'tcx>>> for Predicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, Binder<'tcx, ClauseKind<'tcx>>> for Predicate<'tcx> {
fn upcast_from(from: Binder<'tcx, ClauseKind<'tcx>>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: Binder<'tcx, ClauseKind<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
tcx.mk_predicate(from.map_bound(PredicateKind::Clause)) tcx.mk_predicate(from.map_bound(PredicateKind::Clause))
} }
} }
impl<'tcx> UpcastFrom<'tcx, Clause<'tcx>> for Predicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, Clause<'tcx>> for Predicate<'tcx> {
fn upcast_from(from: Clause<'tcx>, _tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: Clause<'tcx>, _tcx: TyCtxt<'tcx>) -> Self {
from.as_predicate() from.as_predicate()
} }
} }
impl<'tcx> UpcastFrom<'tcx, ClauseKind<'tcx>> for Clause<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, ClauseKind<'tcx>> for Clause<'tcx> {
fn upcast_from(from: ClauseKind<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: ClauseKind<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
tcx.mk_predicate(Binder::dummy(PredicateKind::Clause(from))).expect_clause() tcx.mk_predicate(Binder::dummy(PredicateKind::Clause(from))).expect_clause()
} }
} }
impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, ClauseKind<'tcx>>> for Clause<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, Binder<'tcx, ClauseKind<'tcx>>> for Clause<'tcx> {
fn upcast_from(from: Binder<'tcx, ClauseKind<'tcx>>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: Binder<'tcx, ClauseKind<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
tcx.mk_predicate(from.map_bound(|clause| PredicateKind::Clause(clause))).expect_clause() tcx.mk_predicate(from.map_bound(|clause| PredicateKind::Clause(clause))).expect_clause()
} }
} }
impl<'tcx> UpcastFrom<'tcx, TraitRef<'tcx>> for Predicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, TraitRef<'tcx>> for Predicate<'tcx> {
fn upcast_from(from: TraitRef<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: TraitRef<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
ty::Binder::dummy(from).upcast(tcx) ty::Binder::dummy(from).upcast(tcx)
} }
} }
impl<'tcx> UpcastFrom<'tcx, TraitRef<'tcx>> for TraitPredicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, TraitRef<'tcx>> for TraitPredicate<'tcx> {
fn upcast_from(from: TraitRef<'tcx>, _tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: TraitRef<'tcx>, _tcx: TyCtxt<'tcx>) -> Self {
TraitPredicate { trait_ref: from, polarity: PredicatePolarity::Positive } TraitPredicate { trait_ref: from, polarity: PredicatePolarity::Positive }
} }
} }
impl<'tcx> UpcastFrom<'tcx, TraitRef<'tcx>> for Clause<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, TraitRef<'tcx>> for Clause<'tcx> {
fn upcast_from(from: TraitRef<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: TraitRef<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
let p: Predicate<'tcx> = from.upcast(tcx); let p: Predicate<'tcx> = from.upcast(tcx);
p.expect_clause() p.expect_clause()
} }
} }
impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, TraitRef<'tcx>>> for Predicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, Binder<'tcx, TraitRef<'tcx>>> for Predicate<'tcx> {
fn upcast_from(from: Binder<'tcx, TraitRef<'tcx>>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: Binder<'tcx, TraitRef<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
let pred: PolyTraitPredicate<'tcx> = from.upcast(tcx); let pred: PolyTraitPredicate<'tcx> = from.upcast(tcx);
pred.upcast(tcx) pred.upcast(tcx)
} }
} }
impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, TraitRef<'tcx>>> for Clause<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, Binder<'tcx, TraitRef<'tcx>>> for Clause<'tcx> {
fn upcast_from(from: Binder<'tcx, TraitRef<'tcx>>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: Binder<'tcx, TraitRef<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
let pred: PolyTraitPredicate<'tcx> = from.upcast(tcx); let pred: PolyTraitPredicate<'tcx> = from.upcast(tcx);
pred.upcast(tcx) pred.upcast(tcx)
} }
} }
impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, TraitRef<'tcx>>> for PolyTraitPredicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, Binder<'tcx, TraitRef<'tcx>>> for PolyTraitPredicate<'tcx> {
fn upcast_from(from: Binder<'tcx, TraitRef<'tcx>>, _tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: Binder<'tcx, TraitRef<'tcx>>, _tcx: TyCtxt<'tcx>) -> Self {
from.map_bound(|trait_ref| TraitPredicate { from.map_bound(|trait_ref| TraitPredicate {
trait_ref, trait_ref,
@ -653,71 +629,73 @@ impl<'tcx> UpcastFrom<'tcx, Binder<'tcx, TraitRef<'tcx>>> for PolyTraitPredicate
} }
} }
impl<'tcx> UpcastFrom<'tcx, TraitPredicate<'tcx>> for Predicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, TraitPredicate<'tcx>> for Predicate<'tcx> {
fn upcast_from(from: TraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: TraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
PredicateKind::Clause(ClauseKind::Trait(from)).upcast(tcx) PredicateKind::Clause(ClauseKind::Trait(from)).upcast(tcx)
} }
} }
impl<'tcx> UpcastFrom<'tcx, PolyTraitPredicate<'tcx>> for Predicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, PolyTraitPredicate<'tcx>> for Predicate<'tcx> {
fn upcast_from(from: PolyTraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: PolyTraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
from.map_bound(|p| PredicateKind::Clause(ClauseKind::Trait(p))).upcast(tcx) from.map_bound(|p| PredicateKind::Clause(ClauseKind::Trait(p))).upcast(tcx)
} }
} }
impl<'tcx> UpcastFrom<'tcx, TraitPredicate<'tcx>> for Clause<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, TraitPredicate<'tcx>> for Clause<'tcx> {
fn upcast_from(from: TraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: TraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
let p: Predicate<'tcx> = from.upcast(tcx); let p: Predicate<'tcx> = from.upcast(tcx);
p.expect_clause() p.expect_clause()
} }
} }
impl<'tcx> UpcastFrom<'tcx, PolyTraitPredicate<'tcx>> for Clause<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, PolyTraitPredicate<'tcx>> for Clause<'tcx> {
fn upcast_from(from: PolyTraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: PolyTraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
let p: Predicate<'tcx> = from.upcast(tcx); let p: Predicate<'tcx> = from.upcast(tcx);
p.expect_clause() p.expect_clause()
} }
} }
impl<'tcx> UpcastFrom<'tcx, PolyRegionOutlivesPredicate<'tcx>> for Predicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, PolyRegionOutlivesPredicate<'tcx>> for Predicate<'tcx> {
fn upcast_from(from: PolyRegionOutlivesPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: PolyRegionOutlivesPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
from.map_bound(|p| PredicateKind::Clause(ClauseKind::RegionOutlives(p))).upcast(tcx) from.map_bound(|p| PredicateKind::Clause(ClauseKind::RegionOutlives(p))).upcast(tcx)
} }
} }
impl<'tcx> UpcastFrom<'tcx, OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>> for Predicate<'tcx> { 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 { fn upcast_from(from: OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>, tcx: TyCtxt<'tcx>) -> Self {
ty::Binder::dummy(PredicateKind::Clause(ClauseKind::TypeOutlives(from))).upcast(tcx) ty::Binder::dummy(PredicateKind::Clause(ClauseKind::TypeOutlives(from))).upcast(tcx)
} }
} }
impl<'tcx> UpcastFrom<'tcx, ProjectionPredicate<'tcx>> for Predicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, ProjectionPredicate<'tcx>> for Predicate<'tcx> {
fn upcast_from(from: ProjectionPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: ProjectionPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
ty::Binder::dummy(PredicateKind::Clause(ClauseKind::Projection(from))).upcast(tcx) ty::Binder::dummy(PredicateKind::Clause(ClauseKind::Projection(from))).upcast(tcx)
} }
} }
impl<'tcx> UpcastFrom<'tcx, PolyProjectionPredicate<'tcx>> for Predicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, PolyProjectionPredicate<'tcx>> for Predicate<'tcx> {
fn upcast_from(from: PolyProjectionPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: PolyProjectionPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
from.map_bound(|p| PredicateKind::Clause(ClauseKind::Projection(p))).upcast(tcx) from.map_bound(|p| PredicateKind::Clause(ClauseKind::Projection(p))).upcast(tcx)
} }
} }
impl<'tcx> UpcastFrom<'tcx, ProjectionPredicate<'tcx>> for Clause<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, ProjectionPredicate<'tcx>> for Clause<'tcx> {
fn upcast_from(from: ProjectionPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: ProjectionPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
let p: Predicate<'tcx> = from.upcast(tcx); let p: Predicate<'tcx> = from.upcast(tcx);
p.expect_clause() p.expect_clause()
} }
} }
impl<'tcx> UpcastFrom<'tcx, PolyProjectionPredicate<'tcx>> for Clause<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, PolyProjectionPredicate<'tcx>> for Clause<'tcx> {
fn upcast_from(from: PolyProjectionPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: PolyProjectionPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
let p: Predicate<'tcx> = from.upcast(tcx); let p: Predicate<'tcx> = from.upcast(tcx);
p.expect_clause() p.expect_clause()
} }
} }
impl<'tcx> UpcastFrom<'tcx, NormalizesTo<'tcx>> for Predicate<'tcx> { impl<'tcx> UpcastFrom<TyCtxt<'tcx>, NormalizesTo<'tcx>> for Predicate<'tcx> {
fn upcast_from(from: NormalizesTo<'tcx>, tcx: TyCtxt<'tcx>) -> Self { fn upcast_from(from: NormalizesTo<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
PredicateKind::NormalizesTo(from).upcast(tcx) PredicateKind::NormalizesTo(from).upcast(tcx)
} }

View file

@ -6,8 +6,9 @@ use rustc_hir::{def_id::DefId, Movability, Mutability};
use rustc_infer::traits::query::NoSolution; use rustc_infer::traits::query::NoSolution;
use rustc_macros::{TypeFoldable, TypeVisitable}; use rustc_macros::{TypeFoldable, TypeVisitable};
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::traits::solve::Goal; use rustc_middle::ty::{
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, Upcast}; self, Goal, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, Upcast,
};
use rustc_span::sym; use rustc_span::sym;
use crate::solve::EvalCtxt; use crate::solve::EvalCtxt;

View file

@ -20,11 +20,11 @@ use rustc_macros::extension;
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::infer::canonical::CanonicalVarInfos; use rustc_middle::infer::canonical::CanonicalVarInfos;
use rustc_middle::traits::solve::{ use rustc_middle::traits::solve::{
CanonicalResponse, Certainty, ExternalConstraintsData, Goal, GoalSource, QueryResult, Response, CanonicalResponse, Certainty, ExternalConstraintsData, GoalSource, QueryResult, Response,
}; };
use rustc_middle::ty::{self, AliasRelationDirection, Ty, TyCtxt, UniverseIndex};
use rustc_middle::ty::{ use rustc_middle::ty::{
CoercePredicate, RegionOutlivesPredicate, SubtypePredicate, TypeOutlivesPredicate, self, AliasRelationDirection, CoercePredicate, Goal, RegionOutlivesPredicate, SubtypePredicate,
Ty, TyCtxt, TypeOutlivesPredicate, UniverseIndex,
}; };
mod alias_relate; mod alias_relate;

View file

@ -2703,7 +2703,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
obligated_types: &mut Vec<Ty<'tcx>>, obligated_types: &mut Vec<Ty<'tcx>>,
seen_requirements: &mut FxHashSet<DefId>, seen_requirements: &mut FxHashSet<DefId>,
) where ) where
T: Upcast<'tcx, ty::Predicate<'tcx>>, T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
{ {
let mut long_ty_file = None; let mut long_ty_file = None;

View file

@ -302,7 +302,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
suggest_increasing_limit: bool, suggest_increasing_limit: bool,
) -> ! ) -> !
where where
T: Upcast<'tcx, ty::Predicate<'tcx>> + Clone, T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>> + Clone,
{ {
let predicate = obligation.predicate.clone().upcast(self.tcx); let predicate = obligation.predicate.clone().upcast(self.tcx);
let predicate = self.resolve_vars_if_possible(predicate); let predicate = self.resolve_vars_if_possible(predicate);

View file

@ -142,7 +142,7 @@ pub fn type_known_to_meet_bound_modulo_regions<'tcx>(
fn pred_known_to_hold_modulo_regions<'tcx>( fn pred_known_to_hold_modulo_regions<'tcx>(
infcx: &InferCtxt<'tcx>, infcx: &InferCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
pred: impl Upcast<'tcx, ty::Predicate<'tcx>>, pred: impl Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
) -> bool { ) -> bool {
let obligation = Obligation::new(infcx.tcx, ObligationCause::dummy(), param_env, pred); let obligation = Obligation::new(infcx.tcx, ObligationCause::dummy(), param_env, pred);

View file

@ -1378,7 +1378,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
error_obligation: &Obligation<'tcx, T>, error_obligation: &Obligation<'tcx, T>,
) -> Result<(), OverflowError> ) -> Result<(), OverflowError>
where where
T: Upcast<'tcx, ty::Predicate<'tcx>> + Clone, T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>> + Clone,
{ {
if !self.infcx.tcx.recursion_limit().value_within_limit(depth) { if !self.infcx.tcx.recursion_limit().value_within_limit(depth) {
match self.query_mode { match self.query_mode {
@ -1407,7 +1407,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
error_obligation: &Obligation<'tcx, V>, error_obligation: &Obligation<'tcx, V>,
) -> Result<(), OverflowError> ) -> Result<(), OverflowError>
where where
V: Upcast<'tcx, ty::Predicate<'tcx>> + Clone, V: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>> + Clone,
{ {
self.check_recursion_depth(obligation.recursion_depth, error_obligation) self.check_recursion_depth(obligation.recursion_depth, error_obligation)
} }

View file

@ -79,6 +79,7 @@ pub trait Interner:
type PlaceholderRegion: PlaceholderLike; type PlaceholderRegion: PlaceholderLike;
// Predicates // Predicates
type ParamEnv: Copy + Debug + Hash + Eq;
type Predicate: Predicate<Self>; type Predicate: Predicate<Self>;
type TraitPredicate: Copy + Debug + Hash + Eq; type TraitPredicate: Copy + Debug + Hash + Eq;
type RegionOutlivesPredicate: Copy + Debug + Hash + Eq; type RegionOutlivesPredicate: Copy + Debug + Hash + Eq;

View file

@ -40,6 +40,7 @@ mod interner;
mod predicate; mod predicate;
mod predicate_kind; mod predicate_kind;
mod region_kind; mod region_kind;
mod upcast;
pub use canonical::*; pub use canonical::*;
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
@ -54,6 +55,7 @@ pub use predicate_kind::*;
pub use region_kind::*; pub use region_kind::*;
pub use ty_info::*; pub use ty_info::*;
pub use ty_kind::*; pub use ty_kind::*;
pub use upcast::*;
pub use AliasTyKind::*; pub use AliasTyKind::*;
pub use DynKind::*; pub use DynKind::*;
pub use InferTy::*; pub use InferTy::*;

View file

@ -1,4 +1,5 @@
use std::fmt; use std::fmt;
use std::hash::Hash;
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
use rustc_macros::{Decodable, Encodable, HashStable_NoContext, TyDecodable, TyEncodable}; use rustc_macros::{Decodable, Encodable, HashStable_NoContext, TyDecodable, TyEncodable};
@ -7,9 +8,42 @@ use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Gen
use crate::inherent::*; use crate::inherent::*;
use crate::visit::TypeVisitableExt as _; use crate::visit::TypeVisitableExt as _;
use crate::{ use crate::{
AliasTy, AliasTyKind, DebugWithInfcx, InferCtxtLike, Interner, UnevaluatedConst, WithInfcx, AliasTy, AliasTyKind, DebugWithInfcx, InferCtxtLike, Interner, UnevaluatedConst, Upcast,
WithInfcx,
}; };
/// A goal is a statement, i.e. `predicate`, we want to prove
/// given some assumptions, i.e. `param_env`.
///
/// Most of the time the `param_env` contains the `where`-bounds of the function
/// we're currently typechecking while the `predicate` is some trait bound.
#[derive(derivative::Derivative)]
#[derivative(
Clone(bound = "P: Clone"),
Copy(bound = "P: Copy"),
Hash(bound = "P: Hash"),
PartialEq(bound = "P: PartialEq"),
Eq(bound = "P: Eq"),
Debug(bound = "P: fmt::Debug")
)]
#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
pub struct Goal<I: Interner, P> {
pub param_env: I::ParamEnv,
pub predicate: P,
}
impl<I: Interner, P> Goal<I, P> {
pub fn new(tcx: I, param_env: I::ParamEnv, predicate: impl Upcast<I, P>) -> Goal<I, P> {
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: I, predicate: impl Upcast<I, Q>) -> Goal<I, Q> {
Goal { param_env: self.param_env, predicate: predicate.upcast(tcx) }
}
}
/// A complete reference to a trait. These take numerous guises in syntax, /// A complete reference to a trait. These take numerous guises in syntax,
/// but perhaps the most recognizable form is in a where-clause: /// but perhaps the most recognizable form is in a where-clause:
/// ```ignore (illustrative) /// ```ignore (illustrative)

View file

@ -0,0 +1,24 @@
/// An `Into`-like trait that takes `TyCtxt` to perform interner-specific transformations.
pub trait Upcast<I, T> {
fn upcast(self, interner: I) -> T;
}
impl<I, T, U> Upcast<I, U> for T
where
U: UpcastFrom<I, T>,
{
fn upcast(self, interner: I) -> U {
U::upcast_from(self, interner)
}
}
/// A `From`-like trait that takes `TyCtxt` to perform interner-specific transformations.
pub trait UpcastFrom<I, T> {
fn upcast_from(from: T, interner: I) -> Self;
}
impl<I, T> UpcastFrom<I, T> for T {
fn upcast_from(from: T, _tcx: I) -> Self {
from
}
}