Skip to content

Commit 9fa07a4

Browse files
Uplift TermKind
1 parent b0f1afd commit 9fa07a4

File tree

5 files changed

+41
-20
lines changed

5 files changed

+41
-20
lines changed

compiler/rustc_middle/src/ty/generic_args.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use std::ops::Deref;
2727
use std::ptr::NonNull;
2828

2929
pub type GenericArgKind<'tcx> = rustc_type_ir::GenericArgKind<TyCtxt<'tcx>>;
30+
pub type TermKind<'tcx> = rustc_type_ir::TermKind<TyCtxt<'tcx>>;
3031

3132
/// An entity in the Rust type system, which can be one of
3233
/// several kinds (types, lifetimes, and consts).

compiler/rustc_middle/src/ty/mod.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::ty::fast_reject::SimplifiedType;
2828
use crate::ty::util::Discr;
2929
pub use adt::*;
3030
pub use assoc::*;
31-
pub use generic_args::{GenericArgKind, *};
31+
pub use generic_args::{GenericArgKind, TermKind, *};
3232
pub use generics::*;
3333
pub use intrinsic::IntrinsicDef;
3434
use rustc_ast as ast;
@@ -48,7 +48,8 @@ use rustc_hir::def::{CtorKind, CtorOf, DefKind, DocLinkResMap, LifetimeRes, Res}
4848
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LocalDefIdMap};
4949
use rustc_index::IndexVec;
5050
use rustc_macros::{
51-
Decodable, Encodable, HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable,
51+
extension, Decodable, Encodable, HashStable, TyDecodable, TyEncodable, TypeFoldable,
52+
TypeVisitable,
5253
};
5354
use rustc_query_system::ich::StableHashingContext;
5455
use rustc_serialize::{Decodable, Encodable};
@@ -521,6 +522,14 @@ pub struct Term<'tcx> {
521522
marker: PhantomData<(Ty<'tcx>, Const<'tcx>)>,
522523
}
523524

525+
impl<'tcx> rustc_type_ir::inherent::IntoKind for Term<'tcx> {
526+
type Kind = TermKind<'tcx>;
527+
528+
fn kind(self) -> Self::Kind {
529+
self.unpack()
530+
}
531+
}
532+
524533
#[cfg(parallel_compiler)]
525534
unsafe impl<'tcx> rustc_data_structures::sync::DynSend for Term<'tcx> where
526535
&'tcx (Ty<'tcx>, Const<'tcx>): rustc_data_structures::sync::DynSend
@@ -566,13 +575,19 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for Term<'tcx> {
566575
self,
567576
folder: &mut F,
568577
) -> Result<Self, F::Error> {
569-
Ok(self.unpack().try_fold_with(folder)?.pack())
578+
match self.unpack() {
579+
ty::TermKind::Ty(ty) => ty.try_fold_with(folder).map(Into::into),
580+
ty::TermKind::Const(ct) => ct.try_fold_with(folder).map(Into::into),
581+
}
570582
}
571583
}
572584

573585
impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for Term<'tcx> {
574586
fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
575-
self.unpack().visit_with(visitor)
587+
match self.unpack() {
588+
ty::TermKind::Ty(ty) => ty.visit_with(visitor),
589+
ty::TermKind::Const(ct) => ct.visit_with(visitor),
590+
}
576591
}
577592
}
578593

@@ -650,13 +665,7 @@ const TAG_MASK: usize = 0b11;
650665
const TYPE_TAG: usize = 0b00;
651666
const CONST_TAG: usize = 0b01;
652667

653-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
654-
#[derive(HashStable, TypeFoldable, TypeVisitable)]
655-
pub enum TermKind<'tcx> {
656-
Ty(Ty<'tcx>),
657-
Const(Const<'tcx>),
658-
}
659-
668+
#[extension(pub trait TermKindPackExt<'tcx>)]
660669
impl<'tcx> TermKind<'tcx> {
661670
#[inline]
662671
fn pack(self) -> Term<'tcx> {

compiler/rustc_middle/src/ty/structural_impls.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,10 @@ impl<'tcx, T: Lift<TyCtxt<'tcx>>> Lift<TyCtxt<'tcx>> for Option<T> {
371371
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for Term<'a> {
372372
type Lifted = ty::Term<'tcx>;
373373
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
374-
Some(
375-
match self.unpack() {
376-
TermKind::Ty(ty) => TermKind::Ty(tcx.lift(ty)?),
377-
TermKind::Const(c) => TermKind::Const(tcx.lift(c)?),
378-
}
379-
.pack(),
380-
)
374+
match self.unpack() {
375+
TermKind::Ty(ty) => tcx.lift(ty).map(Into::into),
376+
TermKind::Const(c) => tcx.lift(c).map(Into::into),
377+
}
381378
}
382379
}
383380

compiler/rustc_type_ir/src/generic_arg.rs

+14
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,17 @@ pub enum GenericArgKind<I: Interner> {
1616
Type(I::Ty),
1717
Const(I::Const),
1818
}
19+
20+
#[derive(derivative::Derivative)]
21+
#[derivative(
22+
Clone(bound = ""),
23+
Copy(bound = ""),
24+
Debug(bound = ""),
25+
Eq(bound = ""),
26+
PartialEq(bound = "")
27+
)]
28+
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
29+
pub enum TermKind<I: Interner> {
30+
Ty(I::Ty),
31+
Const(I::Const),
32+
}

compiler/rustc_type_ir/src/interner.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable};
1010
use crate::{
1111
AliasTerm, AliasTermKind, AliasTy, AliasTyKind, CanonicalVarInfo, CoercePredicate,
1212
DebugWithInfcx, ExistentialProjection, ExistentialTraitRef, FnSig, GenericArgKind,
13-
NormalizesTo, ProjectionPredicate, SubtypePredicate, TraitPredicate, TraitRef,
13+
NormalizesTo, ProjectionPredicate, SubtypePredicate, TermKind, TraitPredicate, TraitRef,
1414
};
1515

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

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

0 commit comments

Comments
 (0)