]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/mir/interpret/error.rs
implementing Debug for UnsupportedInfo
[rust.git] / src / librustc / mir / interpret / error.rs
index c28582f1ab08ddb674695d21efed9d646527e7c8..e519a83c026678801c9abe080dca210b89307c7e 100644 (file)
@@ -425,171 +425,180 @@ pub enum UnsupportedInfo<'tcx> {
     PathNotFound(Vec<String>),
 }
 
-#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
-pub enum ResourceExhaustionInfo {
-    StackFrameLimitReached,
-    InfiniteLoop,
-}
-
-impl fmt::Debug for ResourceExhaustionInfo {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        use ResourceExhaustionInfo::*;
-        match self {
-            StackFrameLimitReached =>
-                write!(f, "reached the configured maximum number of stack frames"),
-            InfiniteLoop =>
-                write!(f, "duplicate interpreter state observed here, const evaluation will never \
-                    terminate"),
-        }
-    }
-}
-
-#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
-pub enum InterpError<'tcx> {
-    /// The program panicked.
-    Panic(PanicMessage<u64>),
-    /// The program caused undefined behavior.
-    UndefinedBehaviour(UndefinedBehaviourInfo),
-    /// The program did something the interpreter does not support (some of these *might* be UB
-    /// but the interpreter is not sure).
-    Unsupported(UnsupportedInfo<'tcx>),
-    /// The program was invalid (ill-typed, not sufficiently monomorphized, ...).
-    InvalidProgram(InvalidProgramInfo<'tcx>),
-    /// The program exhausted the interpreter's resources (stack/heap too big,
-    /// execution takes too long, ..).
-    ResourceExhaustion(ResourceExhaustionInfo),
-    /// Not actually an interpreter error -- used to signal that execution has exited
-    /// with the given status code.  Used by Miri, but not by CTFE.
-    Exit(i32),
-}
-
-pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
-
-impl fmt::Display for InterpError<'_> {
+impl fmt::Debug for UnsupportedInfo<'tcx> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        // Forward `Display` to `Debug`
-        write!(f, "{:?}", self)
-    }
-}
-
-impl fmt::Debug for InterpError<'_> {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        use InterpError::*;
         use UnsupportedInfo::*;
