@@ -290,10 +290,33 @@ pub fn eval_static_initializer_provider<'tcx>(
290
290
// they do not have to behave "as if" they were evaluated at runtime.
291
291
CompileTimeInterpreter :: new ( CanAccessMutGlobal :: Yes , CheckAlignment :: Error ) ,
292
292
) ;
293
- let alloc_id = eval_in_interpreter ( & mut ecx, cid, true ) ?. alloc_id ;
294
- let alloc = take_static_root_alloc ( & mut ecx, alloc_id) ;
295
- let alloc = tcx. mk_const_alloc ( alloc) ;
296
- Ok ( alloc)
293
+ eval_in_interpreter ( & mut ecx, cid, true )
294
+ }
295
+
296
+ trait InterpretationResult < ' tcx > {
297
+ fn make_result < ' mir > (
298
+ mplace : MPlaceTy < ' tcx > ,
299
+ ecx : & mut InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
300
+ ) -> Self ;
301
+ }
302
+
303
+ impl < ' tcx > InterpretationResult < ' tcx > for mir:: interpret:: ConstAllocation < ' tcx > {
304
+ fn make_result < ' mir > (
305
+ mplace : MPlaceTy < ' tcx > ,
306
+ ecx : & mut InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
307
+ ) -> Self {
308
+ let alloc = take_static_root_alloc ( ecx, mplace. ptr ( ) . provenance . unwrap ( ) . alloc_id ( ) ) ;
309
+ ecx. tcx . mk_const_alloc ( alloc)
310
+ }
311
+ }
312
+
313
+ impl < ' tcx > InterpretationResult < ' tcx > for ConstAlloc < ' tcx > {
314
+ fn make_result < ' mir > (
315
+ mplace : MPlaceTy < ' tcx > ,
316
+ _ecx : & mut InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
317
+ ) -> Self {
318
+ ConstAlloc { alloc_id : mplace. ptr ( ) . provenance . unwrap ( ) . alloc_id ( ) , ty : mplace. layout . ty }
319
+ }
297
320
}
298
321
299
322
#[ instrument( skip( tcx) , level = "debug" ) ]
@@ -336,11 +359,11 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
336
359
eval_in_interpreter ( & mut ecx, cid, is_static)
337
360
}
338
361
339
- pub fn eval_in_interpreter < ' mir , ' tcx > (
362
+ fn eval_in_interpreter < ' mir , ' tcx , R : InterpretationResult < ' tcx > > (
340
363
ecx : & mut InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
341
364
cid : GlobalId < ' tcx > ,
342
365
is_static : bool ,
343
- ) -> :: rustc_middle :: mir :: interpret :: EvalToAllocationRawResult < ' tcx > {
366
+ ) -> Result < R , ErrorHandled > {
344
367
// `is_static` just means "in static", it could still be a promoted!
345
368
debug_assert_eq ! ( is_static, ecx. tcx. static_mutability( cid. instance. def_id( ) ) . is_some( ) ) ;
346
369
@@ -386,14 +409,12 @@ pub fn eval_in_interpreter<'mir, 'tcx>(
386
409
let res = const_validate_mplace ( & ecx, & mplace, cid) ;
387
410
ecx. machine . static_root_ids = static_root_ids;
388
411
389
- let alloc_id = mplace. ptr ( ) . provenance . unwrap ( ) . alloc_id ( ) ;
390
-
391
412
// Validation failed, report an error.
392
413
if let Err ( error) = res {
414
+ let alloc_id = mplace. ptr ( ) . provenance . unwrap ( ) . alloc_id ( ) ;
393
415
Err ( const_report_error ( & ecx, error, alloc_id) )
394
416
} else {
395
- // Convert to raw constant
396
- Ok ( ConstAlloc { alloc_id, ty : mplace. layout . ty } )
417
+ Ok ( R :: make_result ( mplace, ecx) )
397
418
}
398
419
}
399
420
}
0 commit comments