@@ -24,12 +24,12 @@ use crate::interpret::{
2424} ;
2525
2626// Returns a pointer to where the result lives
27- #[ instrument( level = "trace" , skip( ecx, body) , ret ) ]
28- fn eval_body_using_ecx < ' mir , ' tcx > (
27+ #[ instrument( level = "trace" , skip( ecx, body) ) ]
28+ fn eval_body_using_ecx < ' mir , ' tcx , R : InterpretationResult < ' tcx > > (
2929 ecx : & mut CompileTimeEvalContext < ' mir , ' tcx > ,
3030 cid : GlobalId < ' tcx > ,
3131 body : & ' mir mir:: Body < ' tcx > ,
32- ) -> InterpResult < ' tcx , MPlaceTy < ' tcx > > {
32+ ) -> InterpResult < ' tcx , R > {
3333 trace ! ( ?ecx. param_env) ;
3434 let tcx = * ecx. tcx ;
3535 assert ! (
@@ -87,7 +87,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
8787 // Since evaluation had no errors, validate the resulting constant.
8888 const_validate_mplace ( & ecx, & ret, cid) ?;
8989
90- Ok ( ret)
90+ Ok ( R :: make_result ( ret, ecx ) )
9191}
9292
9393/// The `InterpCx` is only meant to be used to do field and index projections into constants for
@@ -291,14 +291,14 @@ pub fn eval_static_initializer_provider<'tcx>(
291291pub trait InterpretationResult < ' tcx > {
292292 fn make_result < ' mir > (
293293 mplace : MPlaceTy < ' tcx > ,
294- ecx : InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
294+ ecx : & mut InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
295295 ) -> Self ;
296296}
297297
298298impl < ' tcx > InterpretationResult < ' tcx > for ConstAlloc < ' tcx > {
299299 fn make_result < ' mir > (
300300 mplace : MPlaceTy < ' tcx > ,
301- _ecx : InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
301+ _ecx : & mut InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
302302 ) -> Self {
303303 ConstAlloc { alloc_id : mplace. ptr ( ) . provenance . unwrap ( ) . alloc_id ( ) , ty : mplace. layout . ty }
304304 }
@@ -349,41 +349,33 @@ fn eval_in_interpreter<'tcx, R: InterpretationResult<'tcx>>(
349349 CompileTimeInterpreter :: new ( CanAccessMutGlobal :: from ( is_static) , CheckAlignment :: Error ) ,
350350 ) ;
351351 let res = ecx. load_mir ( cid. instance . def , cid. promoted ) ;
352- match res. and_then ( |body| eval_body_using_ecx ( & mut ecx, cid, body) ) {
353- Err ( error) => {
354- let ( error, backtrace) = error. into_parts ( ) ;
355- backtrace. print_backtrace ( ) ;
356-
357- let ( kind, instance) = if ecx. tcx . is_static ( cid. instance . def_id ( ) ) {
358- ( "static" , String :: new ( ) )
352+ res. and_then ( |body| eval_body_using_ecx ( & mut ecx, cid, body) ) . map_err ( |error| {
353+ let ( error, backtrace) = error. into_parts ( ) ;
354+ backtrace. print_backtrace ( ) ;
355+
356+ let ( kind, instance) = if ecx. tcx . is_static ( cid. instance . def_id ( ) ) {
357+ ( "static" , String :: new ( ) )
358+ } else {
359+ // If the current item has generics, we'd like to enrich the message with the
360+ // instance and its args: to show the actual compile-time values, in addition to
361+ // the expression, leading to the const eval error.
362+ let instance = & cid. instance ;
363+ if !instance. args . is_empty ( ) {
364+ let instance = with_no_trimmed_paths ! ( instance. to_string( ) ) ;
365+ ( "const_with_path" , instance)
359366 } else {
360- // If the current item has generics, we'd like to enrich the message with the
361- // instance and its args: to show the actual compile-time values, in addition to
362- // the expression, leading to the const eval error.
363- let instance = & cid. instance ;
364- if !instance. args . is_empty ( ) {
365- let instance = with_no_trimmed_paths ! ( instance. to_string( ) ) ;
366- ( "const_with_path" , instance)
367- } else {
368- ( "const" , String :: new ( ) )
369- }
370- } ;
371-
372- Err ( super :: report (
373- * ecx. tcx ,
374- error,
375- None ,
376- || super :: get_span_and_frames ( ecx. tcx , ecx. stack ( ) ) ,
377- |span, frames| ConstEvalError {
378- span,
379- error_kind : kind,
380- instance,
381- frame_notes : frames,
382- } ,
383- ) )
384- }
385- Ok ( mplace) => Ok ( R :: make_result ( mplace, ecx) ) ,
386- }
367+ ( "const" , String :: new ( ) )
368+ }
369+ } ;
370+
371+ super :: report (
372+ * ecx. tcx ,
373+ error,
374+ None ,
375+ || super :: get_span_and_frames ( ecx. tcx , ecx. stack ( ) ) ,
376+ |span, frames| ConstEvalError { span, error_kind : kind, instance, frame_notes : frames } ,
377+ )
378+ } )
387379}
388380
389381#[ inline( always) ]
0 commit comments