@@ -3,7 +3,7 @@ use std::mem;
33use either:: { Left , Right } ;
44
55use rustc_hir:: def:: DefKind ;
6- use rustc_middle:: mir:: interpret:: ErrorHandled ;
6+ use rustc_middle:: mir:: interpret:: { AllocId , ErrorHandled , InterpErrorInfo } ;
77use rustc_middle:: mir:: pretty:: write_allocation_bytes;
88use rustc_middle:: mir:: { self , ConstAlloc , ConstValue } ;
99use rustc_middle:: traits:: Reveal ;
@@ -338,36 +338,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
338338
339339 // Validation failed, report an error.
340340 if let Err ( error) = validation {
341- let ( error, backtrace) = error. into_parts ( ) ;
342- backtrace. print_backtrace ( ) ;
343-
344- let ub_note = matches ! ( error, InterpError :: UndefinedBehavior ( _) ) . then ( || { } ) ;
345-
346- let alloc = ecx. tcx . global_alloc ( alloc_id) . unwrap_memory ( ) . inner ( ) ;
347- let mut bytes = String :: new ( ) ;
348- if alloc. size ( ) != abi:: Size :: ZERO {
349- bytes = "\n " . into ( ) ;
350- // FIXME(translation) there might be pieces that are translatable.
351- write_allocation_bytes ( * ecx. tcx , alloc, & mut bytes, " " ) . unwrap ( ) ;
352- }
353- let raw_bytes = errors:: RawBytesNote {
354- size : alloc. size ( ) . bytes ( ) ,
355- align : alloc. align . bytes ( ) ,
356- bytes,
357- } ;
358-
359- Err ( super :: report (
360- * ecx. tcx ,
361- error,
362- None ,
363- || super :: get_span_and_frames ( & ecx) ,
364- move |span, frames| errors:: UndefinedBehavior {
365- span,
366- ub_note,
367- frames,
368- raw_bytes,
369- } ,
370- ) )
341+ Err ( const_report_error ( & ecx, error, alloc_id) )
371342 } else {
372343 // Convert to raw constant
373344 Ok ( ConstAlloc { alloc_id, ty : mplace. layout . ty } )
@@ -403,3 +374,33 @@ pub fn const_validate_mplace<'mir, 'tcx>(
403374
404375 Ok ( ( ) )
405376}
377+
378+ #[ inline( always) ]
379+ pub fn const_report_error < ' mir , ' tcx > (
380+ ecx : & InterpCx < ' mir , ' tcx , CompileTimeInterpreter < ' mir , ' tcx > > ,
381+ error : InterpErrorInfo < ' tcx > ,
382+ alloc_id : AllocId ,
383+ ) -> ErrorHandled {
384+ let ( error, backtrace) = error. into_parts ( ) ;
385+ backtrace. print_backtrace ( ) ;
386+
387+ let ub_note = matches ! ( error, InterpError :: UndefinedBehavior ( _) ) . then ( || { } ) ;
388+
389+ let alloc = ecx. tcx . global_alloc ( alloc_id) . unwrap_memory ( ) . inner ( ) ;
390+ let mut bytes = String :: new ( ) ;
391+ if alloc. size ( ) != abi:: Size :: ZERO {
392+ bytes = "\n " . into ( ) ;
393+ // FIXME(translation) there might be pieces that are translatable.
394+ write_allocation_bytes ( * ecx. tcx , alloc, & mut bytes, " " ) . unwrap ( ) ;
395+ }
396+ let raw_bytes =
397+ errors:: RawBytesNote { size : alloc. size ( ) . bytes ( ) , align : alloc. align . bytes ( ) , bytes } ;
398+
399+ crate :: const_eval:: report (
400+ * ecx. tcx ,
401+ error,
402+ None ,
403+ || crate :: const_eval:: get_span_and_frames ( ecx) ,
404+ move |span, frames| errors:: UndefinedBehavior { span, ub_note, frames, raw_bytes } ,
405+ )
406+ }
0 commit comments