1- use super :: { AllocId , CheckInAllocMsg , Pointer , RawConst , ScalarMaybeUndef } ;
1+ use super :: { AllocId , Pointer , RawConst , ScalarMaybeUndef } ;
22
33use crate :: mir:: interpret:: ConstValue ;
44use crate :: ty:: layout:: LayoutError ;
@@ -285,7 +285,7 @@ pub enum InvalidProgramInfo<'tcx> {
285285 TransmuteSizeDiff ( Ty < ' tcx > , Ty < ' tcx > ) ,
286286}
287287
288- impl fmt:: Debug for InvalidProgramInfo < ' _ > {
288+ impl fmt:: Display for InvalidProgramInfo < ' _ > {
289289 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
290290 use InvalidProgramInfo :: * ;
291291 match self {
@@ -304,14 +304,38 @@ impl fmt::Debug for InvalidProgramInfo<'_> {
304304 }
305305}
306306
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+
307333/// Error information for when the program caused Undefined Behavior.
308334pub enum UndefinedBehaviorInfo {
309335 /// Free-form case. Only for errors that are never caught!
310336 Ub ( String ) ,
311337 /// Unreachable code was executed.
312338 Unreachable ,
313- /// An enum discriminant was set to a value which was outside the range of valid values.
314- InvalidDiscriminant ( ScalarMaybeUndef ) ,
315339 /// A slice/array index projection went out-of-bounds.
316340 BoundsCheckFailed {
317341 len : u64 ,
@@ -335,17 +359,15 @@ pub enum UndefinedBehaviorInfo {
335359 msg : CheckInAllocMsg ,
336360 allocation_size : Size ,
337361 } ,
362+ /// Using an integer as a pointer in the wrong way.
363+ DanglingIntPointer ( u64 , CheckInAllocMsg ) ,
338364 /// Used a pointer with bad alignment.
339365 AlignmentCheckFailed {
340366 required : Align ,
341367 has : Align ,
342368 } ,
343- /// Using an integer as a pointer in the wrong way.
344- InvalidIntPointerUsage ( u64 ) ,
345369 /// Writing to read-only memory.
346370 WriteToReadOnly ( AllocId ) ,
347- /// Using a pointer-not-to-a-function as function pointer.
348- InvalidFunctionPointer ( Pointer ) ,
349371 // Trying to access the data behind a function pointer.
350372 DerefFunctionPointer ( AllocId ) ,
351373 /// The value validity check found a problem.
@@ -356,6 +378,10 @@ pub enum UndefinedBehaviorInfo {
356378 InvalidBool ( u8 ) ,
357379 /// Using a non-character `u32` as character.
358380 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 ) ,
359385 /// Using uninitialized data where it is not allowed.
360386 InvalidUndefBytes ( Option < Pointer > ) ,
361387 /// Working with a local that is not currently live.
@@ -367,29 +393,26 @@ pub enum UndefinedBehaviorInfo {
367393 } ,
368394}
369395
370- impl fmt:: Debug for UndefinedBehaviorInfo {
396+ impl fmt:: Display for UndefinedBehaviorInfo {
371397 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
372398 use UndefinedBehaviorInfo :: * ;
373399 match self {
374400 Ub ( msg) => write ! ( f, "{}" , msg) ,
375401 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+ }
382405 DivisionByZero => write ! ( f, "dividing by zero" ) ,
383406 RemainderByZero => write ! ( f, "calculating the remainder with a divisor of zero" ) ,
384407 PointerArithOverflow => write ! ( f, "overflowing in-bounds pointer arithmetic" ) ,
385408 InvalidMeta ( msg) => write ! ( f, "invalid metadata in wide pointer: {}" , msg) ,
386409 UnterminatedCString ( p) => write ! (
387410 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" ,
389412 p,
390413 ) ,
391414 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)
393416 }
394417 PointerOutOfBounds { ptr, msg, allocation_size } => write ! (
395418 f,
@@ -400,25 +423,34 @@ impl fmt::Debug for UndefinedBehaviorInfo {
400423 ptr. alloc_id,
401424 allocation_size. bytes( )
402425 ) ,
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+ }
405432 AlignmentCheckFailed { required, has } => write ! (
406433 f,
407434 "accessing memory with alignment {}, but alignment {} is required" ,
408435 has. bytes( ) ,
409436 required. bytes( )
410437 ) ,
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) ,
412448 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)
414450 }
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) ,
419451 InvalidUndefBytes ( Some ( p) ) => write ! (
420452 f,
421- "reading uninitialized memory at {:? }, but this operation requires initialized memory" ,
453+ "reading uninitialized memory at {}, but this operation requires initialized memory" ,
422454 p
423455 ) ,
424456 InvalidUndefBytes ( None ) => write ! (
@@ -455,7 +487,7 @@ pub enum UnsupportedOpInfo {
455487 ReadBytesAsPointer ,
456488}
457489
458- impl fmt:: Debug for UnsupportedOpInfo {
490+ impl fmt:: Display for UnsupportedOpInfo {
459491 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
460492 use UnsupportedOpInfo :: * ;
461493 match self {
@@ -481,7 +513,7 @@ pub enum ResourceExhaustionInfo {
481513 StepLimitReached ,
482514}
483515
484- impl fmt:: Debug for ResourceExhaustionInfo {
516+ impl fmt:: Display for ResourceExhaustionInfo {
485517 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
486518 use ResourceExhaustionInfo :: * ;
487519 match self {
@@ -499,7 +531,6 @@ impl fmt::Debug for ResourceExhaustionInfo {
499531pub trait AsAny : Any {
500532 fn as_any ( & self ) -> & dyn Any ;
501533}
502-
503534impl < T : Any > AsAny for T {
504535 #[ inline( always) ]
505536 fn as_any ( & self ) -> & dyn Any {
@@ -508,7 +539,7 @@ impl<T: Any> AsAny for T {
508539}
509540
510541/// 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 { }
512543impl MachineStopType for String { }
513544
514545impl dyn MachineStopType {
@@ -538,21 +569,21 @@ pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
538569
539570impl fmt:: Display for InterpError < ' _ > {
540571 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+ }
543580 }
544581}
545582
583+ // Forward `Debug` to `Display`, so it does not look awful.
546584impl fmt:: Debug for InterpError < ' _ > {
547585 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)
556587 }
557588}
558589
0 commit comments