Skip to content

Commit 9782b37

Browse files
committed
implementing Debug for UnsupportedInfo
1 parent aa3d40c commit 9782b37

File tree

1 file changed

+108
-99
lines changed

1 file changed

+108
-99
lines changed

src/librustc/mir/interpret/error.rs

Lines changed: 108 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -425,171 +425,180 @@ pub enum UnsupportedInfo<'tcx> {
425425
PathNotFound(Vec<String>),
426426
}
427427

428-
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
429-
pub enum ResourceExhaustionInfo {
430-
StackFrameLimitReached,
431-
InfiniteLoop,
432-
}
433-
434-
impl fmt::Debug for ResourceExhaustionInfo {
435-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
436-
use ResourceExhaustionInfo::*;
437-
match self {
438-
StackFrameLimitReached =>
439-
write!(f, "reached the configured maximum number of stack frames"),
440-
InfiniteLoop =>
441-
write!(f, "duplicate interpreter state observed here, const evaluation will never \
442-
terminate"),
443-
}
444-
}
445-
}
446-
447-
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
448-
pub enum InterpError<'tcx> {
449-
/// The program panicked.
450-
Panic(PanicMessage<u64>),
451-
/// The program caused undefined behavior.
452-
UndefinedBehaviour(UndefinedBehaviourInfo),
453-
/// The program did something the interpreter does not support (some of these *might* be UB
454-
/// but the interpreter is not sure).
455-
Unsupported(UnsupportedInfo<'tcx>),
456-
/// The program was invalid (ill-typed, not sufficiently monomorphized, ...).
457-
InvalidProgram(InvalidProgramInfo<'tcx>),
458-
/// The program exhausted the interpreter's resources (stack/heap too big,
459-
/// execution takes too long, ..).
460-
ResourceExhaustion(ResourceExhaustionInfo),
461-
/// Not actually an interpreter error -- used to signal that execution has exited
462-
/// with the given status code. Used by Miri, but not by CTFE.
463-
Exit(i32),
464-
}
465-
466-
pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
467-
468-
impl fmt::Display for InterpError<'_> {
428+
impl fmt::Debug for UnsupportedInfo<'tcx> {
469429
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
470-
// Forward `Display` to `Debug`
471-
write!(f, "{:?}", self)
472-
}
473-
}
474-
475-
impl fmt::Debug for InterpError<'_> {
476-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
477-
use InterpError::*;
478430
use UnsupportedInfo::*;
479-
match *self {
480-
Unsupported(PointerOutOfBounds { ptr, msg, allocation_size }) => {
431+
match self {
432+
PointerOutOfBounds { ptr, msg, allocation_size } => {
481433
write!(f, "{} failed: pointer must be in-bounds at offset {}, \
482434
but is outside bounds of allocation {} which has size {}",
483435
msg, ptr.offset.bytes(), ptr.alloc_id, allocation_size.bytes())
484436
},
485-
Unsupported(ValidationFailure(ref err)) => {
437+
ValidationFailure(ref err) => {
486438
write!(f, "type validation failed: {}", err)
487439
}
488-
Unsupported(NoMirFor(ref func)) => write!(f, "no mir for `{}`", func),
489-
Unsupported(FunctionAbiMismatch(caller_abi, callee_abi)) =>
440+
NoMirFor(ref func) => write!(f, "no mir for `{}`", func),
441+
FunctionAbiMismatch(caller_abi, callee_abi) =>
490442
write!(f, "tried to call a function with ABI {:?} using caller ABI {:?}",
491443
callee_abi, caller_abi),
492-
Unsupported(FunctionArgMismatch(caller_ty, callee_ty)) =>
444+
FunctionArgMismatch(caller_ty, callee_ty) =>
493445
write!(f, "tried to call a function with argument of type {:?} \
494446
passing data of type {:?}",
495447
callee_ty, caller_ty),
496-
Unsupported(FunctionRetMismatch(caller_ty, callee_ty)) =>
448+
FunctionRetMismatch(caller_ty, callee_ty) =>
497449
write!(f, "tried to call a function with return type {:?} \
498450
passing return place of type {:?}",
499451
callee_ty, caller_ty),
500-
Unsupported(FunctionArgCountMismatch) =>
452+
FunctionArgCountMismatch =>
501453
write!(f, "tried to call a function with incorrect number of arguments"),
502-
Unsupported(ReallocatedWrongMemoryKind(ref old, ref new)) =>
454+
ReallocatedWrongMemoryKind(ref old, ref new) =>
503455
write!(f, "tried to reallocate memory from {} to {}", old, new),
504-
Unsupported(DeallocatedWrongMemoryKind(ref old, ref new)) =>
456+
DeallocatedWrongMemoryKind(ref old, ref new) =>
505457
write!(f, "tried to deallocate {} memory but gave {} as the kind", old, new),
506-
Unsupported(InvalidChar(c)) =>
458+
InvalidChar(c) =>
507459
write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c),
508-
Unsupported(AlignmentCheckFailed { required, has }) =>
460+
AlignmentCheckFailed { required, has } =>
509461
write!(f, "tried to access memory with alignment {}, but alignment {} is required",
510462
has.bytes(), required.bytes()),
511-
Unsupported(TypeNotPrimitive(ty)) =>
463+
TypeNotPrimitive(ty) =>
512464
write!(f, "expected primitive type, got {}", ty),
513-
Unsupported(PathNotFound(ref path)) =>
465+
PathNotFound(ref path) =>
514466
write!(f, "Cannot find path {:?}", path),
515-
Unsupported(IncorrectAllocationInformation(size, size2, align, align2)) =>
467+
IncorrectAllocationInformation(size, size2, align, align2) =>
516468
write!(f, "incorrect alloc info: expected size {} and align {}, \
517469
got size {} and align {}",
518470
size.bytes(), align.bytes(), size2.bytes(), align2.bytes()),
519-
Unsupported(InvalidDiscriminant(val)) =>
471+
InvalidDiscriminant(val) =>
520472
write!(f, "encountered invalid enum discriminant {}", val),
521-
Unsupported(InvalidMemoryAccess) =>
473+
InvalidMemoryAccess =>
522474
write!(f, "tried to access memory through an invalid pointer"),
523-
Unsupported(DanglingPointerDeref) =>
475+
DanglingPointerDeref =>
524476
write!(f, "dangling pointer was dereferenced"),
525-
Unsupported(DoubleFree) =>
477+
DoubleFree =>
526478
write!(f, "tried to deallocate dangling pointer"),
527-
Unsupported(InvalidFunctionPointer) =>
479+
InvalidFunctionPointer =>
528480
write!(f, "tried to use a function pointer after offsetting it"),
529-
Unsupported(InvalidBool) =>
481+
InvalidBool =>
530482
write!(f, "invalid boolean value read"),
531-
Unsupported(InvalidNullPointerUsage) =>
483+
InvalidNullPointerUsage =>
532484
write!(f, "invalid use of NULL pointer"),
533-
Unsupported(ReadPointerAsBytes) =>
485+
ReadPointerAsBytes =>
534486
write!(f, "a raw memory access tried to access part of a pointer value as raw \
535487
bytes"),
536-
Unsupported(ReadBytesAsPointer) =>
488+
ReadBytesAsPointer =>
537489
write!(f, "a memory access tried to interpret some bytes as a pointer"),
538-
Unsupported(ReadForeignStatic) =>
490+
ReadForeignStatic =>
539491
write!(f, "tried to read from foreign (extern) static"),
540-
Unsupported(InvalidPointerMath) =>
492+
InvalidPointerMath =>
541493
write!(f, "attempted to do invalid arithmetic on pointers that would leak base \
542494
addresses, e.g., comparing pointers into different allocations"),
543-
Unsupported(DeadLocal) =>
495+
DeadLocal =>
544496
write!(f, "tried to access a dead local variable"),
545-
Unsupported(DerefFunctionPointer) =>
497+
DerefFunctionPointer =>
546498
write!(f, "tried to dereference a function pointer"),
547-
Unsupported(ExecuteMemory) =>
499+
ExecuteMemory =>
548500
write!(f, "tried to treat a memory pointer as a function pointer"),
549-
Unsupported(OutOfTls) =>
501+
OutOfTls =>
550502
write!(f, "reached the maximum number of representable TLS keys"),
551-
Unsupported(TlsOutOfBounds) =>
503+
TlsOutOfBounds =>
552504
write!(f, "accessed an invalid (unallocated) TLS key"),
553-
Unsupported(CalledClosureAsFunction) =>
505+
CalledClosureAsFunction =>
554506
write!(f, "tried to call a closure through a function pointer"),
555-
Unsupported(VtableForArgumentlessMethod) =>
507+
VtableForArgumentlessMethod =>
556508
write!(f, "tried to call a vtable function without arguments"),
557-
Unsupported(ModifiedConstantMemory) =>
509+
ModifiedConstantMemory =>
558510
write!(f, "tried to modify constant memory"),
559-
Unsupported(ModifiedStatic) =>
511+
ModifiedStatic =>
560512
write!(f, "tried to modify a static's initial value from another static's \
561513
initializer"),
562-
Unsupported(AssumptionNotHeld) =>
514+
AssumptionNotHeld =>
563515
write!(f, "`assume` argument was false"),
564-
Unsupported(InlineAsm) =>
516+
InlineAsm =>
565517
write!(f, "miri does not support inline assembly"),
566-
Unsupported(ReallocateNonBasePtr) =>
518+
ReallocateNonBasePtr =>
567519
write!(f, "tried to reallocate with a pointer not to the beginning of an \
568520
existing object"),
569-
Unsupported(DeallocateNonBasePtr) =>
521+
DeallocateNonBasePtr =>
570522
write!(f, "tried to deallocate with a pointer not to the beginning of an \
571523
existing object"),
572-
Unsupported(HeapAllocZeroBytes) =>
524+
HeapAllocZeroBytes =>
573525
write!(f, "tried to re-, de- or allocate zero bytes on the heap"),
574-
Unsupported(ReadFromReturnPointer) =>
526+
ReadFromReturnPointer =>
575527
write!(f, "tried to read from the return pointer"),
576-
Unsupported(UnimplementedTraitSelection) =>
528+
UnimplementedTraitSelection =>
577529
write!(f, "there were unresolved type arguments during trait selection"),
578-
Unsupported(InvalidBoolOp(_)) =>
530+
InvalidBoolOp(_) =>
579531
write!(f, "invalid boolean operation"),
580-
Unsupported(UnterminatedCString(_)) =>
532+
UnterminatedCString(_) =>
581533
write!(f, "attempted to get length of a null terminated string, but no null \
582534
found before end of allocation"),
583-
Unsupported(ReadUndefBytes(_)) =>
535+
ReadUndefBytes(_) =>
584536
write!(f, "attempted to read undefined bytes"),
585-
Unsupported(HeapAllocNonPowerOfTwoAlignment(_)) =>
537+
HeapAllocNonPowerOfTwoAlignment(_) =>
586538
write!(f, "tried to re-, de-, or allocate heap memory with alignment that is \
587539
not a power of two"),
588-
Unsupported(MachineError(ref msg)) |
589-
Unsupported(Unimplemented(ref msg)) |
590-
Unsupported(AbiViolation(ref msg)) |
591-
Unsupported(Intrinsic(ref msg)) =>
540+
MachineError(ref msg) |
541+
Unimplemented(ref msg) |
542+
AbiViolation(ref msg) |
543+
Intrinsic(ref msg) =>
592544
write!(f, "{}", msg),
545+
}
546+
}
547+
}
548+
549+
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
550+
pub enum ResourceExhaustionInfo {
551+
StackFrameLimitReached,
552+
InfiniteLoop,
553+
}
554+
555+
impl fmt::Debug for ResourceExhaustionInfo {
556+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
557+
use ResourceExhaustionInfo::*;
558+
match self {
559+
StackFrameLimitReached =>
560+
write!(f, "reached the configured maximum number of stack frames"),
561+
InfiniteLoop =>
562+
write!(f, "duplicate interpreter state observed here, const evaluation will never \
563+
terminate"),
564+
}
565+
}
566+
}
567+
568+
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
569+
pub enum InterpError<'tcx> {
570+
/// The program panicked.
571+
Panic(PanicMessage<u64>),
572+
/// The program caused undefined behavior.
573+
UndefinedBehaviour(UndefinedBehaviourInfo),
574+
/// The program did something the interpreter does not support (some of these *might* be UB
575+
/// but the interpreter is not sure).
576+
Unsupported(UnsupportedInfo<'tcx>),
577+
/// The program was invalid (ill-typed, not sufficiently monomorphized, ...).
578+
InvalidProgram(InvalidProgramInfo<'tcx>),
579+
/// The program exhausted the interpreter's resources (stack/heap too big,
580+
/// execution takes too long, ..).
581+
ResourceExhaustion(ResourceExhaustionInfo),
582+
/// Not actually an interpreter error -- used to signal that execution has exited
583+
/// with the given status code. Used by Miri, but not by CTFE.
584+
Exit(i32),
585+
}
586+
587+
pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
588+
589+
impl fmt::Display for InterpError<'_> {
590+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
591+
// Forward `Display` to `Debug`
592+
write!(f, "{:?}", self)
593+
}
594+
}
595+
596+
impl fmt::Debug for InterpError<'_> {
597+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
598+
use InterpError::*;
599+
match *self {
600+
Unsupported(ref msg) =>
601+
write!(f, "{:?}", msg),
593602
InvalidProgram(ref msg) =>
594603
write!(f, "{:?}", msg),
595604
UndefinedBehaviour(ref msg) =>

0 commit comments

Comments
 (0)