@@ -6,6 +6,7 @@ use stable_mir::mir::Mutability;
6
6
use stable_mir:: ty:: { Allocation , ProvenanceMap } ;
7
7
8
8
use crate :: rustc_smir:: { Stable , Tables } ;
9
+ use crate :: stable_mir;
9
10
10
11
/// Creates new empty `Allocation` from given `Align`.
11
12
fn new_empty_allocation ( align : Align ) -> Allocation {
@@ -27,7 +28,7 @@ pub(crate) fn new_allocation<'tcx>(
27
28
tables : & mut Tables < ' tcx > ,
28
29
) -> Allocation {
29
30
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:?}" ) )
31
32
}
32
33
33
34
#[ allow( rustc:: usage_of_qualified_ty) ]
@@ -36,39 +37,30 @@ pub(crate) fn try_new_allocation<'tcx>(
36
37
const_value : ConstValue < ' tcx > ,
37
38
tables : & mut Tables < ' tcx > ,
38
39
) -> 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) ) ?;
39
44
Ok ( match const_value {
40
45
ConstValue :: Scalar ( scalar) => {
41
46
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
+ ) ;
49
52
allocation
50
53
. write_scalar ( & tables. tcx , alloc_range ( Size :: ZERO , size) , scalar)
51
54
. map_err ( |e| e. stable ( tables) ) ?;
52
55
allocation. stable ( tables)
53
56
}
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 ) ,
62
58
ConstValue :: Slice { data, meta } => {
63
59
let alloc_id = tables. tcx . reserve_and_set_memory_alloc ( data) ;
64
60
let ptr = Pointer :: new ( alloc_id. into ( ) , Size :: ZERO ) ;
65
61
let scalar_ptr = rustc_middle:: mir:: interpret:: Scalar :: from_pointer ( ptr, & tables. tcx ) ;
66
62
let scalar_meta =
67
63
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) ) ?;
72
64
let mut allocation = rustc_middle:: mir:: interpret:: Allocation :: new (
73
65
layout. size ,
74
66
layout. align . abi ,
@@ -92,12 +84,7 @@ pub(crate) fn try_new_allocation<'tcx>(
92
84
}
93
85
ConstValue :: Indirect { alloc_id, offset } => {
94
86
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)
101
88
}
102
89
} )
103
90
}
0 commit comments