@@ -2,6 +2,7 @@ use rustc_middle::mir::{
22 interpret:: { alloc_range, AllocRange , Pointer } ,
33 ConstValue ,
44} ;
5+ use stable_mir:: Error ;
56
67use crate :: rustc_smir:: { Stable , Tables } ;
78use stable_mir:: mir:: Mutability ;
@@ -26,23 +27,35 @@ pub fn new_allocation<'tcx>(
2627 const_value : ConstValue < ' tcx > ,
2728 tables : & mut Tables < ' tcx > ,
2829) -> Allocation {
29- match const_value {
30+ try_new_allocation ( ty, const_value, tables) . unwrap ( )
31+ }
32+
33+ #[ allow( rustc:: usage_of_qualified_ty) ]
34+ pub fn try_new_allocation < ' tcx > (
35+ ty : rustc_middle:: ty:: Ty < ' tcx > ,
36+ const_value : ConstValue < ' tcx > ,
37+ tables : & mut Tables < ' tcx > ,
38+ ) -> Result < Allocation , Error > {
39+ Ok ( match const_value {
3040 ConstValue :: Scalar ( scalar) => {
3141 let size = scalar. size ( ) ;
3242 let align = tables
3343 . tcx
3444 . layout_of ( rustc_middle:: ty:: ParamEnv :: reveal_all ( ) . and ( ty) )
35- . unwrap ( )
45+ . map_err ( |e| e . stable ( tables ) ) ?
3646 . align ;
3747 let mut allocation = rustc_middle:: mir:: interpret:: Allocation :: uninit ( size, align. abi ) ;
3848 allocation
3949 . write_scalar ( & tables. tcx , alloc_range ( rustc_target:: abi:: Size :: ZERO , size) , scalar)
40- . unwrap ( ) ;
50+ . map_err ( |e| e . stable ( tables ) ) ? ;
4151 allocation. stable ( tables)
4252 }
4353 ConstValue :: ZeroSized => {
44- let align =
45- tables. tcx . layout_of ( rustc_middle:: ty:: ParamEnv :: empty ( ) . and ( ty) ) . unwrap ( ) . align ;
54+ let align = tables
55+ . tcx
56+ . layout_of ( rustc_middle:: ty:: ParamEnv :: empty ( ) . and ( ty) )
57+ . map_err ( |e| e. stable ( tables) ) ?
58+ . align ;
4659 new_empty_allocation ( align. abi )
4760 }
4861 ConstValue :: Slice { data, meta } => {
@@ -51,8 +64,10 @@ pub fn new_allocation<'tcx>(
5164 let scalar_ptr = rustc_middle:: mir:: interpret:: Scalar :: from_pointer ( ptr, & tables. tcx ) ;
5265 let scalar_meta =
5366 rustc_middle:: mir:: interpret:: Scalar :: from_target_usize ( meta, & tables. tcx ) ;
54- let layout =
55- tables. tcx . layout_of ( rustc_middle:: ty:: ParamEnv :: reveal_all ( ) . and ( ty) ) . unwrap ( ) ;
67+ let layout = tables
68+ . tcx
69+ . layout_of ( rustc_middle:: ty:: ParamEnv :: reveal_all ( ) . and ( ty) )
70+ . map_err ( |e| e. stable ( tables) ) ?;
5671 let mut allocation =
5772 rustc_middle:: mir:: interpret:: Allocation :: uninit ( layout. size , layout. align . abi ) ;
5873 allocation
@@ -61,26 +76,26 @@ pub fn new_allocation<'tcx>(
6176 alloc_range ( rustc_target:: abi:: Size :: ZERO , tables. tcx . data_layout . pointer_size ) ,
6277 scalar_ptr,
6378 )
64- . unwrap ( ) ;
79+ . map_err ( |e| e . stable ( tables ) ) ? ;
6580 allocation
6681 . write_scalar (
6782 & tables. tcx ,
6883 alloc_range ( tables. tcx . data_layout . pointer_size , scalar_meta. size ( ) ) ,
6984 scalar_meta,
7085 )
71- . unwrap ( ) ;
86+ . map_err ( |e| e . stable ( tables ) ) ? ;
7287 allocation. stable ( tables)
7388 }
7489 ConstValue :: Indirect { alloc_id, offset } => {
7590 let alloc = tables. tcx . global_alloc ( alloc_id) . unwrap_memory ( ) ;
7691 let ty_size = tables
7792 . tcx
7893 . layout_of ( rustc_middle:: ty:: ParamEnv :: reveal_all ( ) . and ( ty) )
79- . unwrap ( )
94+ . map_err ( |e| e . stable ( tables ) ) ?
8095 . size ;
8196 allocation_filter ( & alloc. 0 , alloc_range ( offset, ty_size) , tables)
8297 }
83- }
98+ } )
8499}
85100
86101/// Creates an `Allocation` only from information within the `AllocRange`.
0 commit comments