@@ -17,15 +17,16 @@ use rustc_middle::mir::mono::MonoItem;
1717use rustc_middle:: ty:: { self , Instance , ParamEnv , ScalarInt , Ty , TyCtxt , Variance } ;
1818use rustc_span:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
1919use 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 } ;
2122use stable_mir:: mir:: {
2223 Body , ConstOperand , CopyNonOverlapping , Statement , UserTypeProjection , VarDebugInfoFragment ,
2324 VariantIdx ,
2425} ;
2526use 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 ,
2930} ;
3031use stable_mir:: { self , opaque, Context , CrateItem , Error , Filename , ItemKind } ;
3132use std:: cell:: RefCell ;
@@ -318,6 +319,30 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
318319 . ok_or_else ( || Error :: new ( format ! ( "Const `{cnst:?}` cannot be encoded as u64" ) ) )
319320 }
320321
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+
321346 fn usize_to_const ( & self , val : u64 ) -> Result < Const , Error > {
322347 let mut tables = self . 0 . borrow_mut ( ) ;
323348 let ty = tables. tcx . types . usize ;
@@ -342,7 +367,7 @@ pub(crate) struct TablesWrapper<'tcx>(pub(crate) RefCell<Tables<'tcx>>);
342367pub struct Tables < ' tcx > {
343368 pub ( crate ) tcx : TyCtxt < ' tcx > ,
344369 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 > ,
346371 pub ( crate ) spans : IndexMap < rustc_span:: Span , Span > ,
347372 pub ( crate ) types : IndexMap < Ty < ' tcx > , stable_mir:: ty:: Ty > ,
348373 pub ( crate ) instances : IndexMap < ty:: Instance < ' tcx > , InstanceDef > ,
@@ -1590,6 +1615,14 @@ impl<'tcx> Stable<'tcx> for ty::BoundTy {
15901615 }
15911616}
15921617
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+
15931626impl < ' tcx > Stable < ' tcx > for mir:: interpret:: Allocation {
15941627 type T = stable_mir:: ty:: Allocation ;
15951628
@@ -1602,6 +1635,32 @@ impl<'tcx> Stable<'tcx> for mir::interpret::Allocation {
16021635 }
16031636}
16041637
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+
16051664impl < ' tcx > Stable < ' tcx > for ty:: trait_def:: TraitSpecializationKind {
16061665 type T = stable_mir:: ty:: TraitSpecializationKind ;
16071666 fn stable ( & self , _: & mut Tables < ' tcx > ) -> Self :: T {
@@ -1989,6 +2048,14 @@ impl<'tcx> Stable<'tcx> for MonoItem<'tcx> {
19892048 }
19902049}
19912050
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+
19922059impl < ' tcx , T > Stable < ' tcx > for & T
19932060where
19942061 T : Stable < ' tcx > ,
@@ -2010,3 +2077,18 @@ where
20102077 self . as_ref ( ) . map ( |value| value. stable ( tables) )
20112078 }
20122079}
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