@@ -17,15 +17,16 @@ use rustc_middle::mir::mono::MonoItem;
17
17
use rustc_middle:: ty:: { self , Instance , ParamEnv , ScalarInt , Ty , TyCtxt , Variance } ;
18
18
use rustc_span:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
19
19
use rustc_target:: abi:: FieldIdx ;
20
- use stable_mir:: mir:: mono:: InstanceDef ;
20
+ use stable_mir:: mir:: alloc:: GlobalAlloc ;
21
+ use stable_mir:: mir:: mono:: { InstanceDef , StaticDef } ;
21
22
use stable_mir:: mir:: {
22
23
Body , ConstOperand , CopyNonOverlapping , Statement , UserTypeProjection , VarDebugInfoFragment ,
23
24
VariantIdx ,
24
25
} ;
25
26
use stable_mir:: ty:: {
26
- AdtDef , AdtKind , ClosureDef , ClosureKind , Const , ConstId , ConstantKind , EarlyParamRegion ,
27
- FloatTy , FnDef , GenericArgs , GenericParamDef , IntTy , LineInfo , Movability , RigidTy , Span ,
28
- TyKind , UintTy ,
27
+ AdtDef , AdtKind , Allocation , ClosureDef , ClosureKind , Const , ConstId , ConstantKind ,
28
+ EarlyParamRegion , FloatTy , FnDef , GenericArgs , GenericParamDef , IntTy , LineInfo , Movability ,
29
+ RigidTy , Span , TyKind , UintTy ,
29
30
} ;
30
31
use stable_mir:: { self , opaque, Context , CrateItem , Error , Filename , ItemKind } ;
31
32
use std:: cell:: RefCell ;
@@ -318,6 +319,30 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
318
319
. ok_or_else ( || Error :: new ( format ! ( "Const `{cnst:?}` cannot be encoded as u64" ) ) )
319
320
}
320
321
322
+ fn eval_static_initializer ( & self , def : StaticDef ) -> Result < Allocation , Error > {
323
+ let mut tables = self . 0 . borrow_mut ( ) ;
324
+ let def_id = def. 0 . internal ( & mut * tables) ;
325
+ tables. tcx . eval_static_initializer ( def_id) . stable ( & mut * tables)
326
+ }
327
+
328
+ fn global_alloc ( & self , alloc : stable_mir:: mir:: alloc:: AllocId ) -> GlobalAlloc {
329
+ let mut tables = self . 0 . borrow_mut ( ) ;
330
+ let alloc_id = alloc. internal ( & mut * tables) ;
331
+ tables. tcx . global_alloc ( alloc_id) . stable ( & mut * tables)
332
+ }
333
+
334
+ fn vtable_allocation (
335
+ & self ,
336
+ global_alloc : & GlobalAlloc ,
337
+ ) -> Option < stable_mir:: mir:: alloc:: AllocId > {
338
+ let mut tables = self . 0 . borrow_mut ( ) ;
339
+ let GlobalAlloc :: VTable ( ty, trait_ref) = global_alloc else { return None } ;
340
+ let alloc_id = tables
341
+ . tcx
342
+ . vtable_allocation ( ( ty. internal ( & mut * tables) , trait_ref. internal ( & mut * tables) ) ) ;
343
+ Some ( alloc_id. stable ( & mut * tables) )
344
+ }
345
+
321
346
fn usize_to_const ( & self , val : u64 ) -> Result < Const , Error > {
322
347
let mut tables = self . 0 . borrow_mut ( ) ;
323
348
let ty = tables. tcx . types . usize ;
@@ -342,7 +367,7 @@ pub(crate) struct TablesWrapper<'tcx>(pub(crate) RefCell<Tables<'tcx>>);
342
367
pub struct Tables < ' tcx > {
343
368
pub ( crate ) tcx : TyCtxt < ' tcx > ,
344
369
pub ( crate ) def_ids : IndexMap < DefId , stable_mir:: DefId > ,
345
- pub ( crate ) alloc_ids : IndexMap < AllocId , stable_mir:: AllocId > ,
370
+ pub ( crate ) alloc_ids : IndexMap < AllocId , stable_mir:: mir :: alloc :: AllocId > ,
346
371
pub ( crate ) spans : IndexMap < rustc_span:: Span , Span > ,
347
372
pub ( crate ) types : IndexMap < Ty < ' tcx > , stable_mir:: ty:: Ty > ,
348
373
pub ( crate ) instances : IndexMap < ty:: Instance < ' tcx > , InstanceDef > ,
@@ -1590,6 +1615,14 @@ impl<'tcx> Stable<'tcx> for ty::BoundTy {
1590
1615
}
1591
1616
}
1592
1617
1618
+ impl < ' tcx > Stable < ' tcx > for mir:: interpret:: ConstAllocation < ' tcx > {
1619
+ type T = Allocation ;
1620
+
1621
+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1622
+ self . inner ( ) . stable ( tables)
1623
+ }
1624
+ }
1625
+
1593
1626
impl < ' tcx > Stable < ' tcx > for mir:: interpret:: Allocation {
1594
1627
type T = stable_mir:: ty:: Allocation ;
1595
1628
@@ -1602,6 +1635,32 @@ impl<'tcx> Stable<'tcx> for mir::interpret::Allocation {
1602
1635
}
1603
1636
}
1604
1637
1638
+ impl < ' tcx > Stable < ' tcx > for mir:: interpret:: AllocId {
1639
+ type T = stable_mir:: mir:: alloc:: AllocId ;
1640
+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1641
+ tables. create_alloc_id ( * self )
1642
+ }
1643
+ }
1644
+
1645
+ impl < ' tcx > Stable < ' tcx > for mir:: interpret:: GlobalAlloc < ' tcx > {
1646
+ type T = GlobalAlloc ;
1647
+
1648
+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1649
+ match self {
1650
+ mir:: interpret:: GlobalAlloc :: Function ( instance) => {
1651
+ GlobalAlloc :: Function ( instance. stable ( tables) )
1652
+ }
1653
+ mir:: interpret:: GlobalAlloc :: VTable ( ty, trait_ref) => {
1654
+ GlobalAlloc :: VTable ( ty. stable ( tables) , trait_ref. stable ( tables) )
1655
+ }
1656
+ mir:: interpret:: GlobalAlloc :: Static ( def) => {
1657
+ GlobalAlloc :: Static ( tables. static_def ( * def) )
1658
+ }
1659
+ mir:: interpret:: GlobalAlloc :: Memory ( alloc) => GlobalAlloc :: Memory ( alloc. stable ( tables) ) ,
1660
+ }
1661
+ }
1662
+ }
1663
+
1605
1664
impl < ' tcx > Stable < ' tcx > for ty:: trait_def:: TraitSpecializationKind {
1606
1665
type T = stable_mir:: ty:: TraitSpecializationKind ;
1607
1666
fn stable ( & self , _: & mut Tables < ' tcx > ) -> Self :: T {
@@ -1989,6 +2048,14 @@ impl<'tcx> Stable<'tcx> for MonoItem<'tcx> {
1989
2048
}
1990
2049
}
1991
2050
2051
+ impl < ' tcx > Stable < ' tcx > for mir:: interpret:: ErrorHandled {
2052
+ type T = Error ;
2053
+
2054
+ fn stable ( & self , _tables : & mut Tables < ' tcx > ) -> Self :: T {
2055
+ Error :: new ( format ! ( "{self:?}" ) )
2056
+ }
2057
+ }
2058
+
1992
2059
impl < ' tcx , T > Stable < ' tcx > for & T
1993
2060
where
1994
2061
T : Stable < ' tcx > ,
@@ -2010,3 +2077,18 @@ where
2010
2077
self . as_ref ( ) . map ( |value| value. stable ( tables) )
2011
2078
}
2012
2079
}
2080
+
2081
+ impl < ' tcx , T , E > Stable < ' tcx > for Result < T , E >
2082
+ where
2083
+ T : Stable < ' tcx > ,
2084
+ E : Stable < ' tcx > ,
2085
+ {
2086
+ type T = Result < T :: T , E :: T > ;
2087
+
2088
+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
2089
+ match self {
2090
+ Ok ( val) => Ok ( val. stable ( tables) ) ,
2091
+ Err ( error) => Err ( error. stable ( tables) ) ,
2092
+ }
2093
+ }
2094
+ }
0 commit comments