@@ -241,6 +241,11 @@ impl<'tcx> CtxtInterners<'tcx> {
241
241
}
242
242
}
243
243
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
+
244
249
pub struct CommonTypes < ' tcx > {
245
250
pub unit : Ty < ' tcx > ,
246
251
pub bool : Ty < ' tcx > ,
@@ -266,7 +271,20 @@ pub struct CommonTypes<'tcx> {
266
271
/// Dummy type used for the `Self` of a `TraitRef` created for converting
267
272
/// a trait object, and which gets removed in `ExistentialTraitRef`.
268
273
/// This type must not appear anywhere in other converted types.
274
+ /// `Infer(ty::FreshTy(0))` does the job.
269
275
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 > > ,
270
288
}
271
289
272
290
pub struct CommonLifetimes < ' tcx > {
@@ -289,6 +307,15 @@ impl<'tcx> CommonTypes<'tcx> {
289
307
) -> CommonTypes < ' tcx > {
290
308
let mk = |ty| interners. intern_ty ( ty, sess, untracked) ;
291
309
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
+
292
319
CommonTypes {
293
320
unit : mk ( Tuple ( List :: empty ( ) ) ) ,
294
321
bool : mk ( Bool ) ,
@@ -311,7 +338,12 @@ impl<'tcx> CommonTypes<'tcx> {
311
338
str_ : mk ( Str ) ,
312
339
self_param : mk ( ty:: Param ( ty:: ParamTy { index : 0 , name : kw:: SelfUpper } ) ) ,
313
340
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,
315
347
}
316
348
}
317
349
}
@@ -1868,28 +1900,54 @@ impl<'tcx> TyCtxt<'tcx> {
1868
1900
}
1869
1901
1870
1902
#[ 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 } )
1873
1905
}
1874
1906
1875
1907
#[ 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) ) ) )
1878
1911
}
1879
1912
1880
1913
#[ inline]
1881
1914
pub fn mk_int_var ( self , v : IntVid ) -> Ty < ' tcx > {
1882
- self . mk_ty_infer ( IntVar ( v) )
1915
+ self . mk_ty ( Infer ( IntVar ( v) ) )
1883
1916
}
1884
1917
1885
1918
#[ inline]
1886
1919
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) ) ) )
1888
1941
}
1889
1942
1890
1943
#[ 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) ) ) )
1893
1951
}
1894
1952
1895
1953
#[ inline]
0 commit comments