@@ -6,6 +6,7 @@ use stable_mir::mir::Mutability;
66use stable_mir:: ty:: { Allocation , ProvenanceMap } ;
77
88use crate :: rustc_smir:: { Stable , Tables } ;
9+ use crate :: stable_mir;
910
1011/// Creates new empty `Allocation` from given `Align`.
1112fn new_empty_allocation ( align : Align ) -> Allocation {
@@ -27,7 +28,7 @@ pub(crate) fn new_allocation<'tcx>(
2728 tables : & mut Tables < ' tcx > ,
2829) -> Allocation {
2930 try_new_allocation ( ty, const_value, tables)
30- . expect ( & format ! ( "Failed to convert: {const_value:?} to {ty:?}" ) )
31+ . unwrap_or_else ( |_| panic ! ( "Failed to convert: {const_value:?} to {ty:?}" ) )
3132}
3233
3334#[ allow( rustc:: usage_of_qualified_ty) ]
@@ -36,39 +37,30 @@ pub(crate) fn try_new_allocation<'tcx>(
3637 const_value : ConstValue < ' tcx > ,
3738 tables : & mut Tables < ' tcx > ,
3839) -> Result < Allocation , Error > {
40+ let layout = tables
41+ . tcx
42+ . layout_of ( rustc_middle:: ty:: TypingEnv :: fully_monomorphized ( ) . as_query_input ( ty) )
43+ . map_err ( |e| e. stable ( tables) ) ?;
3944 Ok ( match const_value {
4045 ConstValue :: Scalar ( scalar) => {
4146 let size = scalar. size ( ) ;
42- let align = tables
43- . tcx
44- . layout_of ( rustc_middle:: ty:: TypingEnv :: fully_monomorphized ( ) . as_query_input ( ty) )
45- . map_err ( |e| e. stable ( tables) ) ?
46- . align ;
47- let mut allocation =
48- rustc_middle:: mir:: interpret:: Allocation :: new ( size, align. abi , AllocInit :: Uninit ) ;
47+ let mut allocation = rustc_middle:: mir:: interpret:: Allocation :: new (
48+ size,
49+ layout. align . abi ,
50+ AllocInit :: Uninit ,
51+ ) ;
4952 allocation
5053 . write_scalar ( & tables. tcx , alloc_range ( Size :: ZERO , size) , scalar)
5154 . map_err ( |e| e. stable ( tables) ) ?;
5255 allocation. stable ( tables)
5356 }
54- ConstValue :: ZeroSized => {
55- let align = tables
56- . tcx
57- . layout_of ( rustc_middle:: ty:: TypingEnv :: fully_monomorphized ( ) . as_query_input ( ty) )
58- . map_err ( |e| e. stable ( tables) ) ?
59- . align ;
60- new_empty_allocation ( align. abi )
61- }
57+ ConstValue :: ZeroSized => new_empty_allocation ( layout. align . abi ) ,
6258 ConstValue :: Slice { data, meta } => {
6359 let alloc_id = tables. tcx . reserve_and_set_memory_alloc ( data) ;
6460 let ptr = Pointer :: new ( alloc_id. into ( ) , Size :: ZERO ) ;
6561 let scalar_ptr = rustc_middle:: mir:: interpret:: Scalar :: from_pointer ( ptr, & tables. tcx ) ;
6662 let scalar_meta =
6763 rustc_middle:: mir:: interpret:: Scalar :: from_target_usize ( meta, & tables. tcx ) ;
68- let layout = tables
69- . tcx
70- . layout_of ( rustc_middle:: ty:: TypingEnv :: fully_monomorphized ( ) . as_query_input ( ty) )
71- . map_err ( |e| e. stable ( tables) ) ?;
7264 let mut allocation = rustc_middle:: mir:: interpret:: Allocation :: new (
7365 layout. size ,
7466 layout. align . abi ,
@@ -92,12 +84,7 @@ pub(crate) fn try_new_allocation<'tcx>(
9284 }
9385 ConstValue :: Indirect { alloc_id, offset } => {
9486 let alloc = tables. tcx . global_alloc ( alloc_id) . unwrap_memory ( ) ;
95- let ty_size = tables
96- . tcx
97- . layout_of ( rustc_middle:: ty:: TypingEnv :: fully_monomorphized ( ) . as_query_input ( ty) )
98- . map_err ( |e| e. stable ( tables) ) ?
99- . size ;
100- allocation_filter ( & alloc. 0 , alloc_range ( offset, ty_size) , tables)
87+ allocation_filter ( & alloc. 0 , alloc_range ( offset, layout. size ) , tables)
10188 }
10289 } )
10390}
0 commit comments