1
Fork 0

Pre-intern some commonly used type variables.

This requires some rearrangement of plumbing, such as adding
`mk_fresh_{,int_,float_}ty` and removing `mk_ty_infer`.
This commit is contained in:
Nicholas Nethercote 2023-02-08 11:52:19 +11:00
parent 5b8f284536
commit 6248bbbf26
6 changed files with 80 additions and 31 deletions

View file

@ -241,6 +241,11 @@ impl<'tcx> CtxtInterners<'tcx> {
}
}
const NUM_PREINTERNED_TY_VARS: u32 = 100;
const NUM_PREINTERNED_FRESH_TYS: u32 = 20;
const NUM_PREINTERNED_FRESH_INT_TYS: u32 = 3;
const NUM_PREINTERNED_FRESH_FLOAT_TYS: u32 = 3;
pub struct CommonTypes<'tcx> {
pub unit: Ty<'tcx>,
pub bool: Ty<'tcx>,
@ -266,7 +271,20 @@ pub struct CommonTypes<'tcx> {
/// Dummy type used for the `Self` of a `TraitRef` created for converting
/// a trait object, and which gets removed in `ExistentialTraitRef`.
/// This type must not appear anywhere in other converted types.
/// `Infer(ty::FreshTy(0))` does the job.
pub trait_object_dummy_self: Ty<'tcx>,
/// Pre-interned `Infer(ty::TyVar(n))` for small values of `n`.
pub ty_vars: Vec<Ty<'tcx>>,
/// Pre-interned `Infer(ty::FreshTy(n))` for small values of `n`.
pub fresh_tys: Vec<Ty<'tcx>>,
/// Pre-interned `Infer(ty::FreshIntTy(n))` for small values of `n`.
pub fresh_int_tys: Vec<Ty<'tcx>>,
/// Pre-interned `Infer(ty::FreshFloatTy(n))` for small values of `n`.
pub fresh_float_tys: Vec<Ty<'tcx>>,
}
pub struct CommonLifetimes<'tcx> {
@ -289,6 +307,15 @@ impl<'tcx> CommonTypes<'tcx> {
) -> CommonTypes<'tcx> {
let mk = |ty| interners.intern_ty(ty, sess, untracked);
let ty_vars =
(0..NUM_PREINTERNED_TY_VARS).map(|n| mk(Infer(ty::TyVar(TyVid::from(n))))).collect();
let fresh_tys: Vec<_> =
(0..NUM_PREINTERNED_FRESH_TYS).map(|n| mk(Infer(ty::FreshTy(n)))).collect();
let fresh_int_tys: Vec<_> =
(0..NUM_PREINTERNED_FRESH_INT_TYS).map(|n| mk(Infer(ty::FreshIntTy(n)))).collect();
let fresh_float_tys: Vec<_> =
(0..NUM_PREINTERNED_FRESH_FLOAT_TYS).map(|n| mk(Infer(ty::FreshFloatTy(n)))).collect();
CommonTypes {
unit: mk(Tuple(List::empty())),
bool: mk(Bool),
@ -311,7 +338,12 @@ impl<'tcx> CommonTypes<'tcx> {
str_: mk(Str),
self_param: mk(ty::Param(ty::ParamTy { index: 0, name: kw::SelfUpper })),
trait_object_dummy_self: mk(Infer(ty::FreshTy(0))),
trait_object_dummy_self: fresh_tys[0],
ty_vars,
fresh_tys,
fresh_int_tys,
fresh_float_tys,
}
}
}
@ -1867,29 +1899,55 @@ impl<'tcx> TyCtxt<'tcx> {
self.mk_ty(GeneratorWitnessMIR(id, substs))
}
#[inline]
pub fn mk_ty_var(self, v: TyVid) -> Ty<'tcx> {
self.mk_ty_infer(TyVar(v))
}
#[inline]
pub fn mk_const(self, kind: impl Into<ty::ConstKind<'tcx>>, ty: Ty<'tcx>) -> Const<'tcx> {
self.mk_const_internal(ty::ConstData { kind: kind.into(), ty })
}
#[inline]
pub fn mk_ty_var(self, v: TyVid) -> Ty<'tcx> {
// Use a pre-interned one when possible.
self.types.ty_vars.get(v.as_usize()).copied().unwrap_or_else(|| self.mk_ty(Infer(TyVar(v))))
}
#[inline]
pub fn mk_int_var(self, v: IntVid) -> Ty<'tcx> {
self.mk_ty_infer(IntVar(v))
self.mk_ty(Infer(IntVar(v)))
}
#[inline]
pub fn mk_float_var(self, v: FloatVid) -> Ty<'tcx> {
self.mk_ty_infer(FloatVar(v))
self.mk_ty(Infer(FloatVar(v)))
}
#[inline]
pub fn mk_ty_infer(self, it: InferTy) -> Ty<'tcx> {
self.mk_ty(Infer(it))
pub fn mk_fresh_ty(self, n: u32) -> Ty<'tcx> {
// Use a pre-interned one when possible.
self.types
.fresh_tys
.get(n as usize)
.copied()
.unwrap_or_else(|| self.mk_ty(Infer(ty::FreshTy(n))))
}
#[inline]
pub fn mk_fresh_int_ty(self, n: u32) -> Ty<'tcx> {
// Use a pre-interned one when possible.
self.types
.fresh_int_tys
.get(n as usize)
.copied()
.unwrap_or_else(|| self.mk_ty(Infer(ty::FreshIntTy(n))))
}
#[inline]
pub fn mk_fresh_float_ty(self, n: u32) -> Ty<'tcx> {
// Use a pre-interned one when possible.
self.types
.fresh_float_tys
.get(n as usize)
.copied()
.unwrap_or_else(|| self.mk_ty(Infer(ty::FreshFloatTy(n))))
}
#[inline]

View file

@ -1209,7 +1209,7 @@ pub trait PrettyPrinter<'tcx>:
// in order to place the projections inside the `<...>`.
if !resugared {
// Use a type that can't appear in defaults of type parameters.
let dummy_cx = cx.tcx().mk_ty_infer(ty::FreshTy(0));
let dummy_cx = cx.tcx().mk_fresh_ty(0);
let principal = principal.with_self_ty(cx.tcx(), dummy_cx);
let args = cx
@ -2696,7 +2696,7 @@ define_print_and_forward_display! {
ty::ExistentialTraitRef<'tcx> {
// Use a type that can't appear in defaults of type parameters.
let dummy_self = cx.tcx().mk_ty_infer(ty::FreshTy(0));
let dummy_self = cx.tcx().mk_fresh_ty(0);
let trait_ref = self.with_self_ty(cx.tcx(), dummy_self);
p!(print(trait_ref.print_only_trait_path()))
}