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(
&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,
category: ConstraintCategory<'tcx>,
) {
@ -127,7 +127,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
#[instrument(skip(self), level = "debug")]
pub(super) fn prove_predicate(
&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,
category: ConstraintCategory<'tcx>,
) {

View File

@ -548,7 +548,7 @@ impl<'bccx, 'tcx> ObligationEmittingRelation<'tcx> for NllTypeRelating<'_, 'bccx
fn register_predicates(
&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(
obligations

View File

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

View File

@ -339,7 +339,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
pub fn register_predicates(
&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| {
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.
fn register_predicates(
&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.

View File

@ -142,7 +142,7 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for Glb<'_, '_, 'tcx> {
fn register_predicates(
&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);
}

View File

@ -142,7 +142,7 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for Lub<'_, '_, 'tcx> {
fn register_predicates(
&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);
}

View File

@ -314,7 +314,7 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
fn register_predicates(
&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);
}

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 {
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>,
cause: ObligationCause<'tcx>,
param_env: ty::ParamEnv<'tcx>,
predicate: impl Upcast<'tcx, O>,
predicate: impl Upcast<TyCtxt<'tcx>, O>,
) -> Obligation<'tcx, O> {
Self::with_depth(tcx, cause, 0, param_env, predicate)
}
@ -173,7 +173,7 @@ impl<'tcx, O> Obligation<'tcx, O> {
cause: ObligationCause<'tcx>,
recursion_depth: usize,
param_env: ty::ParamEnv<'tcx>,
predicate: impl Upcast<'tcx, O>,
predicate: impl Upcast<TyCtxt<'tcx>, O>,
) -> Obligation<'tcx, O> {
let predicate = predicate.upcast(tcx);
Obligation { cause, param_env, recursion_depth, predicate }
@ -184,12 +184,16 @@ impl<'tcx, O> Obligation<'tcx, O> {
span: Span,
body_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
trait_ref: impl Upcast<'tcx, O>,
trait_ref: impl Upcast<TyCtxt<'tcx>, O>,
) -> Obligation<'tcx, O> {
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)
}
}

View File

@ -8,8 +8,9 @@ use crate::traits::query::NoSolution;
use crate::traits::Canonical;
use crate::ty::{
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;
@ -18,32 +19,6 @@ pub mod inspect;
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)]
pub struct Response<'tcx> {
pub certainty: Certainty,

View File

@ -132,6 +132,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type InferRegion = ty::RegionVid;
type PlaceholderRegion = ty::PlaceholderRegion;
type ParamEnv = ty::ParamEnv<'tcx>;
type Predicate = Predicate<'tcx>;
type TraitPredicate = ty::TraitPredicate<'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::predicate::{
AliasTerm, Clause, ClauseKind, CoercePredicate, ExistentialPredicate,
ExistentialPredicateStableCmpExt, ExistentialProjection, ExistentialTraitRef, NormalizesTo,
OutlivesPredicate, PolyCoercePredicate, PolyExistentialPredicate, PolyExistentialProjection,
PolyExistentialTraitRef, PolyProjectionPredicate, PolyRegionOutlivesPredicate,
PolySubtypePredicate, PolyTraitPredicate, PolyTraitRef, PolyTypeOutlivesPredicate, Predicate,
PredicateKind, ProjectionPredicate, RegionOutlivesPredicate, SubtypePredicate, ToPolyTraitRef,
TraitPredicate, TraitRef, TypeOutlivesPredicate, Upcast,
ExistentialPredicateStableCmpExt, ExistentialProjection, ExistentialTraitRef, Goal,
NormalizesTo, OutlivesPredicate, PolyCoercePredicate, PolyExistentialPredicate,
PolyExistentialProjection, PolyExistentialTraitRef, PolyProjectionPredicate,
PolyRegionOutlivesPredicate, PolySubtypePredicate, PolyTraitPredicate, PolyTraitRef,
PolyTypeOutlivesPredicate, Predicate, PredicateKind, ProjectionPredicate,
RegionOutlivesPredicate, SubtypePredicate, ToPolyTraitRef, TraitPredicate, TraitRef,
TypeOutlivesPredicate,
};
pub use self::region::{
BoundRegion, BoundRegionKind, BoundRegionKind::*, EarlyParamRegion, LateParamRegion, Region,

View File

@ -9,9 +9,10 @@ use std::cmp::Ordering;
use crate::ty::{
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 AliasTerm<'tcx> = ir::AliasTerm<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.
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> {
impl<'tcx> UpcastFrom<TyCtxt<'tcx>, PredicateKind<'tcx>> for Predicate<'tcx> {
fn upcast_from(from: PredicateKind<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
let p: Predicate<'tcx> = from.upcast(tcx);
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 {
let pred: PolyTraitPredicate<'tcx> = from.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 {
let pred: PolyTraitPredicate<'tcx> = from.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 {
from.map_bound(|trait_ref| TraitPredicate {
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 {
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 {
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 {
let p: Predicate<'tcx> = from.upcast(tcx);
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 {
let p: Predicate<'tcx> = from.upcast(tcx);
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 {
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 {
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 {
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 {
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 {
let p: Predicate<'tcx> = from.upcast(tcx);
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 {
let p: Predicate<'tcx> = from.upcast(tcx);
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 {
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_macros::{TypeFoldable, TypeVisitable};
use rustc_middle::bug;
use rustc_middle::traits::solve::Goal;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, Upcast};
use rustc_middle::ty::{
self, Goal, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, Upcast,
};
use rustc_span::sym;
use crate::solve::EvalCtxt;

View File

@ -20,11 +20,11 @@ use rustc_macros::extension;
use rustc_middle::bug;
use rustc_middle::infer::canonical::CanonicalVarInfos;
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::{
CoercePredicate, RegionOutlivesPredicate, SubtypePredicate, TypeOutlivesPredicate,
self, AliasRelationDirection, CoercePredicate, Goal, RegionOutlivesPredicate, SubtypePredicate,
Ty, TyCtxt, TypeOutlivesPredicate, UniverseIndex,
};
mod alias_relate;

View File

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

View File

@ -302,7 +302,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
suggest_increasing_limit: bool,
) -> !
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 = 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>(
infcx: &InferCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
pred: impl Upcast<'tcx, ty::Predicate<'tcx>>,
pred: impl Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
) -> bool {
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>,
) -> Result<(), OverflowError>
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) {
match self.query_mode {
@ -1407,7 +1407,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
error_obligation: &Obligation<'tcx, V>,
) -> Result<(), OverflowError>
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)
}

View File

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

View File

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

View File

@ -1,4 +1,5 @@
use std::fmt;
use std::hash::Hash;
#[cfg(feature = "nightly")]
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::visit::TypeVisitableExt as _;
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,
/// but perhaps the most recognizable form is in a where-clause:
/// ```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
}
}