-        match *self {
-            Unsupported(PointerOutOfBounds { ptr, msg, allocation_size }) => {
+        match self {
+            PointerOutOfBounds { ptr, msg, allocation_size } => {
                 write!(f, "{} failed: pointer must be in-bounds at offset {}, \
                           but is outside bounds of allocation {} which has size {}",
                     msg, ptr.offset.bytes(), ptr.alloc_id, allocation_size.bytes())
             },
-            Unsupported(ValidationFailure(ref err)) => {
+            ValidationFailure(ref err) => {
                 write!(f, "type validation failed: {}", err)
             }
-            Unsupported(NoMirFor(ref func)) => write!(f, "no mir for `{}`", func),
-            Unsupported(FunctionAbiMismatch(caller_abi, callee_abi)) =>
+            NoMirFor(ref func) => write!(f, "no mir for `{}`", func),
+            FunctionAbiMismatch(caller_abi, callee_abi) =>
                 write!(f, "tried to call a function with ABI {:?} using caller ABI {:?}",
                     callee_abi, caller_abi),
-            Unsupported(FunctionArgMismatch(caller_ty, callee_ty)) =>
+            FunctionArgMismatch(caller_ty, callee_ty) =>
                 write!(f, "tried to call a function with argument of type {:?} \
                            passing data of type {:?}",
                     callee_ty, caller_ty),
-            Unsupported(FunctionRetMismatch(caller_ty, callee_ty)) =>
+            FunctionRetMismatch(caller_ty, callee_ty) =>
                 write!(f, "tried to call a function with return type {:?} \
                            passing return place of type {:?}",
                     callee_ty, caller_ty),
-            Unsupported(FunctionArgCountMismatch) =>
+            FunctionArgCountMismatch =>
                 write!(f, "tried to call a function with incorrect number of arguments"),
-            Unsupported(ReallocatedWrongMemoryKind(ref old, ref new)) =>
+            ReallocatedWrongMemoryKind(ref old, ref new) =>
                 write!(f, "tried to reallocate memory from {} to {}", old, new),
-            Unsupported(DeallocatedWrongMemoryKind(ref old, ref new)) =>
+            DeallocatedWrongMemoryKind(ref old, ref new) =>
                 write!(f, "tried to deallocate {} memory but gave {} as the kind", old, new),
-            Unsupported(InvalidChar(c)) =>
+            InvalidChar(c) =>
                 write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c),
-            Unsupported(AlignmentCheckFailed { required, has }) =>
+            AlignmentCheckFailed { required, has } =>
                write!(f, "tried to access memory with alignment {}, but alignment {} is required",
                       has.bytes(), required.bytes()),
-            Unsupported(TypeNotPrimitive(ty)) =>
+            TypeNotPrimitive(ty) =>
                 write!(f, "expected primitive type, got {}", ty),
-            Unsupported(PathNotFound(ref path)) =>
+            PathNotFound(ref path) =>
                 write!(f, "Cannot find path {:?}", path),
-            Unsupported(IncorrectAllocationInformation(size, size2, align, align2)) =>
+            IncorrectAllocationInformation(size, size2, align, align2) =>
                 write!(f, "incorrect alloc info: expected size {} and align {}, \
                            got size {} and align {}",
                     size.bytes(), align.bytes(), size2.bytes(), align2.bytes()),
-            Unsupported(InvalidDiscriminant(val)) =>
+            InvalidDiscriminant(val) =>
                 write!(f, "encountered invalid enum discriminant {}", val),
-            Unsupported(InvalidMemoryAccess) =>
+            InvalidMemoryAccess =>
                 write!(f, "tried to access memory through an invalid pointer"),
-            Unsupported(DanglingPointerDeref) =>
+            DanglingPointerDeref =>
                 write!(f, "dangling pointer was dereferenced"),
-            Unsupported(DoubleFree) =>
+            DoubleFree =>
                 write!(f, "tried to deallocate dangling pointer"),
-            Unsupported(InvalidFunctionPointer) =>
+            InvalidFunctionPointer =>
                 write!(f, "tried to use a function pointer after offsetting it"),
-            Unsupported(InvalidBool) =>
+            InvalidBool =>
                 write!(f, "invalid boolean value read"),
-            Unsupported(InvalidNullPointerUsage) =>
+            InvalidNullPointerUsage =>
                 write!(f, "invalid use of NULL pointer"),
-            Unsupported(ReadPointerAsBytes) =>
+            ReadPointerAsBytes =>
                 write!(f, "a raw memory access tried to access part of a pointer value as raw \
                     bytes"),
-            Unsupported(ReadBytesAsPointer) =>
+            ReadBytesAsPointer =>
                 write!(f, "a memory access tried to interpret some bytes as a pointer"),
-            Unsupported(ReadForeignStatic) =>
+            ReadForeignStatic =>
                 write!(f, "tried to read from foreign (extern) static"),
-            Unsupported(InvalidPointerMath) =>
+            InvalidPointerMath =>
                 write!(f, "attempted to do invalid arithmetic on pointers that would leak base \
                     addresses, e.g., comparing pointers into different allocations"),
-            Unsupported(DeadLocal) =>
+            DeadLocal =>
                 write!(f, "tried to access a dead local variable"),
-            Unsupported(DerefFunctionPointer) =>
+            DerefFunctionPointer =>
                 write!(f, "tried to dereference a function pointer"),
-            Unsupported(ExecuteMemory) =>
+            ExecuteMemory =>
                 write!(f, "tried to treat a memory pointer as a function pointer"),
-            Unsupported(OutOfTls) =>
+            OutOfTls =>
                 write!(f, "reached the maximum number of representable TLS keys"),
-            Unsupported(TlsOutOfBounds) =>
+            TlsOutOfBounds =>
                 write!(f, "accessed an invalid (unallocated) TLS key"),
-            Unsupported(CalledClosureAsFunction) =>
+            CalledClosureAsFunction =>
                 write!(f, "tried to call a closure through a function pointer"),
-            Unsupported(VtableForArgumentlessMethod) =>
+            VtableForArgumentlessMethod =>
                 write!(f, "tried to call a vtable function without arguments"),
-            Unsupported(ModifiedConstantMemory) =>
+            ModifiedConstantMemory =>
                 write!(f, "tried to modify constant memory"),
-            Unsupported(ModifiedStatic) =>
+            ModifiedStatic =>
                 write!(f, "tried to modify a static's initial value from another static's \
                     initializer"),
-            Unsupported(AssumptionNotHeld) =>
+            AssumptionNotHeld =>
                 write!(f, "`assume` argument was false"),
-            Unsupported(InlineAsm) =>
+            InlineAsm =>
                 write!(f, "miri does not support inline assembly"),
-            Unsupported(ReallocateNonBasePtr) =>
+            ReallocateNonBasePtr =>
                 write!(f, "tried to reallocate with a pointer not to the beginning of an \
                     existing object"),
-            Unsupported(DeallocateNonBasePtr) =>
+            DeallocateNonBasePtr =>
                 write!(f, "tried to deallocate with a pointer not to the beginning of an \
                     existing object"),
-            Unsupported(HeapAllocZeroBytes) =>
+            HeapAllocZeroBytes =>
                 write!(f, "tried to re-, de- or allocate zero bytes on the heap"),
-            Unsupported(ReadFromReturnPointer) =>
+            ReadFromReturnPointer =>
                 write!(f, "tried to read from the return pointer"),
-            Unsupported(UnimplementedTraitSelection) =>
+            UnimplementedTraitSelection =>
                 write!(f, "there were unresolved type arguments during trait selection"),
-            Unsupported(InvalidBoolOp(_)) =>
+            InvalidBoolOp(_) =>
                 write!(f, "invalid boolean operation"),
-            Unsupported(UnterminatedCString(_)) =>
+            UnterminatedCString(_) =>
                 write!(f, "attempted to get length of a null terminated string, but no null \
                     found before end of allocation"),
-            Unsupported(ReadUndefBytes(_)) =>
+            ReadUndefBytes(_) =>
                 write!(f, "attempted to read undefined bytes"),
-            Unsupported(HeapAllocNonPowerOfTwoAlignment(_)) =>
+            HeapAllocNonPowerOfTwoAlignment(_) =>
                 write!(f, "tried to re-, de-, or allocate heap memory with alignment that is \
                     not a power of two"),
-            Unsupported(MachineError(ref msg)) |
-            Unsupported(Unimplemented(ref msg)) |
-            Unsupported(AbiViolation(ref msg)) |
-            Unsupported(Intrinsic(ref msg)) =>
+            MachineError(ref msg) |
+            Unimplemented(ref msg) |
+            AbiViolation(ref msg) |
+            Intrinsic(ref msg) =>
                 write!(f, "{}", msg),
+        }
+    }
+}
+
+#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
+pub enum ResourceExhaustionInfo {
+    StackFrameLimitReached,
+    InfiniteLoop,
+}
+
+impl fmt::Debug for ResourceExhaustionInfo {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        use ResourceExhaustionInfo::*;
+        match self {
+            StackFrameLimitReached =>
+                write!(f, "reached the configured maximum number of stack frames"),
+            InfiniteLoop =>
+                write!(f, "duplicate interpreter state observed here, const evaluation will never \
+                    terminate"),
+        }
+    }
+}
+
+#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
+pub enum InterpError<'tcx> {
+    /// The program panicked.
+    Panic(PanicMessage<u64>),
+    /// The program caused undefined behavior.
+    UndefinedBehaviour(UndefinedBehaviourInfo),
+    /// The program did something the interpreter does not support (some of these *might* be UB
+    /// but the interpreter is not sure).
+    Unsupported(UnsupportedInfo<'tcx>),
+    /// The program was invalid (ill-typed, not sufficiently monomorphized, ...).
+    InvalidProgram(InvalidProgramInfo<'tcx>),
+    /// The program exhausted the interpreter's resources (stack/heap too big,
+    /// execution takes too long, ..).
+    ResourceExhaustion(ResourceExhaustionInfo),
+    /// Not actually an interpreter error -- used to signal that execution has exited
+    /// with the given status code.  Used by Miri, but not by CTFE.
+    Exit(i32),
+}
+
+pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
+
+impl fmt::Display for InterpError<'_> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        // Forward `Display` to `Debug`
+        write!(f, "{:?}", self)
+    }
+}
+
+impl fmt::Debug for InterpError<'_> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        use InterpError::*;
+        match *self {
+            Unsupported(ref msg) =>
+                write!(f, "{:?}", msg),
             InvalidProgram(ref msg) =>
                 write!(f, "{:?}", msg),
             UndefinedBehaviour(ref msg) =>