@@ -2,6 +2,7 @@ use rustc_middle::mir::{
2
2
interpret:: { alloc_range, AllocRange , Pointer } ,
3
3
ConstValue ,
4
4
} ;
5
+ use stable_mir:: Error ;
5
6
6
7
use crate :: rustc_smir:: { Stable , Tables } ;
7
8
use stable_mir:: mir:: Mutability ;
@@ -26,23 +27,35 @@ pub fn new_allocation<'tcx>(
26
27
const_value : ConstValue < ' tcx > ,
27
28
tables : & mut Tables < ' tcx > ,
28
29
) -> 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 {
30
40
ConstValue :: Scalar ( scalar) => {
31
41
let size = scalar. size ( ) ;
32
42
let align = tables
33
43
. tcx
34
44
. layout_of ( rustc_middle:: ty:: ParamEnv :: reveal_all ( ) . and ( ty) )
35
- . unwrap ( )
45
+ . map_err ( |e| e . stable ( tables ) ) ?
36
46
. align ;
37
47
let mut allocation = rustc_middle:: mir:: interpret:: Allocation :: uninit ( size, align. abi ) ;
38
48
allocation
39
49
. write_scalar ( & tables. tcx , alloc_range ( rustc_target:: abi:: Size :: ZERO , size) , scalar)
40
- . unwrap ( ) ;
50
+ . map_err ( |e| e . stable ( tables ) ) ? ;
41
51
allocation. stable ( tables)
42
52
}
43
53
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 ;
46
59
new_empty_allocation ( align. abi )
47
60
}
48
61
ConstValue :: Slice { data, meta } => {
@@ -51,8 +64,10 @@ pub fn new_allocation<'tcx>(
51
64
let scalar_ptr = rustc_middle:: mir:: interpret:: Scalar :: from_pointer ( ptr, & tables. tcx ) ;
52
65
let scalar_meta =
53
66
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) ) ?;
56
71
let mut allocation =
57
72
rustc_middle:: mir:: interpret:: Allocation :: uninit ( layout. size , layout. align . abi ) ;
58
73
allocation
@@ -61,26 +76,26 @@ pub fn new_allocation<'tcx>(
61
76
alloc_range ( rustc_target:: abi:: Size :: ZERO , tables. tcx . data_layout . pointer_size ) ,
62
77
scalar_ptr,
63
78
)
64
- . unwrap ( ) ;
79
+ . map_err ( |e| e . stable ( tables ) ) ? ;
65
80
allocation
66
81
. write_scalar (
67
82
& tables. tcx ,
68
83
alloc_range ( tables. tcx . data_layout . pointer_size , scalar_meta. size ( ) ) ,
69
84
scalar_meta,
70
85
)
71
- . unwrap ( ) ;
86
+ . map_err ( |e| e . stable ( tables ) ) ? ;
72
87
allocation. stable ( tables)
73
88
}
74
89
ConstValue :: Indirect { alloc_id, offset } => {
75
90
let alloc = tables. tcx . global_alloc ( alloc_id) . unwrap_memory ( ) ;
76
91
let ty_size = tables
77
92
. tcx
78
93
. layout_of ( rustc_middle:: ty:: ParamEnv :: reveal_all ( ) . and ( ty) )
79
- . unwrap ( )
94
+ . map_err ( |e| e . stable ( tables ) ) ?
80
95
. size ;
81
96
allocation_filter ( & alloc. 0 , alloc_range ( offset, ty_size) , tables)
82
97
}
83
- }
98
+ } )
84
99
}
85
100
86
101
/// Creates an `Allocation` only from information within the `AllocRange`.
0 commit comments