1
- use super :: { AllocId , CheckInAllocMsg , Pointer , RawConst , ScalarMaybeUndef } ;
1
+ use super :: { AllocId , Pointer , RawConst , ScalarMaybeUndef } ;
2
2
3
3
use crate :: mir:: interpret:: ConstValue ;
4
4
use crate :: ty:: layout:: LayoutError ;
@@ -285,7 +285,7 @@ pub enum InvalidProgramInfo<'tcx> {
285
285
TransmuteSizeDiff ( Ty < ' tcx > , Ty < ' tcx > ) ,
286
286
}
287
287
288
- impl fmt:: Debug for InvalidProgramInfo < ' _ > {
288
+ impl fmt:: Display for InvalidProgramInfo < ' _ > {
289
289
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
290
290
use InvalidProgramInfo :: * ;
291
291
match self {
@@ -304,14 +304,38 @@ impl fmt::Debug for InvalidProgramInfo<'_> {
304
304
}
305
305
}
306
306
307
+ /// Details of why a pointer had to be in-bounds.
308
+ #[ derive( Debug , Copy , Clone , RustcEncodable , RustcDecodable , HashStable ) ]
309
+ pub enum CheckInAllocMsg {
310
+ MemoryAccessTest ,
311
+ NullPointerTest ,
312
+ PointerArithmeticTest ,
313
+ InboundsTest ,
314
+ }
315
+
316
+ impl fmt:: Display for CheckInAllocMsg {
317
+ /// When this is printed as an error the context looks like this
318
+ /// "{test name} failed: pointer must be in-bounds at offset..."
319
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
320
+ write ! (
321
+ f,
322
+ "{}" ,
323
+ match * self {
324
+ CheckInAllocMsg :: MemoryAccessTest => "memory access" ,
325
+ CheckInAllocMsg :: NullPointerTest => "NULL pointer test" ,
326
+ CheckInAllocMsg :: PointerArithmeticTest => "pointer arithmetic" ,
327
+ CheckInAllocMsg :: InboundsTest => "inbounds test" ,
328
+ }
329
+ )
330
+ }
331
+ }
332
+
307
333
/// Error information for when the program caused Undefined Behavior.
308
334
pub enum UndefinedBehaviorInfo {
309
335
/// Free-form case. Only for errors that are never caught!
310
336
Ub ( String ) ,
311
337
/// Unreachable code was executed.
312
338
Unreachable ,
313
- /// An enum discriminant was set to a value which was outside the range of valid values.
314
- InvalidDiscriminant ( ScalarMaybeUndef ) ,
315
339
/// A slice/array index projection went out-of-bounds.
316
340
BoundsCheckFailed {
317
341
len : u64 ,
@@ -335,17 +359,15 @@ pub enum UndefinedBehaviorInfo {
335
359
msg : CheckInAllocMsg ,
336
360
allocation_size : Size ,
337
361
} ,
362
+ /// Using an integer as a pointer in the wrong way.
363
+ DanglingIntPointer ( u64 , CheckInAllocMsg ) ,
338
364
/// Used a pointer with bad alignment.
339
365
AlignmentCheckFailed {
340
366
required : Align ,
341
367
has : Align ,
342
368
} ,
343
- /// Using an integer as a pointer in the wrong way.
344
- InvalidIntPointerUsage ( u64 ) ,
345
369
/// Writing to read-only memory.
346
370
WriteToReadOnly ( AllocId ) ,
347
- /// Using a pointer-not-to-a-function as function pointer.
348
- InvalidFunctionPointer ( Pointer ) ,
349
371
// Trying to access the data behind a function pointer.
350
372
DerefFunctionPointer ( AllocId ) ,
351
373
/// The value validity check found a problem.
@@ -356,6 +378,10 @@ pub enum UndefinedBehaviorInfo {
356
378
InvalidBool ( u8 ) ,
357
379
/// Using a non-character `u32` as character.
358
380
InvalidChar ( u32 ) ,
381
+ /// An enum discriminant was set to a value which was outside the range of valid values.
382
+ InvalidDiscriminant ( ScalarMaybeUndef ) ,
383
+ /// Using a pointer-not-to-a-function as function pointer.
384
+ InvalidFunctionPointer ( Pointer ) ,
359
385
/// Using uninitialized data where it is not allowed.
360
386
InvalidUndefBytes ( Option < Pointer > ) ,
361
387
/// Working with a local that is not currently live.
@@ -367,29 +393,26 @@ pub enum UndefinedBehaviorInfo {
367
393
} ,
368
394
}
369
395
370
- impl fmt:: Debug for UndefinedBehaviorInfo {
396
+ impl fmt:: Display for UndefinedBehaviorInfo {
371
397
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
372
398
use UndefinedBehaviorInfo :: * ;
373
399
match self {
374
400
Ub ( msg) => write ! ( f, "{}" , msg) ,
375
401
Unreachable => write ! ( f, "entering unreachable code" ) ,
376
- InvalidDiscriminant ( val) => write ! ( f, "encountering invalid enum discriminant {}" , val) ,
377
- BoundsCheckFailed { ref len, ref index } => write ! (
378
- f,
379
- "indexing out of bounds: the len is {:?} but the index is {:?}" ,
380
- len, index
381
- ) ,
402
+ BoundsCheckFailed { ref len, ref index } => {
403
+ write ! ( f, "indexing out of bounds: the len is {} but the index is {}" , len, index)
404
+ }
382
405
DivisionByZero => write ! ( f, "dividing by zero" ) ,
383
406
RemainderByZero => write ! ( f, "calculating the remainder with a divisor of zero" ) ,
384
407
PointerArithOverflow => write ! ( f, "overflowing in-bounds pointer arithmetic" ) ,
385
408
InvalidMeta ( msg) => write ! ( f, "invalid metadata in wide pointer: {}" , msg) ,
386
409
UnterminatedCString ( p) => write ! (
387
410
f,
388
- "reading a null-terminated string starting at {:? } with no null found before end of allocation" ,
411
+ "reading a null-terminated string starting at {} with no null found before end of allocation" ,
389
412
p,
390
413
) ,
391
414
PointerUseAfterFree ( a) => {
392
- write ! ( f, "pointer to {:? } was dereferenced after this allocation got freed" , a)
415
+ write ! ( f, "pointer to {} was dereferenced after this allocation got freed" , a)
393
416
}
394
417
PointerOutOfBounds { ptr, msg, allocation_size } => write ! (
395
418
f,
@@ -400,25 +423,34 @@ impl fmt::Debug for UndefinedBehaviorInfo {
400
423
ptr. alloc_id,
401
424
allocation_size. bytes( )
402
425
) ,
403
- InvalidIntPointerUsage ( 0 ) => write ! ( f, "invalid use of NULL pointer" ) ,
404
- InvalidIntPointerUsage ( i) => write ! ( f, "invalid use of {} as a pointer" , i) ,
426
+ DanglingIntPointer ( _, CheckInAllocMsg :: NullPointerTest ) => {
427
+ write ! ( f, "NULL pointer is not allowed for this operation" )
428
+ }
429
+ DanglingIntPointer ( i, msg) => {
430
+ write ! ( f, "{} failed: 0x{:x} is not a valid pointer" , msg, i)
431
+ }
405
432
AlignmentCheckFailed { required, has } => write ! (
406
433
f,
407
434
"accessing memory with alignment {}, but alignment {} is required" ,
408
435
has. bytes( ) ,
409
436
required. bytes( )
410
437
) ,
411
- WriteToReadOnly ( a) => write ! ( f, "writing to {:?} which is read-only" , a) ,
438
+ WriteToReadOnly ( a) => write ! ( f, "writing to {} which is read-only" , a) ,
439
+ DerefFunctionPointer ( a) => write ! ( f, "accessing {} which contains a function" , a) ,
440
+ ValidationFailure ( ref err) => write ! ( f, "type validation failed: {}" , err) ,
441
+ InvalidBool ( b) => {
442
+ write ! ( f, "interpreting an invalid 8-bit value as a bool: 0x{:2x}" , b)
443
+ }
444
+ InvalidChar ( c) => {
445
+ write ! ( f, "interpreting an invalid 32-bit value as a char: 0x{:8x}" , c)
446
+ }
447
+ InvalidDiscriminant ( val) => write ! ( f, "enum value has invalid discriminant: {}" , val) ,
412
448
InvalidFunctionPointer ( p) => {
413
- write ! ( f, "using {:? } as function pointer but it does not point to a function" , p)
449
+ write ! ( f, "using {} as function pointer but it does not point to a function" , p)
414
450
}
415
- DerefFunctionPointer ( a) => write ! ( f, "accessing {:?} which contains a function" , a) ,
416
- ValidationFailure ( ref err) => write ! ( f, "type validation failed: {}" , err) ,
417
- InvalidBool ( b) => write ! ( f, "interpreting an invalid 8-bit value as a bool: {}" , b) ,
418
- InvalidChar ( c) => write ! ( f, "interpreting an invalid 32-bit value as a char: {}" , c) ,
419
451
InvalidUndefBytes ( Some ( p) ) => write ! (
420
452
f,
421
- "reading uninitialized memory at {:? }, but this operation requires initialized memory" ,
453
+ "reading uninitialized memory at {}, but this operation requires initialized memory" ,
422
454
p
423
455
) ,
424
456
InvalidUndefBytes ( None ) => write ! (
@@ -455,7 +487,7 @@ pub enum UnsupportedOpInfo {
455
487
ReadBytesAsPointer ,
456
488
}
457
489
458
- impl fmt:: Debug for UnsupportedOpInfo {
490
+ impl fmt:: Display for UnsupportedOpInfo {
459
491
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
460
492
use UnsupportedOpInfo :: * ;
461
493
match self {
@@ -481,7 +513,7 @@ pub enum ResourceExhaustionInfo {
481
513
StepLimitReached ,
482
514
}
483
515
484
- impl fmt:: Debug for ResourceExhaustionInfo {
516
+ impl fmt:: Display for ResourceExhaustionInfo {
485
517
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
486
518
use ResourceExhaustionInfo :: * ;
487
519
match self {
@@ -499,7 +531,6 @@ impl fmt::Debug for ResourceExhaustionInfo {
499
531
pub trait AsAny : Any {
500
532
fn as_any ( & self ) -> & dyn Any ;
501
533
}
502
-
503
534
impl < T : Any > AsAny for T {
504
535
#[ inline( always) ]
505
536
fn as_any ( & self ) -> & dyn Any {
@@ -508,7 +539,7 @@ impl<T: Any> AsAny for T {
508
539
}
509
540
510
541
/// A trait for machine-specific errors (or other "machine stop" conditions).
511
- pub trait MachineStopType : AsAny + fmt:: Debug + Send { }
542
+ pub trait MachineStopType : AsAny + fmt:: Display + Send { }
512
543
impl MachineStopType for String { }
513
544
514
545
impl dyn MachineStopType {
@@ -538,21 +569,21 @@ pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
538
569
539
570
impl fmt:: Display for InterpError < ' _ > {
540
571
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
541
- // Forward `Display` to `Debug`.
542
- fmt:: Debug :: fmt ( self , f)
572
+ use InterpError :: * ;
573
+ match * self {
574
+ Unsupported ( ref msg) => write ! ( f, "{}" , msg) ,
575
+ InvalidProgram ( ref msg) => write ! ( f, "{}" , msg) ,
576
+ UndefinedBehavior ( ref msg) => write ! ( f, "{}" , msg) ,
577
+ ResourceExhaustion ( ref msg) => write ! ( f, "{}" , msg) ,
578
+ MachineStop ( ref msg) => write ! ( f, "{}" , msg) ,
579
+ }
543
580
}
544
581
}
545
582
583
+ // Forward `Debug` to `Display`, so it does not look awful.
546
584
impl fmt:: Debug for InterpError < ' _ > {
547
585
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
548
- use InterpError :: * ;
549
- match * self {
550
- Unsupported ( ref msg) => write ! ( f, "{:?}" , msg) ,
551
- InvalidProgram ( ref msg) => write ! ( f, "{:?}" , msg) ,
552
- UndefinedBehavior ( ref msg) => write ! ( f, "{:?}" , msg) ,
553
- ResourceExhaustion ( ref msg) => write ! ( f, "{:?}" , msg) ,
554
- MachineStop ( ref msg) => write ! ( f, "{:?}" , msg) ,
555
- }
586
+ fmt:: Display :: fmt ( self , f)
556
587
}
557
588
}
558
589
0 commit comments