@@ -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
@@ -294,14 +294,14 @@ pub trait InterpretationResult<'tcx> {
294294 /// evaluation query.
295295 fn make_result < ' mir > (
296296 mplace : MPlaceTy < ' tcx > ,
297- ecx : InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
297+ ecx : & mut InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
298298 ) -> Self ;
299299}
300300
301301impl < ' tcx > InterpretationResult < ' tcx > for ConstAlloc < ' tcx > {
302302 fn make_result < ' mir > (
303303 mplace : MPlaceTy < ' tcx > ,
304- _ecx : InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
304+ _ecx : & mut InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
305305 ) -> Self {
306306 ConstAlloc { alloc_id : mplace. ptr ( ) . provenance . unwrap ( ) . alloc_id ( ) , ty : mplace. layout . ty }
307307 }
@@ -352,41 +352,33 @@ fn eval_in_interpreter<'tcx, R: InterpretationResult<'tcx>>(
352352 CompileTimeInterpreter :: new ( CanAccessMutGlobal :: from ( is_static) , CheckAlignment :: Error ) ,
353353 ) ;
354354 let res = ecx. load_mir ( cid. instance . def , cid. promoted ) ;
355- match res. and_then ( |body| eval_body_using_ecx ( & mut ecx, cid, body) ) {
356- Err ( error) => {
357- let ( error, backtrace) = error. into_parts ( ) ;
358- backtrace. print_backtrace ( ) ;
359-
360- let ( kind, instance) = if ecx. tcx . is_static ( cid. instance . def_id ( ) ) {
361- ( "static" , String :: new ( ) )
355+ res. and_then ( |body| eval_body_using_ecx ( & mut ecx, cid, body) ) . map_err ( |error| {
356+ let ( error, backtrace) = error. into_parts ( ) ;
357+ backtrace. print_backtrace ( ) ;
358+
359+ let ( kind, instance) = if ecx. tcx . is_static ( cid. instance . def_id ( ) ) {
360+ ( "static" , String :: new ( ) )
361+ } else {
362+ // If the current item has generics, we'd like to enrich the message with the
363+ // instance and its args: to show the actual compile-time values, in addition to
364+ // the expression, leading to the const eval error.
365+ let instance = & cid. instance ;
366+ if !instance. args . is_empty ( ) {
367+ let instance = with_no_trimmed_paths ! ( instance. to_string( ) ) ;
368+ ( "const_with_path" , instance)
362369 } else {
363- // If the current item has generics, we'd like to enrich the message with the
364- // instance and its args: to show the actual compile-time values, in addition to
365- // the expression, leading to the const eval error.
366- let instance = & cid. instance ;
367- if !instance. args . is_empty ( ) {
368- let instance = with_no_trimmed_paths ! ( instance. to_string( ) ) ;
369- ( "const_with_path" , instance)
370- } else {
371- ( "const" , String :: new ( ) )
372- }
373- } ;
374-
375- Err ( super :: report (
376- * ecx. tcx ,
377- error,
378- None ,
379- || super :: get_span_and_frames ( ecx. tcx , ecx. stack ( ) ) ,
380- |span, frames| ConstEvalError {
381- span,
382- error_kind : kind,
383- instance,
384- frame_notes : frames,
385- } ,
386- ) )
387- }
388- Ok ( mplace) => Ok ( R :: make_result ( mplace, ecx) ) ,
389- }
370+ ( "const" , String :: new ( ) )
371+ }
372+ } ;
373+
374+ super :: report (
375+ * ecx. tcx ,
376+ error,
377+ None ,
378+ || super :: get_span_and_frames ( ecx. tcx , ecx. stack ( ) ) ,
379+ |span, frames| ConstEvalError { span, error_kind : kind, instance, frame_notes : frames } ,
380+ )
381+ } )
390382}
391383
392384#[ inline( always) ]
0 commit comments