@@ -17,12 +17,13 @@ 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:: { Body , CopyNonOverlapping , Statement , UserTypeProjection , VariantIdx } ;
2223use stable_mir:: ty:: {
23- AdtDef , AdtKind , ClosureDef , ClosureKind , Const , ConstId , ConstantKind , EarlyParamRegion ,
24- FloatTy , FnDef , GenericArgs , GenericParamDef , IntTy , LineInfo , Movability , RigidTy , Span ,
25- TyKind , UintTy ,
24+ AdtDef , AdtKind , Allocation , ClosureDef , ClosureKind , Const , ConstId , ConstantKind ,
25+ EarlyParamRegion , FloatTy , FnDef , GenericArgs , GenericParamDef , IntTy , LineInfo , Movability ,
26+ RigidTy , Span , TyKind , UintTy ,
2627} ;
2728use stable_mir:: { self , opaque, Context , CrateItem , Error , Filename , ItemKind } ;
2829use std:: cell:: RefCell ;
@@ -317,6 +318,18 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
317318 . ok_or_else ( || Error :: new ( format ! ( "Const `{cnst:?}` cannot be encoded as u64" ) ) )
318319 }
319320
321+ fn eval_static_initializer ( & self , def : StaticDef ) -> Result < Allocation , Error > {
322+ let mut tables = self . 0 . borrow_mut ( ) ;
323+ let def_id = def. 0 . internal ( & mut * tables) ;
324+ tables. tcx . eval_static_initializer ( def_id) . stable ( & mut * tables)
325+ }
326+
327+ fn global_alloc ( & self , alloc : stable_mir:: mir:: alloc:: AllocId ) -> GlobalAlloc {
328+ let mut tables = self . 0 . borrow_mut ( ) ;
329+ let alloc_id = alloc. internal ( & mut * tables) ;
330+ tables. tcx . global_alloc ( alloc_id) . stable ( & mut * tables)
331+ }
332+
320333 fn usize_to_const ( & self , val : u64 ) -> Result < Const , Error > {
321334 let mut tables = self . 0 . borrow_mut ( ) ;
322335 let ty = tables. tcx . types . usize ;
@@ -341,7 +354,7 @@ pub(crate) struct TablesWrapper<'tcx>(pub(crate) RefCell<Tables<'tcx>>);
341354pub struct Tables < ' tcx > {
342355 pub ( crate ) tcx : TyCtxt < ' tcx > ,
343356 pub ( crate ) def_ids : IndexMap < DefId , stable_mir:: DefId > ,
344- pub ( crate ) alloc_ids : IndexMap < AllocId , stable_mir:: AllocId > ,
357+ pub ( crate ) alloc_ids : IndexMap < AllocId , stable_mir:: mir :: alloc :: AllocId > ,
345358 pub ( crate ) spans : IndexMap < rustc_span:: Span , Span > ,
346359 pub ( crate ) types : IndexMap < Ty < ' tcx > , stable_mir:: ty:: Ty > ,
347360 pub ( crate ) instances : IndexMap < ty:: Instance < ' tcx > , InstanceDef > ,
@@ -1539,6 +1552,14 @@ impl<'tcx> Stable<'tcx> for ty::BoundTy {
15391552 }
15401553}
15411554
1555+ impl < ' tcx > Stable < ' tcx > for mir:: interpret:: ConstAllocation < ' tcx > {
1556+ type T = Allocation ;
1557+
1558+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1559+ self . inner ( ) . stable ( tables)
1560+ }
1561+ }
1562+
15421563impl < ' tcx > Stable < ' tcx > for mir:: interpret:: Allocation {
15431564 type T = stable_mir:: ty:: Allocation ;
15441565
@@ -1551,6 +1572,25 @@ impl<'tcx> Stable<'tcx> for mir::interpret::Allocation {
15511572 }
15521573}
15531574
1575+ impl < ' tcx > Stable < ' tcx > for mir:: interpret:: GlobalAlloc < ' tcx > {
1576+ type T = GlobalAlloc ;
1577+
1578+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1579+ match self {
1580+ mir:: interpret:: GlobalAlloc :: Function ( instance) => {
1581+ GlobalAlloc :: Function ( instance. stable ( tables) )
1582+ }
1583+ mir:: interpret:: GlobalAlloc :: VTable ( ty, trait_ref) => {
1584+ GlobalAlloc :: VTable ( ty. stable ( tables) , trait_ref. stable ( tables) )
1585+ }
1586+ mir:: interpret:: GlobalAlloc :: Static ( def) => {
1587+ GlobalAlloc :: Static ( tables. static_def ( * def) )
1588+ }
1589+ mir:: interpret:: GlobalAlloc :: Memory ( alloc) => GlobalAlloc :: Memory ( alloc. stable ( tables) ) ,
1590+ }
1591+ }
1592+ }
1593+
15541594impl < ' tcx > Stable < ' tcx > for ty:: trait_def:: TraitSpecializationKind {
15551595 type T = stable_mir:: ty:: TraitSpecializationKind ;
15561596 fn stable ( & self , _: & mut Tables < ' tcx > ) -> Self :: T {
@@ -1938,6 +1978,14 @@ impl<'tcx> Stable<'tcx> for MonoItem<'tcx> {
19381978 }
19391979}
19401980
1981+ impl < ' tcx > Stable < ' tcx > for mir:: interpret:: ErrorHandled {
1982+ type T = Error ;
1983+
1984+ fn stable ( & self , _tables : & mut Tables < ' tcx > ) -> Self :: T {
1985+ Error :: new ( format ! ( "{self:?}" ) )
1986+ }
1987+ }
1988+
19411989impl < ' tcx , T > Stable < ' tcx > for & T
19421990where
19431991 T : Stable < ' tcx > ,
@@ -1959,3 +2007,18 @@ where
19592007 self . as_ref ( ) . map ( |value| value. stable ( tables) )
19602008 }
19612009}
2010+
2011+ impl < ' tcx , T , E > Stable < ' tcx > for Result < T , E >
2012+ where
2013+ T : Stable < ' tcx > ,
2014+ E : Stable < ' tcx > ,
2015+ {
2016+ type T = Result < T :: T , E :: T > ;
2017+
2018+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
2019+ match self {
2020+ Ok ( val) => Ok ( val. stable ( tables) ) ,
2021+ Err ( error) => Err ( error. stable ( tables) ) ,
2022+ }
2023+ }
2024+ }
0 commit comments