Skip to content

Commit

Permalink
Uplift TermKind
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed May 20, 2024
1 parent b0f1afd commit 9fa07a4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/generic_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::ops::Deref;
use std::ptr::NonNull;

pub type GenericArgKind<'tcx> = rustc_type_ir::GenericArgKind<TyCtxt<'tcx>>;
pub type TermKind<'tcx> = rustc_type_ir::TermKind<TyCtxt<'tcx>>;

/// An entity in the Rust type system, which can be one of
/// several kinds (types, lifetimes, and consts).
Expand Down
31 changes: 20 additions & 11 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::ty::fast_reject::SimplifiedType;
use crate::ty::util::Discr;
pub use adt::*;
pub use assoc::*;
pub use generic_args::{GenericArgKind, *};
pub use generic_args::{GenericArgKind, TermKind, *};
pub use generics::*;
pub use intrinsic::IntrinsicDef;
use rustc_ast as ast;
Expand All @@ -48,7 +48,8 @@ use rustc_hir::def::{CtorKind, CtorOf, DefKind, DocLinkResMap, LifetimeRes, Res}
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LocalDefIdMap};
use rustc_index::IndexVec;
use rustc_macros::{
Decodable, Encodable, HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable,
extension, Decodable, Encodable, HashStable, TyDecodable, TyEncodable, TypeFoldable,
TypeVisitable,
};
use rustc_query_system::ich::StableHashingContext;
use rustc_serialize::{Decodable, Encodable};
Expand Down Expand Up @@ -521,6 +522,14 @@ pub struct Term<'tcx> {
marker: PhantomData<(Ty<'tcx>, Const<'tcx>)>,
}

impl<'tcx> rustc_type_ir::inherent::IntoKind for Term<'tcx> {
type Kind = TermKind<'tcx>;

fn kind(self) -> Self::Kind {
self.unpack()
}
}

#[cfg(parallel_compiler)]
unsafe impl<'tcx> rustc_data_structures::sync::DynSend for Term<'tcx> where
&'tcx (Ty<'tcx>, Const<'tcx>): rustc_data_structures::sync::DynSend
Expand Down Expand Up @@ -566,13 +575,19 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for Term<'tcx> {
self,
folder: &mut F,
) -> Result<Self, F::Error> {
Ok(self.unpack().try_fold_with(folder)?.pack())
match self.unpack() {
ty::TermKind::Ty(ty) => ty.try_fold_with(folder).map(Into::into),
ty::TermKind::Const(ct) => ct.try_fold_with(folder).map(Into::into),
}
}
}

impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for Term<'tcx> {
fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
self.unpack().visit_with(visitor)
match self.unpack() {
ty::TermKind::Ty(ty) => ty.visit_with(visitor),
ty::TermKind::Const(ct) => ct.visit_with(visitor),
}
}
}

Expand Down Expand Up @@ -650,13 +665,7 @@ const TAG_MASK: usize = 0b11;
const TYPE_TAG: usize = 0b00;
const CONST_TAG: usize = 0b01;

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
#[derive(HashStable, TypeFoldable, TypeVisitable)]
pub enum TermKind<'tcx> {
Ty(Ty<'tcx>),
Const(Const<'tcx>),
}

#[extension(pub trait TermKindPackExt<'tcx>)]
impl<'tcx> TermKind<'tcx> {
#[inline]
fn pack(self) -> Term<'tcx> {
Expand Down
11 changes: 4 additions & 7 deletions compiler/rustc_middle/src/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,10 @@ impl<'tcx, T: Lift<TyCtxt<'tcx>>> Lift<TyCtxt<'tcx>> for Option<T> {
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for Term<'a> {
type Lifted = ty::Term<'tcx>;
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
Some(
match self.unpack() {
TermKind::Ty(ty) => TermKind::Ty(tcx.lift(ty)?),
TermKind::Const(c) => TermKind::Const(tcx.lift(c)?),
}
.pack(),
)
match self.unpack() {
TermKind::Ty(ty) => tcx.lift(ty).map(Into::into),
TermKind::Const(c) => tcx.lift(c).map(Into::into),
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_type_ir/src/generic_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,17 @@ pub enum GenericArgKind<I: Interner> {
Type(I::Ty),
Const(I::Const),
}

#[derive(derivative::Derivative)]
#[derivative(
Clone(bound = ""),
Copy(bound = ""),
Debug(bound = ""),
Eq(bound = ""),
PartialEq(bound = "")
)]
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
pub enum TermKind<I: Interner> {
Ty(I::Ty),
Const(I::Const),
}
4 changes: 2 additions & 2 deletions compiler/rustc_type_ir/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable};
use crate::{
AliasTerm, AliasTermKind, AliasTy, AliasTyKind, CanonicalVarInfo, CoercePredicate,
DebugWithInfcx, ExistentialProjection, ExistentialTraitRef, FnSig, GenericArgKind,
NormalizesTo, ProjectionPredicate, SubtypePredicate, TraitPredicate, TraitRef,
NormalizesTo, ProjectionPredicate, SubtypePredicate, TermKind, TraitPredicate, TraitRef,
};

pub trait Interner:
Expand All @@ -36,7 +36,7 @@ pub trait Interner:
/// not including the args from the parent item (trait or impl).
type OwnItemArgs: Copy + Debug + Hash + Eq;
type GenericArg: Copy + DebugWithInfcx<Self> + Hash + Eq + IntoKind<Kind = GenericArgKind<Self>>;
type Term: Copy + Debug + Hash + Eq;
type Term: Copy + Debug + Hash + Eq + IntoKind<Kind = TermKind<Self>>;

type Binder<T: TypeVisitable<Self>>: BoundVars<Self> + TypeSuperVisitable<Self>;
type BoundVars: IntoIterator<Item = Self::BoundVar>;
Expand Down

0 comments on commit 9fa07a4

Please sign in to comment.