Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f948995

Browse files
committed
InterpError printing really is more Display than Debug
also tweak InvalidDiscriminant message
1 parent 671bc08 commit f948995

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

src/librustc_middle/mir/interpret/error.rs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
@@ -310,8 +310,6 @@ pub enum UndefinedBehaviorInfo {
310310
Ub(String),
311311
/// Unreachable code was executed.
312312
Unreachable,
313-
/// An enum discriminant was set to a value which was outside the range of valid values.
314-
InvalidDiscriminant(ScalarMaybeUndef),
315313
/// A slice/array index projection went out-of-bounds.
316314
BoundsCheckFailed {
317315
len: u64,
@@ -356,6 +354,8 @@ pub enum UndefinedBehaviorInfo {
356354
InvalidBool(u8),
357355
/// Using a non-character `u32` as character.
358356
InvalidChar(u32),
357+
/// An enum discriminant was set to a value which was outside the range of valid values.
358+
InvalidDiscriminant(ScalarMaybeUndef),
359359
/// Using uninitialized data where it is not allowed.
360360
InvalidUndefBytes(Option<Pointer>),
361361
/// Working with a local that is not currently live.
@@ -367,29 +367,26 @@ pub enum UndefinedBehaviorInfo {
367367
},
368368
}
369369

370-
impl fmt::Debug for UndefinedBehaviorInfo {
370+
impl fmt::Display for UndefinedBehaviorInfo {
371371
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
372372
use UndefinedBehaviorInfo::*;
373373
match self {
374374
Ub(msg) => write!(f, "{}", msg),
375375
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-
),
376+
BoundsCheckFailed { ref len, ref index } => {
377+
write!(f, "indexing out of bounds: the len is {} but the index is {}", len, index)
378+
}
382379
DivisionByZero => write!(f, "dividing by zero"),
383380
RemainderByZero => write!(f, "calculating the remainder with a divisor of zero"),
384381
PointerArithOverflow => write!(f, "overflowing in-bounds pointer arithmetic"),
385382
InvalidMeta(msg) => write!(f, "invalid metadata in wide pointer: {}", msg),
386383
UnterminatedCString(p) => write!(
387384
f,
388-
"reading a null-terminated string starting at {:?} with no null found before end of allocation",
385+
"reading a null-terminated string starting at {} with no null found before end of allocation",
389386
p,
390387
),
391388
PointerUseAfterFree(a) => {
392-
write!(f, "pointer to {:?} was dereferenced after this allocation got freed", a)
389+
write!(f, "pointer to {} was dereferenced after this allocation got freed", a)
393390
}
394391
PointerOutOfBounds { ptr, msg, allocation_size } => write!(
395392
f,
@@ -408,17 +405,18 @@ impl fmt::Debug for UndefinedBehaviorInfo {
408405
has.bytes(),
409406
required.bytes()
410407
),
411-
WriteToReadOnly(a) => write!(f, "writing to {:?} which is read-only", a),
408+
WriteToReadOnly(a) => write!(f, "writing to {} which is read-only", a),
412409
InvalidFunctionPointer(p) => {
413-
write!(f, "using {:?} as function pointer but it does not point to a function", p)
410+
write!(f, "using {} as function pointer but it does not point to a function", p)
414411
}
415-
DerefFunctionPointer(a) => write!(f, "accessing {:?} which contains a function", a),
412+
DerefFunctionPointer(a) => write!(f, "accessing {} which contains a function", a),
416413
ValidationFailure(ref err) => write!(f, "type validation failed: {}", err),
417414
InvalidBool(b) => write!(f, "interpreting an invalid 8-bit value as a bool: {}", b),
418415
InvalidChar(c) => write!(f, "interpreting an invalid 32-bit value as a char: {}", c),
416+
InvalidDiscriminant(val) => write!(f, "enum value has invalid discriminant: {}", val),
419417
InvalidUndefBytes(Some(p)) => write!(
420418
f,
421-
"reading uninitialized memory at {:?}, but this operation requires initialized memory",
419+
"reading uninitialized memory at {}, but this operation requires initialized memory",
422420
p
423421
),
424422
InvalidUndefBytes(None) => write!(
@@ -455,7 +453,7 @@ pub enum UnsupportedOpInfo {
455453
ReadBytesAsPointer,
456454
}
457455

458-
impl fmt::Debug for UnsupportedOpInfo {
456+
impl fmt::Display for UnsupportedOpInfo {
459457
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
460458
use UnsupportedOpInfo::*;
461459
match self {
@@ -481,7 +479,7 @@ pub enum ResourceExhaustionInfo {
481479
StepLimitReached,
482480
}
483481

484-
impl fmt::Debug for ResourceExhaustionInfo {
482+
impl fmt::Display for ResourceExhaustionInfo {
485483
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
486484
use ResourceExhaustionInfo::*;
487485
match self {
@@ -499,7 +497,6 @@ impl fmt::Debug for ResourceExhaustionInfo {
499497
pub trait AsAny: Any {
500498
fn as_any(&self) -> &dyn Any;
501499
}
502-
503500
impl<T: Any> AsAny for T {
504501
#[inline(always)]
505502
fn as_any(&self) -> &dyn Any {
@@ -508,7 +505,7 @@ impl<T: Any> AsAny for T {
508505
}
509506

510507
/// A trait for machine-specific errors (or other "machine stop" conditions).
511-
pub trait MachineStopType: AsAny + fmt::Debug + Send {}
508+
pub trait MachineStopType: AsAny + fmt::Display + Send {}
512509
impl MachineStopType for String {}
513510

514511
impl dyn MachineStopType {
@@ -538,21 +535,21 @@ pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
538535

539536
impl fmt::Display for InterpError<'_> {
540537
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
541-
// Forward `Display` to `Debug`.
542-
fmt::Debug::fmt(self, f)
538+
use InterpError::*;
539+
match *self {
540+
Unsupported(ref msg) => write!(f, "{}", msg),
541+
InvalidProgram(ref msg) => write!(f, "{}", msg),
542+
UndefinedBehavior(ref msg) => write!(f, "{}", msg),
543+
ResourceExhaustion(ref msg) => write!(f, "{}", msg),
544+
MachineStop(ref msg) => write!(f, "{}", msg),
545+
}
543546
}
544547
}
545548

549+
// Forward `Debug` to `Display`, so it does not look awful.
546550
impl fmt::Debug for InterpError<'_> {
547551
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-
}
552+
fmt::Display::fmt(self, f)
556553
}
557554
}
558555

src/librustc_mir/transform/const_prop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ macro_rules! throw_machine_stop_str {
4343
// We make a new local type for it. The type itself does not carry any information,
4444
// but its vtable (for the `MachineStopType` trait) does.
4545
struct Zst;
46-
// Debug-printing this type shows the desired string.
47-
impl std::fmt::Debug for Zst {
46+
// Printing this type shows the desired string.
47+
impl std::fmt::Display for Zst {
4848
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4949
write!(f, $($tt)*)
5050
}

0 commit comments

Comments
 (0)