@@ -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,18 @@ 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+
321334 fn usize_to_const ( & self , val : u64 ) -> Result < Const , Error > {
322335 let mut tables = self . 0 . borrow_mut ( ) ;
323336 let ty = tables. tcx . types . usize ;
@@ -342,7 +355,7 @@ pub(crate) struct TablesWrapper<'tcx>(pub(crate) RefCell<Tables<'tcx>>);
342355pub struct Tables < ' tcx > {
343356 pub ( crate ) tcx : TyCtxt < ' tcx > ,
344357 pub ( crate ) def_ids : IndexMap < DefId , stable_mir:: DefId > ,
345- pub ( crate ) alloc_ids : IndexMap < AllocId , stable_mir:: AllocId > ,
358+ pub ( crate ) alloc_ids : IndexMap < AllocId , stable_mir:: mir :: alloc :: AllocId > ,
346359 pub ( crate ) spans : IndexMap < rustc_span:: Span , Span > ,
347360 pub ( crate ) types : IndexMap < Ty < ' tcx > , stable_mir:: ty:: Ty > ,
348361 pub ( crate ) instances : IndexMap < ty:: Instance < ' tcx > , InstanceDef > ,
@@ -1590,6 +1603,14 @@ impl<'tcx> Stable<'tcx> for ty::BoundTy {
15901603 }
15911604}
15921605
1606+ impl < ' tcx > Stable < ' tcx > for mir:: interpret:: ConstAllocation < ' tcx > {
1607+ type T = Allocation ;
1608+
1609+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1610+ self . inner ( ) . stable ( tables)
1611+ }
1612+ }
1613+
15931614impl < ' tcx > Stable < ' tcx > for mir:: interpret:: Allocation {
15941615 type T = stable_mir:: ty:: Allocation ;
15951616
@@ -1602,6 +1623,25 @@ impl<'tcx> Stable<'tcx> for mir::interpret::Allocation {
16021623 }
16031624}
16041625
1626+ impl < ' tcx > Stable < ' tcx > for mir:: interpret:: GlobalAlloc < ' tcx > {
1627+ type T = GlobalAlloc ;
1628+
1629+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1630+ match self {
1631+ mir:: interpret:: GlobalAlloc :: Function ( instance) => {
1632+ GlobalAlloc :: Function ( instance. stable ( tables) )
1633+ }
1634+ mir:: interpret:: GlobalAlloc :: VTable ( ty, trait_ref) => {
1635+ GlobalAlloc :: VTable ( ty. stable ( tables) , trait_ref. stable ( tables) )
1636+ }
1637+ mir:: interpret:: GlobalAlloc :: Static ( def) => {
1638+ GlobalAlloc :: Static ( tables. static_def ( * def) )
1639+ }
1640+ mir:: interpret:: GlobalAlloc :: Memory ( alloc) => GlobalAlloc :: Memory ( alloc. stable ( tables) ) ,
1641+ }
1642+ }
1643+ }
1644+
16051645impl < ' tcx > Stable < ' tcx > for ty:: trait_def:: TraitSpecializationKind {
16061646 type T = stable_mir:: ty:: TraitSpecializationKind ;
16071647 fn stable ( & self , _: & mut Tables < ' tcx > ) -> Self :: T {
@@ -1989,6 +2029,14 @@ impl<'tcx> Stable<'tcx> for MonoItem<'tcx> {
19892029 }
19902030}
19912031
2032+ impl < ' tcx > Stable < ' tcx > for mir:: interpret:: ErrorHandled {
2033+ type T = Error ;
2034+
2035+ fn stable ( & self , _tables : & mut Tables < ' tcx > ) -> Self :: T {
2036+ Error :: new ( format ! ( "{self:?}" ) )
2037+ }
2038+ }
2039+
19922040impl < ' tcx , T > Stable < ' tcx > for & T
19932041where
19942042 T : Stable < ' tcx > ,
@@ -2010,3 +2058,18 @@ where
20102058 self . as_ref ( ) . map ( |value| value. stable ( tables) )
20112059 }
20122060}
2061+
2062+ impl < ' tcx , T , E > Stable < ' tcx > for Result < T , E >
2063+ where
2064+ T : Stable < ' tcx > ,
2065+ E : Stable < ' tcx > ,
2066+ {
2067+ type T = Result < T :: T , E :: T > ;
2068+
2069+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
2070+ match self {
2071+ Ok ( val) => Ok ( val. stable ( tables) ) ,
2072+ Err ( error) => Err ( error. stable ( tables) ) ,
2073+ }
2074+ }
2075+ }
0 commit comments