]> git.lizzy.rs Git - rust.git/blob - src/error.rs
replace `panic!`s with `Result`
[rust.git] / src / error.rs
1 use std::error::Error;
2 use std::fmt;
3 use rustc::mir::repr as mir;
4
5 #[derive(Clone, Debug)]
6 pub enum EvalError {
7     DanglingPointerDeref,
8     InvalidBool,
9     InvalidDiscriminant,
10     PointerOutOfBounds,
11     ReadPointerAsBytes,
12     ReadBytesAsPointer,
13     InvalidPointerMath,
14     ReadUndefBytes,
15     InvalidBoolOp(mir::BinOp),
16     Unimplemented(String),
17 }
18
19 pub type EvalResult<T> = Result<T, EvalError>;
20
21 impl Error for EvalError {
22     fn description(&self) -> &str {
23         match *self {
24             EvalError::DanglingPointerDeref =>
25                 "dangling pointer was dereferenced",
26             EvalError::InvalidBool =>
27                 "invalid boolean value read",
28             EvalError::InvalidDiscriminant =>
29                 "invalid enum discriminant value read",
30             EvalError::PointerOutOfBounds =>
31                 "pointer offset outside bounds of allocation",
32             EvalError::ReadPointerAsBytes =>
33                 "a raw memory access tried to access part of a pointer value as raw bytes",
34             EvalError::ReadBytesAsPointer =>
35                 "attempted to interpret some raw bytes as a pointer address",
36             EvalError::InvalidPointerMath =>
37                 "attempted to do math or a comparison on pointers into different allocations",
38             EvalError::ReadUndefBytes =>
39                 "attempted to read undefined bytes",
40             EvalError::InvalidBoolOp(_) =>
41                 "invalid boolean operation",
42             EvalError::Unimplemented(ref msg) => msg,
43         }
44     }
45
46     fn cause(&self) -> Option<&Error> { None }
47 }
48
49 impl fmt::Display for EvalError {
50     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51         write!(f, "{}", self.description())
52     }
53 }