@@ -241,6 +241,11 @@ impl<'tcx> CtxtInterners<'tcx> {
241241 }
242242}
243243
244+ const NUM_PREINTERNED_TY_VARS : u32 = 100 ;
245+ const NUM_PREINTERNED_FRESH_TYS : u32 = 20 ;
246+ const NUM_PREINTERNED_FRESH_INT_TYS : u32 = 3 ;
247+ const NUM_PREINTERNED_FRESH_FLOAT_TYS : u32 = 3 ;
248+
244249pub struct CommonTypes < ' tcx > {
245250 pub unit : Ty < ' tcx > ,
246251 pub bool : Ty < ' tcx > ,
@@ -266,7 +271,20 @@ pub struct CommonTypes<'tcx> {
266271 /// Dummy type used for the `Self` of a `TraitRef` created for converting
267272 /// a trait object, and which gets removed in `ExistentialTraitRef`.
268273 /// This type must not appear anywhere in other converted types.
274+ /// `Infer(ty::FreshTy(0))` does the job.
269275 pub trait_object_dummy_self : Ty < ' tcx > ,
276+
277+ /// Pre-interned `Infer(ty::TyVar(n))` for small values of `n`.
278+ pub ty_vars : Vec < Ty < ' tcx > > ,
279+
280+ /// Pre-interned `Infer(ty::FreshTy(n))` for small values of `n`.
281+ pub fresh_tys : Vec < Ty < ' tcx > > ,
282+
283+ /// Pre-interned `Infer(ty::FreshIntTy(n))` for small values of `n`.
284+ pub fresh_int_tys : Vec < Ty < ' tcx > > ,
285+
286+ /// Pre-interned `Infer(ty::FreshFloatTy(n))` for small values of `n`.
287+ pub fresh_float_tys : Vec < Ty < ' tcx > > ,
270288}
271289
272290pub struct CommonLifetimes < ' tcx > {
@@ -289,6 +307,15 @@ impl<'tcx> CommonTypes<'tcx> {
289307 ) -> CommonTypes < ' tcx > {
290308 let mk = |ty| interners. intern_ty ( ty, sess, untracked) ;
291309
310+ let ty_vars =
311+ ( 0 ..NUM_PREINTERNED_TY_VARS ) . map ( |n| mk ( Infer ( ty:: TyVar ( TyVid :: from ( n) ) ) ) ) . collect ( ) ;
312+ let fresh_tys: Vec < _ > =
313+ ( 0 ..NUM_PREINTERNED_FRESH_TYS ) . map ( |n| mk ( Infer ( ty:: FreshTy ( n) ) ) ) . collect ( ) ;
314+ let fresh_int_tys: Vec < _ > =
315+ ( 0 ..NUM_PREINTERNED_FRESH_INT_TYS ) . map ( |n| mk ( Infer ( ty:: FreshIntTy ( n) ) ) ) . collect ( ) ;
316+ let fresh_float_tys: Vec < _ > =
317+ ( 0 ..NUM_PREINTERNED_FRESH_FLOAT_TYS ) . map ( |n| mk ( Infer ( ty:: FreshFloatTy ( n) ) ) ) . collect ( ) ;
318+
292319 CommonTypes {
293320 unit : mk ( Tuple ( List :: empty ( ) ) ) ,
294321 bool : mk ( Bool ) ,
@@ -311,7 +338,12 @@ impl<'tcx> CommonTypes<'tcx> {
311338 str_ : mk ( Str ) ,
312339 self_param : mk ( ty:: Param ( ty:: ParamTy { index : 0 , name : kw:: SelfUpper } ) ) ,
313340
314- trait_object_dummy_self : mk ( Infer ( ty:: FreshTy ( 0 ) ) ) ,
341+ trait_object_dummy_self : fresh_tys[ 0 ] ,
342+
343+ ty_vars,
344+ fresh_tys,
345+ fresh_int_tys,
346+ fresh_float_tys,
315347 }
316348 }
317349}
@@ -1868,28 +1900,54 @@ impl<'tcx> TyCtxt<'tcx> {
18681900 }
18691901
18701902 #[ inline]
1871- pub fn mk_ty_var ( self , v : TyVid ) -> Ty < ' tcx > {
1872- self . mk_ty_infer ( TyVar ( v ) )
1903+ pub fn mk_const ( self , kind : impl Into < ty :: ConstKind < ' tcx > > , ty : Ty < ' tcx > ) -> Const < ' tcx > {
1904+ self . mk_const_internal ( ty :: ConstData { kind : kind . into ( ) , ty } )
18731905 }
18741906
18751907 #[ inline]
1876- pub fn mk_const ( self , kind : impl Into < ty:: ConstKind < ' tcx > > , ty : Ty < ' tcx > ) -> Const < ' tcx > {
1877- self . mk_const_internal ( ty:: ConstData { kind : kind. into ( ) , ty } )
1908+ pub fn mk_ty_var ( self , v : TyVid ) -> Ty < ' tcx > {
1909+ // Use a pre-interned one when possible.
1910+ self . types . ty_vars . get ( v. as_usize ( ) ) . copied ( ) . unwrap_or_else ( || self . mk_ty ( Infer ( TyVar ( v) ) ) )
18781911 }
18791912
18801913 #[ inline]
18811914 pub fn mk_int_var ( self , v : IntVid ) -> Ty < ' tcx > {
1882- self . mk_ty_infer ( IntVar ( v) )
1915+ self . mk_ty ( Infer ( IntVar ( v) ) )
18831916 }
18841917
18851918 #[ inline]
18861919 pub fn mk_float_var ( self , v : FloatVid ) -> Ty < ' tcx > {
1887- self . mk_ty_infer ( FloatVar ( v) )
1920+ self . mk_ty ( Infer ( FloatVar ( v) ) )
1921+ }
1922+
1923+ #[ inline]
1924+ pub fn mk_fresh_ty ( self , n : u32 ) -> Ty < ' tcx > {
1925+ // Use a pre-interned one when possible.
1926+ self . types
1927+ . fresh_tys
1928+ . get ( n as usize )
1929+ . copied ( )
1930+ . unwrap_or_else ( || self . mk_ty ( Infer ( ty:: FreshTy ( n) ) ) )
1931+ }
1932+
1933+ #[ inline]
1934+ pub fn mk_fresh_int_ty ( self , n : u32 ) -> Ty < ' tcx > {
1935+ // Use a pre-interned one when possible.
1936+ self . types
1937+ . fresh_int_tys
1938+ . get ( n as usize )
1939+ . copied ( )
1940+ . unwrap_or_else ( || self . mk_ty ( Infer ( ty:: FreshIntTy ( n) ) ) )
18881941 }
18891942
18901943 #[ inline]
1891- pub fn mk_ty_infer ( self , it : InferTy ) -> Ty < ' tcx > {
1892- self . mk_ty ( Infer ( it) )
1944+ pub fn mk_fresh_float_ty ( self , n : u32 ) -> Ty < ' tcx > {
1945+ // Use a pre-interned one when possible.
1946+ self . types
1947+ . fresh_float_tys
1948+ . get ( n as usize )
1949+ . copied ( )
1950+ . unwrap_or_else ( || self . mk_ty ( Infer ( ty:: FreshFloatTy ( n) ) ) )
18931951 }
18941952
18951953 #[ inline]
0 commit comments