@@ -20,7 +20,8 @@ use rustc_target::abi::FieldIdx;
20
20
use stable_mir:: mir:: mono:: InstanceDef ;
21
21
use stable_mir:: mir:: { Body , CopyNonOverlapping , Statement , UserTypeProjection , VariantIdx } ;
22
22
use stable_mir:: ty:: {
23
- FloatTy , GenericParamDef , IntTy , LineInfo , Movability , RigidTy , Span , TyKind , UintTy ,
23
+ Const , ConstId , ConstantKind , FloatTy , GenericParamDef , IntTy , LineInfo , Movability , RigidTy ,
24
+ Span , TyKind , UintTy ,
24
25
} ;
25
26
use stable_mir:: { self , opaque, Context , Filename } ;
26
27
use std:: cell:: RefCell ;
@@ -147,14 +148,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
147
148
148
149
fn ty_kind ( & self , ty : stable_mir:: ty:: Ty ) -> TyKind {
149
150
let mut tables = self . 0 . borrow_mut ( ) ;
150
- tables. types [ ty. 0 ] . clone ( ) . stable ( & mut * tables)
151
- }
152
-
153
- fn mk_ty ( & self , kind : TyKind ) -> stable_mir:: ty:: Ty {
154
- let mut tables = self . 0 . borrow_mut ( ) ;
155
- let n = tables. types . len ( ) ;
156
- tables. types . push ( MaybeStable :: Stable ( kind) ) ;
157
- stable_mir:: ty:: Ty ( n)
151
+ tables. types [ ty] . kind ( ) . stable ( & mut * tables)
158
152
}
159
153
160
154
fn generics_of ( & self , def_id : stable_mir:: DefId ) -> stable_mir:: ty:: Generics {
@@ -213,8 +207,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
213
207
fn instance_ty ( & self , def : InstanceDef ) -> stable_mir:: ty:: Ty {
214
208
let mut tables = self . 0 . borrow_mut ( ) ;
215
209
let instance = tables. instances [ def] ;
216
- let ty = instance. ty ( tables. tcx , ParamEnv :: empty ( ) ) ;
217
- tables. intern_ty ( ty)
210
+ instance. ty ( tables. tcx , ParamEnv :: empty ( ) ) . stable ( & mut * tables)
218
211
}
219
212
220
213
fn instance_def_id ( & self , def : InstanceDef ) -> stable_mir:: DefId {
@@ -252,52 +245,25 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
252
245
}
253
246
}
254
247
255
- #[ derive( Clone ) ]
256
- pub enum MaybeStable < S , R > {
257
- Stable ( S ) ,
258
- Rustc ( R ) ,
259
- }
260
-
261
- impl < ' tcx , S , R > MaybeStable < S , R > {
262
- fn stable ( self , tables : & mut Tables < ' tcx > ) -> S
263
- where
264
- R : Stable < ' tcx , T = S > ,
265
- {
266
- match self {
267
- MaybeStable :: Stable ( s) => s,
268
- MaybeStable :: Rustc ( r) => r. stable ( tables) ,
269
- }
270
- }
271
- }
272
-
273
- impl < S , R : PartialEq > PartialEq < R > for MaybeStable < S , R > {
274
- fn eq ( & self , other : & R ) -> bool {
275
- match self {
276
- MaybeStable :: Stable ( _) => false ,
277
- MaybeStable :: Rustc ( r) => r == other,
278
- }
279
- }
280
- }
281
-
282
248
pub ( crate ) struct TablesWrapper < ' tcx > ( pub ( crate ) RefCell < Tables < ' tcx > > ) ;
283
249
284
250
pub struct Tables < ' tcx > {
285
251
pub ( crate ) tcx : TyCtxt < ' tcx > ,
286
252
pub ( crate ) def_ids : IndexMap < DefId , stable_mir:: DefId > ,
287
253
pub ( crate ) alloc_ids : IndexMap < AllocId , stable_mir:: AllocId > ,
288
254
pub ( crate ) spans : IndexMap < rustc_span:: Span , Span > ,
289
- pub ( crate ) types : Vec < MaybeStable < TyKind , Ty < ' tcx > > > ,
255
+ pub ( crate ) types : IndexMap < Ty < ' tcx > , stable_mir :: ty :: Ty > ,
290
256
pub ( crate ) instances : IndexMap < ty:: Instance < ' tcx > , InstanceDef > ,
257
+ pub ( crate ) constants : IndexMap < mir:: Const < ' tcx > , ConstId > ,
291
258
}
292
259
293
260
impl < ' tcx > Tables < ' tcx > {
294
261
fn intern_ty ( & mut self , ty : Ty < ' tcx > ) -> stable_mir:: ty:: Ty {
295
- if let Some ( id) = self . types . iter ( ) . position ( |t| * t == ty) {
296
- return stable_mir:: ty:: Ty ( id) ;
297
- }
298
- let id = self . types . len ( ) ;
299
- self . types . push ( MaybeStable :: Rustc ( ty) ) ;
300
- stable_mir:: ty:: Ty ( id)
262
+ self . types . create_or_fetch ( ty)
263
+ }
264
+
265
+ fn intern_const ( & mut self , constant : mir:: Const < ' tcx > ) -> ConstId {
266
+ self . constants . create_or_fetch ( constant)
301
267
}
302
268
}
303
269
@@ -338,7 +304,7 @@ impl<'tcx> Stable<'tcx> for mir::Body<'tcx> {
338
304
. local_decls
339
305
. iter ( )
340
306
. map ( |decl| stable_mir:: mir:: LocalDecl {
341
- ty : tables . intern_ty ( decl. ty ) ,
307
+ ty : decl. ty . stable ( tables ) ,
342
308
span : decl. source_info . span . stable ( tables) ,
343
309
} )
344
310
. collect ( ) ,
@@ -436,7 +402,7 @@ impl<'tcx> Stable<'tcx> for mir::Rvalue<'tcx> {
436
402
Cast ( cast_kind, op, ty) => stable_mir:: mir:: Rvalue :: Cast (
437
403
cast_kind. stable ( tables) ,
438
404
op. stable ( tables) ,
439
- tables . intern_ty ( * ty ) ,
405
+ ty . stable ( tables ) ,
440
406
) ,
441
407
BinaryOp ( bin_op, ops) => stable_mir:: mir:: Rvalue :: BinaryOp (
442
408
bin_op. stable ( tables) ,
@@ -449,7 +415,7 @@ impl<'tcx> Stable<'tcx> for mir::Rvalue<'tcx> {
449
415
ops. 1 . stable ( tables) ,
450
416
) ,
451
417
NullaryOp ( null_op, ty) => {
452
- stable_mir:: mir:: Rvalue :: NullaryOp ( null_op. stable ( tables) , tables . intern_ty ( * ty ) )
418
+ stable_mir:: mir:: Rvalue :: NullaryOp ( null_op. stable ( tables) , ty . stable ( tables ) )
453
419
}
454
420
UnaryOp ( un_op, op) => {
455
421
stable_mir:: mir:: Rvalue :: UnaryOp ( un_op. stable ( tables) , op. stable ( tables) )
@@ -460,7 +426,7 @@ impl<'tcx> Stable<'tcx> for mir::Rvalue<'tcx> {
460
426
stable_mir:: mir:: Rvalue :: Aggregate ( agg_kind. stable ( tables) , operands)
461
427
}
462
428
ShallowInitBox ( op, ty) => {
463
- stable_mir:: mir:: Rvalue :: ShallowInitBox ( op. stable ( tables) , tables . intern_ty ( * ty ) )
429
+ stable_mir:: mir:: Rvalue :: ShallowInitBox ( op. stable ( tables) , ty . stable ( tables ) )
464
430
}
465
431
CopyForDeref ( place) => stable_mir:: mir:: Rvalue :: CopyForDeref ( place. stable ( tables) ) ,
466
432
}
@@ -604,7 +570,7 @@ impl<'tcx> Stable<'tcx> for ty::TermKind<'tcx> {
604
570
fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
605
571
use stable_mir:: ty:: TermKind ;
606
572
match self {
607
- ty:: TermKind :: Ty ( ty) => TermKind :: Type ( tables . intern_ty ( * ty ) ) ,
573
+ ty:: TermKind :: Ty ( ty) => TermKind :: Type ( ty . stable ( tables ) ) ,
608
574
ty:: TermKind :: Const ( cnst) => {
609
575
let cnst = cnst. stable ( tables) ;
610
576
TermKind :: Const ( cnst)
@@ -885,7 +851,7 @@ impl<'tcx> Stable<'tcx> for mir::AggregateKind<'tcx> {
885
851
fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
886
852
match self {
887
853
mir:: AggregateKind :: Array ( ty) => {
888
- stable_mir:: mir:: AggregateKind :: Array ( tables . intern_ty ( * ty ) )
854
+ stable_mir:: mir:: AggregateKind :: Array ( ty . stable ( tables ) )
889
855
}
890
856
mir:: AggregateKind :: Tuple => stable_mir:: mir:: AggregateKind :: Tuple ,
891
857
mir:: AggregateKind :: Adt ( def_id, var_idx, generic_arg, user_ty_index, field_idx) => {
@@ -1053,7 +1019,7 @@ impl<'tcx> Stable<'tcx> for ty::GenericArgKind<'tcx> {
1053
1019
use stable_mir:: ty:: GenericArgKind ;
1054
1020
match self {
1055
1021
ty:: GenericArgKind :: Lifetime ( region) => GenericArgKind :: Lifetime ( region. stable ( tables) ) ,
1056
- ty:: GenericArgKind :: Type ( ty) => GenericArgKind :: Type ( tables . intern_ty ( * ty ) ) ,
1022
+ ty:: GenericArgKind :: Type ( ty) => GenericArgKind :: Type ( ty . stable ( tables ) ) ,
1057
1023
ty:: GenericArgKind :: Const ( cnst) => GenericArgKind :: Const ( cnst. stable ( tables) ) ,
1058
1024
}
1059
1025
}
@@ -1099,11 +1065,7 @@ impl<'tcx> Stable<'tcx> for ty::FnSig<'tcx> {
1099
1065
use stable_mir:: ty:: { Abi , FnSig } ;
1100
1066
1101
1067
FnSig {
1102
- inputs_and_output : self
1103
- . inputs_and_output
1104
- . iter ( )
1105
- . map ( |ty| tables. intern_ty ( ty) )
1106
- . collect ( ) ,
1068
+ inputs_and_output : self . inputs_and_output . iter ( ) . map ( |ty| ty. stable ( tables) ) . collect ( ) ,
1107
1069
c_variadic : self . c_variadic ,
1108
1070
unsafety : self . unsafety . stable ( tables) ,
1109
1071
abi : match self . abi {
@@ -1241,9 +1203,16 @@ impl<'tcx> Stable<'tcx> for hir::Movability {
1241
1203
}
1242
1204
1243
1205
impl < ' tcx > Stable < ' tcx > for Ty < ' tcx > {
1206
+ type T = stable_mir:: ty:: Ty ;
1207
+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1208
+ tables. intern_ty ( * self )
1209
+ }
1210
+ }
1211
+
1212
+ impl < ' tcx > Stable < ' tcx > for ty:: TyKind < ' tcx > {
1244
1213
type T = stable_mir:: ty:: TyKind ;
1245
1214
fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1246
- match self . kind ( ) {
1215
+ match self {
1247
1216
ty:: Bool => TyKind :: RigidTy ( RigidTy :: Bool ) ,
1248
1217
ty:: Char => TyKind :: RigidTy ( RigidTy :: Char ) ,
1249
1218
ty:: Int ( int_ty) => TyKind :: RigidTy ( RigidTy :: Int ( int_ty. stable ( tables) ) ) ,
@@ -1256,15 +1225,15 @@ impl<'tcx> Stable<'tcx> for Ty<'tcx> {
1256
1225
ty:: Foreign ( def_id) => TyKind :: RigidTy ( RigidTy :: Foreign ( tables. foreign_def ( * def_id) ) ) ,
1257
1226
ty:: Str => TyKind :: RigidTy ( RigidTy :: Str ) ,
1258
1227
ty:: Array ( ty, constant) => {
1259
- TyKind :: RigidTy ( RigidTy :: Array ( tables . intern_ty ( * ty ) , constant. stable ( tables) ) )
1228
+ TyKind :: RigidTy ( RigidTy :: Array ( ty . stable ( tables ) , constant. stable ( tables) ) )
1260
1229
}
1261
- ty:: Slice ( ty) => TyKind :: RigidTy ( RigidTy :: Slice ( tables . intern_ty ( * ty ) ) ) ,
1230
+ ty:: Slice ( ty) => TyKind :: RigidTy ( RigidTy :: Slice ( ty . stable ( tables ) ) ) ,
1262
1231
ty:: RawPtr ( ty:: TypeAndMut { ty, mutbl } ) => {
1263
- TyKind :: RigidTy ( RigidTy :: RawPtr ( tables . intern_ty ( * ty ) , mutbl. stable ( tables) ) )
1232
+ TyKind :: RigidTy ( RigidTy :: RawPtr ( ty . stable ( tables ) , mutbl. stable ( tables) ) )
1264
1233
}
1265
1234
ty:: Ref ( region, ty, mutbl) => TyKind :: RigidTy ( RigidTy :: Ref (
1266
1235
region. stable ( tables) ,
1267
- tables . intern_ty ( * ty ) ,
1236
+ ty . stable ( tables ) ,
1268
1237
mutbl. stable ( tables) ,
1269
1238
) ) ,
1270
1239
ty:: FnDef ( def_id, generic_args) => {
@@ -1291,9 +1260,9 @@ impl<'tcx> Stable<'tcx> for Ty<'tcx> {
1291
1260
movability. stable ( tables) ,
1292
1261
) ) ,
1293
1262
ty:: Never => TyKind :: RigidTy ( RigidTy :: Never ) ,
1294
- ty:: Tuple ( fields) => TyKind :: RigidTy ( RigidTy :: Tuple (
1295
- fields. iter ( ) . map ( |ty| tables . intern_ty ( ty ) ) . collect ( ) ,
1296
- ) ) ,
1263
+ ty:: Tuple ( fields) => {
1264
+ TyKind :: RigidTy ( RigidTy :: Tuple ( fields. iter ( ) . map ( |ty| ty . stable ( tables ) ) . collect ( ) ) )
1265
+ }
1297
1266
ty:: Alias ( alias_kind, alias_ty) => {
1298
1267
TyKind :: Alias ( alias_kind. stable ( tables) , alias_ty. stable ( tables) )
1299
1268
}
@@ -1312,32 +1281,32 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
1312
1281
type T = stable_mir:: ty:: Const ;
1313
1282
1314
1283
fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1315
- stable_mir :: ty :: Const {
1316
- literal : match self . kind ( ) {
1317
- ty :: Value ( val ) => {
1318
- let const_val = tables . tcx . valtree_to_const_val ( ( self . ty ( ) , val ) ) ;
1319
- stable_mir :: ty :: ConstantKind :: Allocated ( alloc :: new_allocation (
1320
- self . ty ( ) ,
1321
- const_val ,
1322
- tables ,
1323
- ) )
1324
- }
1325
- ty:: ParamCt ( param ) => stable_mir :: ty :: ConstantKind :: Param ( param . stable ( tables ) ) ,
1326
- ty:: ErrorCt ( _) => unreachable ! ( ) ,
1327
- ty:: InferCt ( _ ) => unreachable ! ( ) ,
1328
- ty:: BoundCt ( _ , _) => unimplemented ! ( ) ,
1329
- ty:: PlaceholderCt ( _ ) => unimplemented ! ( ) ,
1330
- ty:: Unevaluated ( uv ) => {
1331
- stable_mir :: ty :: ConstantKind :: Unevaluated ( stable_mir :: ty :: UnevaluatedConst {
1332
- def : tables . const_def ( uv. def ) ,
1333
- args : uv . args . stable ( tables ) ,
1334
- promoted : None ,
1335
- } )
1336
- }
1337
- ty :: ExprCt ( _ ) => unimplemented ! ( ) ,
1338
- } ,
1339
- ty : tables. intern_ty ( self . ty ( ) ) ,
1340
- }
1284
+ let kind = match self . kind ( ) {
1285
+ ty :: Value ( val ) => {
1286
+ let const_val = tables . tcx . valtree_to_const_val ( ( self . ty ( ) , val ) ) ;
1287
+ stable_mir :: ty :: ConstantKind :: Allocated ( alloc :: new_allocation (
1288
+ self . ty ( ) ,
1289
+ const_val ,
1290
+ tables ,
1291
+ ) )
1292
+ }
1293
+ ty :: ParamCt ( param ) => stable_mir :: ty :: ConstantKind :: Param ( param . stable ( tables ) ) ,
1294
+ ty:: ErrorCt ( _ ) => unreachable ! ( ) ,
1295
+ ty:: InferCt ( _) => unreachable ! ( ) ,
1296
+ ty:: BoundCt ( _ , _ ) => unimplemented ! ( ) ,
1297
+ ty:: PlaceholderCt ( _) => unimplemented ! ( ) ,
1298
+ ty:: Unevaluated ( uv ) => {
1299
+ stable_mir :: ty:: ConstantKind :: Unevaluated ( stable_mir :: ty :: UnevaluatedConst {
1300
+ def : tables . const_def ( uv . def ) ,
1301
+ args : uv. args . stable ( tables ) ,
1302
+ promoted : None ,
1303
+ } )
1304
+ }
1305
+ ty :: ExprCt ( _ ) => unimplemented ! ( ) ,
1306
+ } ;
1307
+ let ty = self . ty ( ) . stable ( tables ) ;
1308
+ let id = tables. intern_const ( mir :: Const :: Ty ( * self ) ) ;
1309
+ Const :: new ( kind , ty , id )
1341
1310
}
1342
1311
}
1343
1312
@@ -1422,22 +1391,23 @@ impl<'tcx> Stable<'tcx> for rustc_middle::mir::Const<'tcx> {
1422
1391
fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1423
1392
match * self {
1424
1393
mir:: Const :: Ty ( c) => c. stable ( tables) ,
1425
- mir:: Const :: Unevaluated ( unev_const, ty) => stable_mir :: ty :: Const {
1426
- literal : stable_mir :: ty :: ConstantKind :: Unevaluated (
1427
- stable_mir:: ty:: UnevaluatedConst {
1394
+ mir:: Const :: Unevaluated ( unev_const, ty) => {
1395
+ let kind =
1396
+ stable_mir:: ty:: ConstantKind :: Unevaluated ( stable_mir :: ty :: UnevaluatedConst {
1428
1397
def : tables. const_def ( unev_const. def ) ,
1429
1398
args : unev_const. args . stable ( tables) ,
1430
1399
promoted : unev_const. promoted . map ( |u| u. as_u32 ( ) ) ,
1431
- } ,
1432
- ) ,
1433
- ty : tables. intern_ty ( ty) ,
1434
- } ,
1435
- mir:: Const :: Val ( val, ty) => stable_mir:: ty:: Const {
1436
- literal : stable_mir:: ty:: ConstantKind :: Allocated ( alloc:: new_allocation (
1437
- ty, val, tables,
1438
- ) ) ,
1439
- ty : tables. intern_ty ( ty) ,
1440
- } ,
1400
+ } ) ;
1401
+ let ty = ty. stable ( tables) ;
1402
+ let id = tables. intern_const ( * self ) ;
1403
+ Const :: new ( kind, ty, id)
1404
+ }
1405
+ mir:: Const :: Val ( val, ty) => {
1406
+ let kind = ConstantKind :: Allocated ( alloc:: new_allocation ( ty, val, tables) ) ;
1407
+ let ty = ty. stable ( tables) ;
1408
+ let id = tables. intern_const ( * self ) ;
1409
+ Const :: new ( kind, ty, id)
1410
+ }
1441
1411
}
1442
1412
}
1443
1413
}
@@ -1562,7 +1532,7 @@ impl<'tcx> Stable<'tcx> for ty::ClauseKind<'tcx> {
1562
1532
ClauseKind :: TypeOutlives ( type_outlives) => {
1563
1533
let ty:: OutlivesPredicate :: < _ , _ > ( a, b) = type_outlives;
1564
1534
stable_mir:: ty:: ClauseKind :: TypeOutlives ( stable_mir:: ty:: OutlivesPredicate (
1565
- tables . intern_ty ( a ) ,
1535
+ a . stable ( tables ) ,
1566
1536
b. stable ( tables) ,
1567
1537
) )
1568
1538
}
@@ -1571,7 +1541,7 @@ impl<'tcx> Stable<'tcx> for ty::ClauseKind<'tcx> {
1571
1541
}
1572
1542
ClauseKind :: ConstArgHasType ( const_, ty) => stable_mir:: ty:: ClauseKind :: ConstArgHasType (
1573
1543
const_. stable ( tables) ,
1574
- tables . intern_ty ( ty ) ,
1544
+ ty . stable ( tables ) ,
1575
1545
) ,
1576
1546
ClauseKind :: WellFormed ( generic_arg) => {
1577
1547
stable_mir:: ty:: ClauseKind :: WellFormed ( generic_arg. unpack ( ) . stable ( tables) )
@@ -1601,7 +1571,7 @@ impl<'tcx> Stable<'tcx> for ty::SubtypePredicate<'tcx> {
1601
1571
1602
1572
fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1603
1573
let ty:: SubtypePredicate { a, b, a_is_expected : _ } = self ;
1604
- stable_mir:: ty:: SubtypePredicate { a : tables . intern_ty ( * a ) , b : tables . intern_ty ( * b ) }
1574
+ stable_mir:: ty:: SubtypePredicate { a : a . stable ( tables ) , b : b . stable ( tables ) }
1605
1575
}
1606
1576
}
1607
1577
@@ -1610,7 +1580,7 @@ impl<'tcx> Stable<'tcx> for ty::CoercePredicate<'tcx> {
1610
1580
1611
1581
fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1612
1582
let ty:: CoercePredicate { a, b } = self ;
1613
- stable_mir:: ty:: CoercePredicate { a : tables . intern_ty ( * a ) , b : tables . intern_ty ( * b ) }
1583
+ stable_mir:: ty:: CoercePredicate { a : a . stable ( tables ) , b : b . stable ( tables ) }
1614
1584
}
1615
1585
}
1616
1586
0 commit comments