Uplift AliasTy
This commit is contained in:
parent
34582118af
commit
1ad28a6f53
15 changed files with 584 additions and 534 deletions
|
@ -7,8 +7,7 @@ use rustc_hir as hir;
|
|||
use rustc_hir::def::{DefKind, Res};
|
||||
use rustc_hir::def_id::LocalDefId;
|
||||
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
|
||||
use rustc_type_ir::ConstKind as IrConstKind;
|
||||
use rustc_type_ir::{TypeFlags, WithCachedTypeInfo};
|
||||
use rustc_type_ir::{self as ir, TypeFlags, WithCachedTypeInfo};
|
||||
|
||||
mod int;
|
||||
mod kind;
|
||||
|
@ -20,7 +19,8 @@ use rustc_span::Span;
|
|||
use rustc_span::DUMMY_SP;
|
||||
pub use valtree::*;
|
||||
|
||||
pub type ConstKind<'tcx> = IrConstKind<TyCtxt<'tcx>>;
|
||||
pub type ConstKind<'tcx> = ir::ConstKind<TyCtxt<'tcx>>;
|
||||
pub type UnevaluatedConst<'tcx> = ir::UnevaluatedConst<TyCtxt<'tcx>>;
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
rustc_data_structures::static_assert_size!(ConstKind<'_>, 32);
|
||||
|
@ -184,9 +184,21 @@ impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
|
|||
Const::new_bound(tcx, debruijn, var, ty)
|
||||
}
|
||||
|
||||
fn new_unevaluated(
|
||||
interner: TyCtxt<'tcx>,
|
||||
uv: ty::UnevaluatedConst<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
) -> Self {
|
||||
Const::new_unevaluated(interner, uv, ty)
|
||||
}
|
||||
|
||||
fn ty(self) -> Ty<'tcx> {
|
||||
self.ty()
|
||||
}
|
||||
|
||||
fn into_term(self) -> ty::Term<'tcx> {
|
||||
self.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Const<'tcx> {
|
||||
|
|
|
@ -1,30 +1,15 @@
|
|||
use super::Const;
|
||||
use crate::mir;
|
||||
use crate::ty::abstract_const::CastKind;
|
||||
use crate::ty::GenericArgsRef;
|
||||
use crate::ty::{self, visit::TypeVisitableExt as _, List, Ty, TyCtxt};
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
|
||||
use rustc_macros::{extension, HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
|
||||
|
||||
/// An unevaluated (potentially generic) constant used in the type-system.
|
||||
#[derive(Copy, Clone, Eq, PartialEq, TyEncodable, TyDecodable)]
|
||||
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
|
||||
pub struct UnevaluatedConst<'tcx> {
|
||||
pub def: DefId,
|
||||
pub args: GenericArgsRef<'tcx>,
|
||||
}
|
||||
|
||||
impl rustc_errors::IntoDiagArg for UnevaluatedConst<'_> {
|
||||
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
|
||||
format!("{self:?}").into_diag_arg()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> UnevaluatedConst<'tcx> {
|
||||
#[extension(pub(crate) trait UnevaluatedConstEvalExt<'tcx>)]
|
||||
impl<'tcx> ty::UnevaluatedConst<'tcx> {
|
||||
/// FIXME(RalfJung): I cannot explain what this does or why it makes sense, but not doing this
|
||||
/// hurts performance.
|
||||
#[inline]
|
||||
pub(crate) fn prepare_for_eval(
|
||||
fn prepare_for_eval(
|
||||
self,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
|
@ -55,13 +40,6 @@ impl<'tcx> UnevaluatedConst<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> UnevaluatedConst<'tcx> {
|
||||
#[inline]
|
||||
pub fn new(def: DefId, args: GenericArgsRef<'tcx>) -> UnevaluatedConst<'tcx> {
|
||||
UnevaluatedConst { def, args }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
|
||||
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
|
||||
pub enum Expr<'tcx> {
|
||||
|
|
|
@ -76,6 +76,7 @@ use rustc_type_ir::TyKind::*;
|
|||
use rustc_type_ir::WithCachedTypeInfo;
|
||||
use rustc_type_ir::{CollectAndApply, Interner, TypeFlags};
|
||||
|
||||
use std::assert_matches::assert_matches;
|
||||
use std::borrow::Borrow;
|
||||
use std::cmp::Ordering;
|
||||
use std::fmt;
|
||||
|
@ -91,67 +92,124 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
|
|||
type DefiningOpaqueTypes = &'tcx ty::List<LocalDefId>;
|
||||
type AdtDef = ty::AdtDef<'tcx>;
|
||||
type GenericArgs = ty::GenericArgsRef<'tcx>;
|
||||
type GenericArgsSlice = &'tcx [ty::GenericArg<'tcx>];
|
||||
type GenericArg = ty::GenericArg<'tcx>;
|
||||
type Term = ty::Term<'tcx>;
|
||||
|
||||
type Term = ty::Term<'tcx>;
|
||||
type Binder<T: TypeVisitable<TyCtxt<'tcx>>> = Binder<'tcx, T>;
|
||||
type BoundVars = &'tcx List<ty::BoundVariableKind>;
|
||||
type BoundVar = ty::BoundVariableKind;
|
||||
type CanonicalVars = CanonicalVarInfos<'tcx>;
|
||||
|
||||
type CanonicalVars = CanonicalVarInfos<'tcx>;
|
||||
type Ty = Ty<'tcx>;
|
||||
type Tys = &'tcx List<Ty<'tcx>>;
|
||||
type AliasTy = ty::AliasTy<'tcx>;
|
||||
type ParamTy = ParamTy;
|
||||
type BoundTy = ty::BoundTy;
|
||||
type PlaceholderTy = ty::PlaceholderType;
|
||||
type ErrorGuaranteed = ErrorGuaranteed;
|
||||
|
||||
type ErrorGuaranteed = ErrorGuaranteed;
|
||||
type BoundExistentialPredicates = &'tcx List<PolyExistentialPredicate<'tcx>>;
|
||||
type PolyFnSig = PolyFnSig<'tcx>;
|
||||
type AllocId = crate::mir::interpret::AllocId;
|
||||
type Pat = Pattern<'tcx>;
|
||||
|
||||
type Pat = Pattern<'tcx>;
|
||||
type Const = ty::Const<'tcx>;
|
||||
type AliasConst = ty::UnevaluatedConst<'tcx>;
|
||||
type PlaceholderConst = ty::PlaceholderConst;
|
||||
type ParamConst = ty::ParamConst;
|
||||
type BoundConst = ty::BoundVar;
|
||||
type ValueConst = ty::ValTree<'tcx>;
|
||||
type ExprConst = ty::Expr<'tcx>;
|
||||
|
||||
type ExprConst = ty::Expr<'tcx>;
|
||||
type Region = Region<'tcx>;
|
||||
type EarlyParamRegion = ty::EarlyParamRegion;
|
||||
type LateParamRegion = ty::LateParamRegion;
|
||||
type BoundRegion = ty::BoundRegion;
|
||||
type InferRegion = ty::RegionVid;
|
||||
type PlaceholderRegion = ty::PlaceholderRegion;
|
||||
|
||||
type PlaceholderRegion = ty::PlaceholderRegion;
|
||||
type Predicate = Predicate<'tcx>;
|
||||
type TraitPredicate = ty::TraitPredicate<'tcx>;
|
||||
type RegionOutlivesPredicate = ty::RegionOutlivesPredicate<'tcx>;
|
||||
type TypeOutlivesPredicate = ty::TypeOutlivesPredicate<'tcx>;
|
||||
type ProjectionPredicate = ty::ProjectionPredicate<'tcx>;
|
||||
type AliasTerm = ty::AliasTerm<'tcx>;
|
||||
type NormalizesTo = ty::NormalizesTo<'tcx>;
|
||||
type SubtypePredicate = ty::SubtypePredicate<'tcx>;
|
||||
type CoercePredicate = ty::CoercePredicate<'tcx>;
|
||||
type ClosureKind = ty::ClosureKind;
|
||||
type Clauses = ty::Clauses<'tcx>;
|
||||
|
||||
type Clauses = ty::Clauses<'tcx>;
|
||||
fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo<Self>]) -> Self::CanonicalVars {
|
||||
self.mk_canonical_var_infos(infos)
|
||||
}
|
||||
|
||||
type GenericsOf = &'tcx ty::Generics;
|
||||
|
||||
fn generics_of(self, def_id: DefId) -> &'tcx ty::Generics {
|
||||
self.generics_of(def_id)
|
||||
}
|
||||
|
||||
fn type_of_instantiated(self, def_id: DefId, args: ty::GenericArgsRef<'tcx>) -> Ty<'tcx> {
|
||||
self.type_of(def_id).instantiate(self, args)
|
||||
}
|
||||
|
||||
fn alias_ty_kind(self, alias: ty::AliasTy<'tcx>) -> ty::AliasTyKind {
|
||||
match self.def_kind(alias.def_id) {
|
||||
DefKind::AssocTy => {
|
||||
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id))
|
||||
{
|
||||
ty::Inherent
|
||||
} else {
|
||||
ty::Projection
|
||||
}
|
||||
}
|
||||
DefKind::OpaqueTy => ty::Opaque,
|
||||
DefKind::TyAlias => ty::Weak,
|
||||
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn alias_term_kind(self, alias: ty::AliasTerm<'tcx>) -> ty::AliasTermKind {
|
||||
match self.def_kind(alias.def_id) {
|
||||
DefKind::AssocTy => {
|
||||
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id))
|
||||
{
|
||||
ty::AliasTermKind::InherentTy
|
||||
} else {
|
||||
ty::AliasTermKind::ProjectionTy
|
||||
}
|
||||
}
|
||||
DefKind::OpaqueTy => ty::AliasTermKind::OpaqueTy,
|
||||
DefKind::TyAlias => ty::AliasTermKind::WeakTy,
|
||||
DefKind::AssocConst => ty::AliasTermKind::ProjectionConst,
|
||||
DefKind::AnonConst => ty::AliasTermKind::UnevaluatedConst,
|
||||
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn trait_ref_and_own_args_for_alias(
|
||||
self,
|
||||
def_id: Self::DefId,
|
||||
args: Self::GenericArgs,
|
||||
) -> (rustc_type_ir::TraitRef<Self>, Self::GenericArgsSlice) {
|
||||
assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst);
|
||||
let trait_def_id = self.parent(def_id);
|
||||
assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
|
||||
let trait_generics = self.generics_of(trait_def_id);
|
||||
(
|
||||
ty::TraitRef::new(self, trait_def_id, args.truncate_to(self, trait_generics)),
|
||||
&args[trait_generics.count()..],
|
||||
)
|
||||
}
|
||||
|
||||
fn mk_args(self, args: &[Self::GenericArg]) -> Self::GenericArgs {
|
||||
self.mk_args(args)
|
||||
}
|
||||
|
||||
fn mk_args_from_iter(self, args: impl Iterator<Item = Self::GenericArg>) -> Self::GenericArgs {
|
||||
self.mk_args_from_iter(args)
|
||||
}
|
||||
|
||||
fn check_and_mk_args(
|
||||
self,
|
||||
def_id: DefId,
|
||||
|
|
|
@ -96,9 +96,9 @@ pub use self::list::{List, ListWithCachedTypeInfo};
|
|||
pub use self::parameterized::ParameterizedOverTcx;
|
||||
pub use self::pattern::{Pattern, PatternKind};
|
||||
pub use self::predicate::{
|
||||
Clause, ClauseKind, CoercePredicate, ExistentialPredicate, ExistentialPredicateStableCmpExt,
|
||||
ExistentialProjection, ExistentialTraitRef, NormalizesTo, OutlivesPredicate,
|
||||
PolyCoercePredicate, PolyExistentialPredicate, PolyExistentialProjection,
|
||||
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,
|
||||
|
@ -110,11 +110,11 @@ pub use self::region::{
|
|||
};
|
||||
pub use self::rvalue_scopes::RvalueScopes;
|
||||
pub use self::sty::{
|
||||
AliasTerm, AliasTy, Article, Binder, BoundTy, BoundTyKind, BoundVariableKind,
|
||||
CanonicalPolyFnSig, ClosureArgs, ClosureArgsParts, CoroutineArgs, CoroutineArgsParts,
|
||||
CoroutineClosureArgs, CoroutineClosureArgsParts, CoroutineClosureSignature, FnSig, GenSig,
|
||||
InlineConstArgs, InlineConstArgsParts, ParamConst, ParamTy, PolyFnSig, TyKind, TypeAndMut,
|
||||
UpvarArgs, VarianceDiagInfo,
|
||||
AliasTy, Article, Binder, BoundTy, BoundTyKind, BoundVariableKind, CanonicalPolyFnSig,
|
||||
ClosureArgs, ClosureArgsParts, CoroutineArgs, CoroutineArgsParts, CoroutineClosureArgs,
|
||||
CoroutineClosureArgsParts, CoroutineClosureSignature, FnSig, GenSig, InlineConstArgs,
|
||||
InlineConstArgsParts, ParamConst, ParamTy, PolyFnSig, TyKind, TypeAndMut, UpvarArgs,
|
||||
VarianceDiagInfo,
|
||||
};
|
||||
pub use self::trait_def::TraitDef;
|
||||
pub use self::typeck_results::{
|
||||
|
|
|
@ -13,6 +13,7 @@ use crate::ty::{
|
|||
};
|
||||
|
||||
pub type TraitRef<'tcx> = ir::TraitRef<TyCtxt<'tcx>>;
|
||||
pub type AliasTerm<'tcx> = ir::AliasTerm<TyCtxt<'tcx>>;
|
||||
pub type ProjectionPredicate<'tcx> = ir::ProjectionPredicate<TyCtxt<'tcx>>;
|
||||
pub type ExistentialPredicate<'tcx> = ir::ExistentialPredicate<TyCtxt<'tcx>>;
|
||||
pub type ExistentialTraitRef<'tcx> = ir::ExistentialTraitRef<TyCtxt<'tcx>>;
|
||||
|
|
|
@ -3038,6 +3038,33 @@ define_print! {
|
|||
p!(write("<{} as {}>", self.self_ty(), self.print_only_trait_path()))
|
||||
}
|
||||
|
||||
ty::AliasTy<'tcx> {
|
||||
let alias_term: ty::AliasTerm<'tcx> = (*self).into();
|
||||
p!(print(alias_term))
|
||||
}
|
||||
|
||||
ty::AliasTerm<'tcx> {
|
||||
match self.kind(cx.tcx()) {
|
||||
ty::AliasTermKind::InherentTy => p!(pretty_print_inherent_projection(*self)),
|
||||
ty::AliasTermKind::ProjectionTy
|
||||
| ty::AliasTermKind::WeakTy
|
||||
| ty::AliasTermKind::OpaqueTy
|
||||
| ty::AliasTermKind::UnevaluatedConst
|
||||
| ty::AliasTermKind::ProjectionConst => {
|
||||
// If we're printing verbosely, or don't want to invoke queries
|
||||
// (`is_impl_trait_in_trait`), then fall back to printing the def path.
|
||||
// This is likely what you want if you're debugging the compiler anyways.
|
||||
if !(cx.should_print_verbose() || with_reduced_queries())
|
||||
&& cx.tcx().is_impl_trait_in_trait(self.def_id)
|
||||
{
|
||||
return cx.pretty_print_opaque_impl_type(self.def_id, self.args);
|
||||
} else {
|
||||
p!(print_def_path(self.def_id, self.args));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ty::TraitPredicate<'tcx> {
|
||||
p!(print(self.trait_ref.self_ty()), ": ");
|
||||
p!(pretty_print_bound_constness(self.trait_ref));
|
||||
|
@ -3205,33 +3232,6 @@ define_print_and_forward_display! {
|
|||
}
|
||||
}
|
||||
|
||||
ty::AliasTy<'tcx> {
|
||||
let alias_term: ty::AliasTerm<'tcx> = (*self).into();
|
||||
p!(print(alias_term))
|
||||
}
|
||||
|
||||
ty::AliasTerm<'tcx> {
|
||||
match self.kind(cx.tcx()) {
|
||||
ty::AliasTermKind::InherentTy => p!(pretty_print_inherent_projection(*self)),
|
||||
ty::AliasTermKind::ProjectionTy
|
||||
| ty::AliasTermKind::WeakTy
|
||||
| ty::AliasTermKind::OpaqueTy
|
||||
| ty::AliasTermKind::UnevaluatedConst
|
||||
| ty::AliasTermKind::ProjectionConst => {
|
||||
// If we're printing verbosely, or don't want to invoke queries
|
||||
// (`is_impl_trait_in_trait`), then fall back to printing the def path.
|
||||
// This is likely what you want if you're debugging the compiler anyways.
|
||||
if !(cx.should_print_verbose() || with_reduced_queries())
|
||||
&& cx.tcx().is_impl_trait_in_trait(self.def_id)
|
||||
{
|
||||
return cx.pretty_print_opaque_impl_type(self.def_id, self.args);
|
||||
} else {
|
||||
p!(print_def_path(self.def_id, self.args));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ty::Predicate<'tcx> {
|
||||
p!(print(self.kind()))
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use crate::mir::interpret;
|
|||
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable};
|
||||
use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer};
|
||||
use crate::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
|
||||
use crate::ty::{self, AliasTy, InferConst, Lift, Term, TermKind, Ty, TyCtxt};
|
||||
use crate::ty::{self, InferConst, Lift, Term, TermKind, Ty, TyCtxt};
|
||||
use rustc_ast_ir::try_visit;
|
||||
use rustc_ast_ir::visit::VisitorResult;
|
||||
use rustc_hir::def::Namespace;
|
||||
|
@ -164,23 +164,6 @@ impl<'tcx> fmt::Debug for ty::Clause<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> fmt::Debug for AliasTy<'tcx> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
WithInfcx::with_no_infcx(self).fmt(f)
|
||||
}
|
||||
}
|
||||
impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for AliasTy<'tcx> {
|
||||
fn fmt<Infcx: InferCtxtLike<Interner = TyCtxt<'tcx>>>(
|
||||
this: WithInfcx<'_, Infcx, &Self>,
|
||||
f: &mut core::fmt::Formatter<'_>,
|
||||
) -> core::fmt::Result {
|
||||
f.debug_struct("AliasTy")
|
||||
.field("args", &this.map(|data| data.args))
|
||||
.field("def_id", &this.data.def_id)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for Pattern<'tcx> {
|
||||
fn fmt<Infcx: InferCtxtLike<Interner = TyCtxt<'tcx>>>(
|
||||
this: WithInfcx<'_, Infcx, &Self>,
|
||||
|
@ -230,23 +213,6 @@ impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::consts::Expr<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> fmt::Debug for ty::UnevaluatedConst<'tcx> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
WithInfcx::with_no_infcx(self).fmt(f)
|
||||
}
|
||||
}
|
||||
impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::UnevaluatedConst<'tcx> {
|
||||
fn fmt<Infcx: InferCtxtLike<Interner = TyCtxt<'tcx>>>(
|
||||
this: WithInfcx<'_, Infcx, &Self>,
|
||||
f: &mut core::fmt::Formatter<'_>,
|
||||
) -> core::fmt::Result {
|
||||
f.debug_struct("UnevaluatedConst")
|
||||
.field("def", &this.data.def)
|
||||
.field("args", &this.wrap(this.data.args))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> fmt::Debug for ty::Const<'tcx> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
WithInfcx::with_no_infcx(self).fmt(f)
|
||||
|
|
|
@ -28,20 +28,17 @@ use std::iter;
|
|||
use std::ops::{ControlFlow, Deref, Range};
|
||||
use ty::util::IntTypeExt;
|
||||
|
||||
use rustc_type_ir::BoundVar;
|
||||
use rustc_type_ir::CollectAndApply;
|
||||
use rustc_type_ir::DynKind;
|
||||
use rustc_type_ir::TyKind as IrTyKind;
|
||||
use rustc_type_ir::TyKind::*;
|
||||
use rustc_type_ir::TypeAndMut as IrTypeAndMut;
|
||||
use rustc_type_ir::{self as ir, BoundVar, CollectAndApply, DynKind};
|
||||
|
||||
use super::fold::FnMutDelegate;
|
||||
use super::GenericParamDefKind;
|
||||
|
||||
// Re-export and re-parameterize some `I = TyCtxt<'tcx>` types here
|
||||
#[rustc_diagnostic_item = "TyKind"]
|
||||
pub type TyKind<'tcx> = IrTyKind<TyCtxt<'tcx>>;
|
||||
pub type TypeAndMut<'tcx> = IrTypeAndMut<TyCtxt<'tcx>>;
|
||||
pub type TyKind<'tcx> = ir::TyKind<TyCtxt<'tcx>>;
|
||||
pub type TypeAndMut<'tcx> = ir::TypeAndMut<TyCtxt<'tcx>>;
|
||||
pub type AliasTy<'tcx> = ir::AliasTy<TyCtxt<'tcx>>;
|
||||
|
||||
pub trait Article {
|
||||
fn article(&self) -> &'static str;
|
||||
|
@ -1105,371 +1102,6 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// Represents the unprojected term of a projection goal.
|
||||
///
|
||||
/// * For a projection, this would be `<Ty as Trait<...>>::N<...>`.
|
||||
/// * For an inherent projection, this would be `Ty::N<...>`.
|
||||
/// * For an opaque type, there is no explicit syntax.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
|
||||
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
|
||||
pub struct AliasTerm<'tcx> {
|
||||
/// The parameters of the associated or opaque item.
|
||||
///
|
||||
/// For a projection, these are the generic parameters for the trait and the
|
||||
/// GAT parameters, if there are any.
|
||||
///
|
||||
/// For an inherent projection, they consist of the self type and the GAT parameters,
|
||||
/// if there are any.
|
||||
///
|
||||
/// For RPIT the generic parameters are for the generics of the function,
|
||||
/// while for TAIT it is used for the generic parameters of the alias.
|
||||
pub args: GenericArgsRef<'tcx>,
|
||||
|
||||
/// The `DefId` of the `TraitItem` or `ImplItem` for the associated type `N` depending on whether
|
||||
/// this is a projection or an inherent projection or the `DefId` of the `OpaqueType` item if
|
||||
/// this is an opaque.
|
||||
///
|
||||
/// During codegen, `tcx.type_of(def_id)` can be used to get the type of the
|
||||
/// underlying type if the type is an opaque.
|
||||
///
|
||||
/// Note that if this is an associated type, this is not the `DefId` of the
|
||||
/// `TraitRef` containing this associated type, which is in `tcx.associated_item(def_id).container`,
|
||||
/// aka. `tcx.parent(def_id)`.
|
||||
pub def_id: DefId,
|
||||
|
||||
/// This field exists to prevent the creation of `AliasTerm` without using
|
||||
/// [AliasTerm::new].
|
||||
_use_alias_term_new_instead: (),
|
||||
}
|
||||
|
||||
// FIXME: Remove these when we uplift `AliasTerm`
|
||||
use crate::ty::{DebugWithInfcx, InferCtxtLike, WithInfcx};
|
||||
impl<'tcx> std::fmt::Debug for AliasTerm<'tcx> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
WithInfcx::with_no_infcx(self).fmt(f)
|
||||
}
|
||||
}
|
||||
impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for AliasTerm<'tcx> {
|
||||
fn fmt<Infcx: InferCtxtLike<Interner = TyCtxt<'tcx>>>(
|
||||
this: WithInfcx<'_, Infcx, &Self>,
|
||||
f: &mut std::fmt::Formatter<'_>,
|
||||
) -> std::fmt::Result {
|
||||
f.debug_struct("AliasTerm")
|
||||
.field("args", &this.map(|data| data.args))
|
||||
.field("def_id", &this.data.def_id)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> rustc_type_ir::inherent::AliasTerm<TyCtxt<'tcx>> for AliasTerm<'tcx> {
|
||||
fn new(
|
||||
interner: TyCtxt<'tcx>,
|
||||
trait_def_id: DefId,
|
||||
args: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>,
|
||||
) -> Self {
|
||||
AliasTerm::new(interner, trait_def_id, args)
|
||||
}
|
||||
|
||||
fn def_id(self) -> DefId {
|
||||
self.def_id
|
||||
}
|
||||
|
||||
fn args(self) -> ty::GenericArgsRef<'tcx> {
|
||||
self.args
|
||||
}
|
||||
|
||||
fn trait_def_id(self, interner: TyCtxt<'tcx>) -> DefId {
|
||||
self.trait_def_id(interner)
|
||||
}
|
||||
|
||||
fn self_ty(self) -> Ty<'tcx> {
|
||||
self.self_ty()
|
||||
}
|
||||
|
||||
fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self {
|
||||
self.with_self_ty(tcx, self_ty)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> AliasTerm<'tcx> {
|
||||
pub fn new(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
def_id: DefId,
|
||||
args: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
|
||||
) -> AliasTerm<'tcx> {
|
||||
let args = tcx.check_and_mk_args(def_id, args);
|
||||
AliasTerm { def_id, args, _use_alias_term_new_instead: () }
|
||||
}
|
||||
|
||||
pub fn expect_ty(self, tcx: TyCtxt<'tcx>) -> AliasTy<'tcx> {
|
||||
match self.kind(tcx) {
|
||||
ty::AliasTermKind::ProjectionTy
|
||||
| ty::AliasTermKind::InherentTy
|
||||
| ty::AliasTermKind::OpaqueTy
|
||||
| ty::AliasTermKind::WeakTy => {}
|
||||
ty::AliasTermKind::UnevaluatedConst | ty::AliasTermKind::ProjectionConst => {
|
||||
bug!("Cannot turn `UnevaluatedConst` into `AliasTy`")
|
||||
}
|
||||
}
|
||||
ty::AliasTy { def_id: self.def_id, args: self.args, _use_alias_ty_new_instead: () }
|
||||
}
|
||||
|
||||
pub fn kind(self, tcx: TyCtxt<'tcx>) -> ty::AliasTermKind {
|
||||
match tcx.def_kind(self.def_id) {
|
||||
DefKind::AssocTy => {
|
||||
if let DefKind::Impl { of_trait: false } = tcx.def_kind(tcx.parent(self.def_id)) {
|
||||
ty::AliasTermKind::InherentTy
|
||||
} else {
|
||||
ty::AliasTermKind::ProjectionTy
|
||||
}
|
||||
}
|
||||
DefKind::OpaqueTy => ty::AliasTermKind::OpaqueTy,
|
||||
DefKind::TyAlias => ty::AliasTermKind::WeakTy,
|
||||
DefKind::AnonConst => ty::AliasTermKind::UnevaluatedConst,
|
||||
DefKind::AssocConst => ty::AliasTermKind::ProjectionConst,
|
||||
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The following methods work only with (trait) associated item projections.
|
||||
impl<'tcx> AliasTerm<'tcx> {
|
||||
pub fn self_ty(self) -> Ty<'tcx> {
|
||||
self.args.type_at(0)
|
||||
}
|
||||
|
||||
pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self {
|
||||
AliasTerm::new(
|
||||
tcx,
|
||||
self.def_id,
|
||||
[self_ty.into()].into_iter().chain(self.args.iter().skip(1)),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId {
|
||||
match tcx.def_kind(self.def_id) {
|
||||
DefKind::AssocTy | DefKind::AssocConst => tcx.parent(self.def_id),
|
||||
kind => bug!("expected a projection AliasTy; found {kind:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts the underlying trait reference from this projection.
|
||||
/// For example, if this is a projection of `<T as Iterator>::Item`,
|
||||
/// then this function would return a `T: Iterator` trait reference.
|
||||
///
|
||||
/// NOTE: This will drop the args for generic associated types
|
||||
/// consider calling [Self::trait_ref_and_own_args] to get those
|
||||
/// as well.
|
||||
pub fn trait_ref(self, tcx: TyCtxt<'tcx>) -> ty::TraitRef<'tcx> {
|
||||
let def_id = self.trait_def_id(tcx);
|
||||
ty::TraitRef::new(tcx, def_id, self.args.truncate_to(tcx, tcx.generics_of(def_id)))
|
||||
}
|
||||
|
||||
/// Extracts the underlying trait reference and own args from this projection.
|
||||
/// For example, if this is a projection of `<T as StreamingIterator>::Item<'a>`,
|
||||
/// then this function would return a `T: StreamingIterator` trait reference and `['a]` as the own args
|
||||
pub fn trait_ref_and_own_args(
|
||||
self,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
|
||||
let trait_def_id = self.trait_def_id(tcx);
|
||||
let trait_generics = tcx.generics_of(trait_def_id);
|
||||
(
|
||||
ty::TraitRef::new(tcx, trait_def_id, self.args.truncate_to(tcx, trait_generics)),
|
||||
&self.args[trait_generics.count()..],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn to_term(self, tcx: TyCtxt<'tcx>) -> ty::Term<'tcx> {
|
||||
match self.kind(tcx) {
|
||||
ty::AliasTermKind::ProjectionTy => Ty::new_alias(
|
||||
tcx,
|
||||
ty::Projection,
|
||||
AliasTy { def_id: self.def_id, args: self.args, _use_alias_ty_new_instead: () },
|
||||
)
|
||||
.into(),
|
||||
ty::AliasTermKind::InherentTy => Ty::new_alias(
|
||||
tcx,
|
||||
ty::Inherent,
|
||||
AliasTy { def_id: self.def_id, args: self.args, _use_alias_ty_new_instead: () },
|
||||
)
|
||||
.into(),
|
||||
ty::AliasTermKind::OpaqueTy => Ty::new_alias(
|
||||
tcx,
|
||||
ty::Opaque,
|
||||
AliasTy { def_id: self.def_id, args: self.args, _use_alias_ty_new_instead: () },
|
||||
)
|
||||
.into(),
|
||||
ty::AliasTermKind::WeakTy => Ty::new_alias(
|
||||
tcx,
|
||||
ty::Weak,
|
||||
AliasTy { def_id: self.def_id, args: self.args, _use_alias_ty_new_instead: () },
|
||||
)
|
||||
.into(),
|
||||
ty::AliasTermKind::UnevaluatedConst | ty::AliasTermKind::ProjectionConst => {
|
||||
ty::Const::new_unevaluated(
|
||||
tcx,
|
||||
ty::UnevaluatedConst::new(self.def_id, self.args),
|
||||
tcx.type_of(self.def_id).instantiate(tcx, self.args),
|
||||
)
|
||||
.into()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> From<AliasTy<'tcx>> for AliasTerm<'tcx> {
|
||||
fn from(ty: AliasTy<'tcx>) -> Self {
|
||||
AliasTerm { args: ty.args, def_id: ty.def_id, _use_alias_term_new_instead: () }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> From<ty::UnevaluatedConst<'tcx>> for AliasTerm<'tcx> {
|
||||
fn from(ct: ty::UnevaluatedConst<'tcx>) -> Self {
|
||||
AliasTerm { args: ct.args, def_id: ct.def, _use_alias_term_new_instead: () }
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the projection of an associated, opaque, or lazy-type-alias type.
|
||||
///
|
||||
/// * For a projection, this would be `<Ty as Trait<...>>::N<...>`.
|
||||
/// * For an inherent projection, this would be `Ty::N<...>`.
|
||||
/// * For an opaque type, there is no explicit syntax.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
|
||||
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
|
||||
pub struct AliasTy<'tcx> {
|
||||
/// The parameters of the associated or opaque type.
|
||||
///
|
||||
/// For a projection, these are the generic parameters for the trait and the
|
||||
/// GAT parameters, if there are any.
|
||||
///
|
||||
/// For an inherent projection, they consist of the self type and the GAT parameters,
|
||||
/// if there are any.
|
||||
///
|
||||
/// For RPIT the generic parameters are for the generics of the function,
|
||||
/// while for TAIT it is used for the generic parameters of the alias.
|
||||
pub args: GenericArgsRef<'tcx>,
|
||||
|
||||
/// The `DefId` of the `TraitItem` or `ImplItem` for the associated type `N` depending on whether
|
||||
/// this is a projection or an inherent projection or the `DefId` of the `OpaqueType` item if
|
||||
/// this is an opaque.
|
||||
///
|
||||
/// During codegen, `tcx.type_of(def_id)` can be used to get the type of the
|
||||
/// underlying type if the type is an opaque.
|
||||
///
|
||||
/// Note that if this is an associated type, this is not the `DefId` of the
|
||||
/// `TraitRef` containing this associated type, which is in `tcx.associated_item(def_id).container`,
|
||||
/// aka. `tcx.parent(def_id)`.
|
||||
pub def_id: DefId,
|
||||
|
||||
/// This field exists to prevent the creation of `AliasT` without using
|
||||
/// [AliasTy::new].
|
||||
_use_alias_ty_new_instead: (),
|
||||
}
|
||||
|
||||
impl<'tcx> AliasTy<'tcx> {
|
||||
pub fn new(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
def_id: DefId,
|
||||
args: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
|
||||
) -> ty::AliasTy<'tcx> {
|
||||
let args = tcx.check_and_mk_args(def_id, args);
|
||||
ty::AliasTy { def_id, args, _use_alias_ty_new_instead: () }
|
||||
}
|
||||
|
||||
pub fn kind(self, tcx: TyCtxt<'tcx>) -> ty::AliasTyKind {
|
||||
match tcx.def_kind(self.def_id) {
|
||||
DefKind::AssocTy
|
||||
if let DefKind::Impl { of_trait: false } =
|
||||
tcx.def_kind(tcx.parent(self.def_id)) =>
|
||||
{
|
||||
ty::Inherent
|
||||
}
|
||||
DefKind::AssocTy => ty::Projection,
|
||||
DefKind::OpaqueTy => ty::Opaque,
|
||||
DefKind::TyAlias => ty::Weak,
|
||||
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this alias type is an opaque.
|
||||
pub fn is_opaque(self, tcx: TyCtxt<'tcx>) -> bool {
|
||||
matches!(self.kind(tcx), ty::Opaque)
|
||||
}
|
||||
|
||||
pub fn to_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
|
||||
Ty::new_alias(tcx, self.kind(tcx), self)
|
||||
}
|
||||
}
|
||||
|
||||
/// The following methods work only with (trait) associated type projections.
|
||||
impl<'tcx> AliasTy<'tcx> {
|
||||
pub fn self_ty(self) -> Ty<'tcx> {
|
||||
self.args.type_at(0)
|
||||
}
|
||||
|
||||
pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self {
|
||||
AliasTy::new(tcx, self.def_id, [self_ty.into()].into_iter().chain(self.args.iter().skip(1)))
|
||||
}
|
||||
|
||||
pub fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId {
|
||||
match tcx.def_kind(self.def_id) {
|
||||
DefKind::AssocTy | DefKind::AssocConst => tcx.parent(self.def_id),
|
||||
kind => bug!("expected a projection AliasTy; found {kind:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts the underlying trait reference and own args from this projection.
|
||||
/// For example, if this is a projection of `<T as StreamingIterator>::Item<'a>`,
|
||||
/// then this function would return a `T: StreamingIterator` trait reference and `['a]` as the own args
|
||||
pub fn trait_ref_and_own_args(
|
||||
self,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
|
||||
let trait_def_id = self.trait_def_id(tcx);
|
||||
let trait_generics = tcx.generics_of(trait_def_id);
|
||||
(
|
||||
ty::TraitRef::new(tcx, trait_def_id, self.args.truncate_to(tcx, trait_generics)),
|
||||
&self.args[trait_generics.count()..],
|
||||
)
|
||||
}
|
||||
|
||||
/// Extracts the underlying trait reference from this projection.
|
||||
/// For example, if this is a projection of `<T as Iterator>::Item`,
|
||||
/// then this function would return a `T: Iterator` trait reference.
|
||||
///
|
||||
/// WARNING: This will drop the args for generic associated types
|
||||
/// consider calling [Self::trait_ref_and_own_args] to get those
|
||||
/// as well.
|
||||
pub fn trait_ref(self, tcx: TyCtxt<'tcx>) -> ty::TraitRef<'tcx> {
|
||||
let def_id = self.trait_def_id(tcx);
|
||||
ty::TraitRef::new(tcx, def_id, self.args.truncate_to(tcx, tcx.generics_of(def_id)))
|
||||
}
|
||||
}
|
||||
|
||||
/// The following methods work only with inherent associated type projections.
|
||||
impl<'tcx> AliasTy<'tcx> {
|
||||
/// Transform the generic parameters to have the given `impl` args as the base and the GAT args on top of that.
|
||||
///
|
||||
/// Does the following transformation:
|
||||
///
|
||||
/// ```text
|
||||
/// [Self, P_0...P_m] -> [I_0...I_n, P_0...P_m]
|
||||
///
|
||||
/// I_i impl args
|
||||
/// P_j GAT args
|
||||
/// ```
|
||||
pub fn rebase_inherent_args_onto_impl(
|
||||
self,
|
||||
impl_args: ty::GenericArgsRef<'tcx>,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
) -> ty::GenericArgsRef<'tcx> {
|
||||
debug_assert_eq!(self.kind(tcx), ty::Inherent);
|
||||
|
||||
tcx.mk_args_from_iter(impl_args.into_iter().chain(self.args.into_iter().skip(1)))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable)]
|
||||
pub struct GenSig<'tcx> {
|
||||
pub resume_ty: Ty<'tcx>,
|
||||
|
@ -2020,6 +1652,18 @@ impl<'tcx> rustc_type_ir::inherent::Ty<TyCtxt<'tcx>> for Ty<'tcx> {
|
|||
fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
|
||||
Ty::new_bound(tcx, debruijn, ty::BoundTy { var, kind: ty::BoundTyKind::Anon })
|
||||
}
|
||||
|
||||
fn new_alias(
|
||||
interner: TyCtxt<'tcx>,
|
||||
kind: ty::AliasTyKind,
|
||||
alias_ty: ty::AliasTy<'tcx>,
|
||||
) -> Self {
|
||||
Ty::new_alias(interner, kind, alias_ty)
|
||||
}
|
||||
|
||||
fn into_term(self) -> ty::Term<'tcx> {
|
||||
self.into()
|
||||
}
|
||||
}
|
||||
|
||||
/// Type utilities
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue