@@ -290,10 +290,36 @@ 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
+ /// This function takes the place where the result of the evaluation is stored
298
+ /// and prepares it for returning it in the appropriate format needed by the specific
299
+ /// evaluation query.
300
+ fn make_result < ' mir > (
301
+ mplace : MPlaceTy < ' tcx > ,
302
+ ecx : & mut InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
303
+ ) -> Self ;
304
+ }
305
+
306
+ impl < ' tcx > InterpretationResult < ' tcx > for mir:: interpret:: ConstAllocation < ' tcx > {
307
+ fn make_result < ' mir > (
308
+ mplace : MPlaceTy < ' tcx > ,
309
+ ecx : & mut InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
310
+ ) -> Self {
311
+ let alloc = take_static_root_alloc ( ecx, mplace. ptr ( ) . provenance . unwrap ( ) . alloc_id ( ) ) ;
312
+ ecx. tcx . mk_const_alloc ( alloc)
313
+ }
314
+ }
315
+
316
+ impl < ' tcx > InterpretationResult < ' tcx > for ConstAlloc < ' tcx > {
317
+ fn make_result < ' mir > (
318
+ mplace : MPlaceTy < ' tcx > ,
319
+ _ecx : & mut InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
320
+ ) -> Self {
321
+ ConstAlloc { alloc_id : mplace. ptr ( ) . provenance . unwrap ( ) . alloc_id ( ) , ty : mplace. layout . ty }
322
+ }
297
323
}
298
324
299
325
#[ instrument( skip( tcx) , level = "debug" ) ]
@@ -336,11 +362,11 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
336
362
eval_in_interpreter ( & mut ecx, cid, is_static)
337
363
}
338
364
339
- pub fn eval_in_interpreter < ' mir , ' tcx > (
365
+ fn eval_in_interpreter < ' mir , ' tcx , R : InterpretationResult < ' tcx > > (
340
366
ecx : & mut InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
341
367
cid : GlobalId < ' tcx > ,
342
368
is_static : bool ,
343
- ) -> :: rustc_middle :: mir :: interpret :: EvalToAllocationRawResult < ' tcx > {
369
+ ) -> Result < R , ErrorHandled > {
344
370
// `is_static` just means "in static", it could still be a promoted!
345
371
debug_assert_eq ! ( is_static, ecx. tcx. static_mutability( cid. instance. def_id( ) ) . is_some( ) ) ;
346
372
@@ -383,14 +409,12 @@ pub fn eval_in_interpreter<'mir, 'tcx>(
383
409
384
410
let res = const_validate_mplace ( & ecx, & mplace, cid) ;
385
411
386
- let alloc_id = mplace. ptr ( ) . provenance . unwrap ( ) . alloc_id ( ) ;
387
-
388
412
// Validation failed, report an error.
389
413
if let Err ( error) = res {
414
+ let alloc_id = mplace. ptr ( ) . provenance . unwrap ( ) . alloc_id ( ) ;
390
415
Err ( const_report_error ( & ecx, error, alloc_id) )
391
416
} else {
392
- // Convert to raw constant
393
- Ok ( ConstAlloc { alloc_id, ty : mplace. layout . ty } )
417
+ Ok ( R :: make_result ( mplace, ecx) )
394
418
}
395
419
}
396
420
}
0 commit comments