1
1
use cranelift_module:: * ;
2
2
use crate :: prelude:: * ;
3
- use rustc:: mir:: interpret:: { read_target_uint, AllocId , AllocType , ConstValue , GlobalId } ;
3
+ use rustc:: mir:: interpret:: {
4
+ read_target_uint, AllocId , AllocType , Allocation , ConstValue , EvalResult , GlobalId ,
5
+ } ;
4
6
use rustc:: ty:: Const ;
5
- use rustc_mir:: interpret:: { CompileTimeEvaluator , Memory } ;
7
+ use rustc_mir:: interpret:: { CompileTimeEvaluator , EvalContext , Memory , MemoryKind } ;
6
8
use syntax:: ast:: Mutability as AstMutability ;
7
9
8
10
#[ derive( Default ) ]
@@ -120,7 +122,22 @@ fn trans_const_place<'a, 'tcx: 'a>(
120
122
fx : & mut FunctionCx < ' a , ' tcx , impl Backend > ,
121
123
const_ : & ' tcx Const < ' tcx > ,
122
124
) -> CPlace < ' tcx > {
123
- let alloc = fx. tcx . const_to_allocation ( const_) ;
125
+ // Adapted from https://github.com/rust-lang/rust/pull/53671/files#diff-e0b58bb6712edaa8595ad7237542c958L551
126
+ let result = || -> EvalResult < ' tcx , & ' tcx Allocation > {
127
+ let mut ecx = EvalContext :: new (
128
+ fx. tcx . at ( DUMMY_SP ) ,
129
+ ty:: ParamEnv :: reveal_all ( ) ,
130
+ CompileTimeEvaluator ,
131
+ ( ) ,
132
+ ) ;
133
+ let op = ecx. const_to_op ( const_) ?;
134
+ let ptr = ecx. allocate ( op. layout , MemoryKind :: Stack ) ?;
135
+ ecx. copy_op ( op, ptr. into ( ) ) ?;
136
+ let alloc = ecx. memory . get ( ptr. to_ptr ( ) ?. alloc_id ) ?;
137
+ Ok ( fx. tcx . intern_const_alloc ( alloc. clone ( ) ) )
138
+ } ;
139
+ let alloc = result ( ) . expect ( "unable to convert ConstValue to Allocation" ) ;
140
+
124
141
//println!("const value: {:?} allocation: {:?}", value, alloc);
125
142
let alloc_id = fx. tcx . alloc_map . lock ( ) . allocate ( alloc) ;
126
143
fx. constants . todo . insert ( TodoItem :: Alloc ( alloc_id) ) ;
@@ -190,7 +207,7 @@ fn define_all_allocs<'a, 'tcx: 'a, B: Backend + 'a>(
190
207
let const_ = tcx. const_eval ( ParamEnv :: reveal_all ( ) . and ( cid) ) . unwrap ( ) ;
191
208
192
209
let alloc = match const_. val {
193
- ConstValue :: ByRef ( alloc, n) if n. bytes ( ) == 0 => alloc,
210
+ ConstValue :: ByRef ( _alloc_id , alloc, n) if n. bytes ( ) == 0 => alloc,
194
211
_ => bug ! ( "static const eval returned {:#?}" , const_) ,
195
212
} ;
196
213
@@ -208,7 +225,7 @@ fn define_all_allocs<'a, 'tcx: 'a, B: Backend + 'a>(
208
225
209
226
data_ctx. define (
210
227
alloc. bytes . to_vec ( ) . into_boxed_slice ( ) ,
211
- match alloc. runtime_mutability {
228
+ match alloc. mutability {
212
229
AstMutability :: Mutable => Writability :: Writable ,
213
230
AstMutability :: Immutable => Writability :: Readonly ,
214
231
} ,
0 commit comments