1
- use std:: fmt;
1
+ use std:: fmt:: { self , Write } ;
2
2
use std:: num:: NonZeroU64 ;
3
3
4
4
use log:: trace;
5
5
6
- use rustc_const_eval:: ReportErrorExt ;
7
6
use rustc_errors:: DiagnosticMessage ;
8
7
use rustc_span:: { source_map:: DUMMY_SP , SpanData , Symbol } ;
9
8
use rustc_target:: abi:: { Align , Size } ;
@@ -271,10 +270,13 @@ pub fn report_error<'tcx, 'mir>(
271
270
} ;
272
271
( title, helps)
273
272
} else {
274
- #[ rustfmt:: skip]
275
273
let title = match e. kind ( ) {
276
- UndefinedBehavior ( UndefinedBehaviorInfo :: ValidationError ( e) ) if matches ! ( e. kind, ValidationErrorKind :: PointerAsInt { .. } | ValidationErrorKind :: PartialPointer ) =>
277
- bug ! ( "This validation error should be impossible in Miri: {:?}" , e. kind) ,
274
+ UndefinedBehavior ( UndefinedBehaviorInfo :: ValidationError ( validation_err) )
275
+ if matches ! ( validation_err. kind, ValidationErrorKind :: PointerAsInt { .. } | ValidationErrorKind :: PartialPointer ) =>
276
+ {
277
+ ecx. handle_ice ( ) ; // print interpreter backtrace
278
+ bug ! ( "This validation error should be impossible in Miri: {}" , ecx. format_error( e) ) ;
279
+ }
278
280
UndefinedBehavior ( _) =>
279
281
"Undefined Behavior" ,
280
282
ResourceExhaustion ( _) =>
@@ -290,8 +292,10 @@ pub fn report_error<'tcx, 'mir>(
290
292
InvalidProgramInfo :: Layout ( ..)
291
293
) =>
292
294
"post-monomorphization error" ,
293
- kind =>
294
- bug ! ( "This error should be impossible in Miri: {kind:?}" ) ,
295
+ _ => {
296
+ ecx. handle_ice ( ) ; // print interpreter backtrace
297
+ bug ! ( "This error should be impossible in Miri: {}" , ecx. format_error( e) ) ;
298
+ }
295
299
} ;
296
300
#[ rustfmt:: skip]
297
301
let helps = match e. kind ( ) {
@@ -333,30 +337,22 @@ pub fn report_error<'tcx, 'mir>(
333
337
334
338
let stacktrace = ecx. generate_stacktrace ( ) ;
335
339
let ( stacktrace, was_pruned) = prune_stacktrace ( stacktrace, & ecx. machine ) ;
336
- let ( e, backtrace) = e. into_parts ( ) ;
337
- backtrace. print_backtrace ( ) ;
338
-
339
- // We want to dump the allocation if this is `InvalidUninitBytes`. Since `add_args` consumes
340
- // the `InterpError`, we extract the variables it before that.
341
- let extra = match e {
342
- UndefinedBehavior ( UndefinedBehaviorInfo :: InvalidUninitBytes ( Some ( ( alloc_id, access) ) ) ) =>
343
- Some ( ( alloc_id, access) ) ,
344
- _ => None ,
345
- } ;
346
340
347
- // FIXME(fee1-dead), HACK: we want to use the error as title therefore we can just extract the
348
- // label and arguments from the InterpError.
349
- let e = {
350
- let handler = & ecx. tcx . sess . parse_sess . span_diagnostic ;
351
- let mut diag = ecx. tcx . sess . struct_allow ( "" ) ;
352
- let msg = e. diagnostic_message ( ) ;
353
- e. add_args ( handler, & mut diag) ;
354
- let s = handler. eagerly_translate_to_string ( msg, diag. args ( ) ) ;
355
- diag. cancel ( ) ;
356
- s
357
- } ;
341
+ // We want to dump the allocation if this is `InvalidUninitBytes`. Since `format_error` consumes `e`, we compute the outut early.
342
+ let mut extra = String :: new ( ) ;
343
+ match e. kind ( ) {
344
+ UndefinedBehavior ( UndefinedBehaviorInfo :: InvalidUninitBytes ( Some ( ( alloc_id, access) ) ) ) => {
345
+ writeln ! (
346
+ extra,
347
+ "Uninitialized memory occurred at {alloc_id:?}{range:?}, in this allocation:" ,
348
+ range = access. bad,
349
+ ) . unwrap ( ) ;
350
+ writeln ! ( extra, "{:?}" , ecx. dump_alloc( * alloc_id) ) . unwrap ( ) ;
351
+ }
352
+ _ => { }
353
+ }
358
354
359
- msg. insert ( 0 , e ) ;
355
+ msg. insert ( 0 , ecx . format_error ( e ) ) ;
360
356
361
357
report_msg (
362
358
DiagLevel :: Error ,
@@ -375,6 +371,8 @@ pub fn report_error<'tcx, 'mir>(
375
371
) ;
376
372
}
377
373
374
+ eprint ! ( "{extra}" ) ; // newlines are already in the string
375
+
378
376
// Debug-dump all locals.
379
377
for ( i, frame) in ecx. active_thread_stack ( ) . iter ( ) . enumerate ( ) {
380
378
trace ! ( "-------------------" ) ;
@@ -385,15 +383,6 @@ pub fn report_error<'tcx, 'mir>(
385
383
}
386
384
}
387
385
388
- // Extra output to help debug specific issues.
389
- if let Some ( ( alloc_id, access) ) = extra {
390
- eprintln ! (
391
- "Uninitialized memory occurred at {alloc_id:?}{range:?}, in this allocation:" ,
392
- range = access. bad,
393
- ) ;
394
- eprintln ! ( "{:?}" , ecx. dump_alloc( alloc_id) ) ;
395
- }
396
-
397
386
None
398
387
}
399
388
0 commit comments