]> git.lizzy.rs Git - rust.git/blob - src/error.rs
c82b7fa1ca61a740bc57e4cd402288d0376306ac
[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         offset: usize,
12         size: usize,
13         len: usize,
14     },
15     ReadPointerAsBytes,
16     ReadBytesAsPointer,
17     InvalidPointerMath,
18     ReadUndefBytes,
19     InvalidBoolOp(mir::BinOp),
20     Unimplemented(String),
21 }
22
23 pub type EvalResult<T> = Result<T, EvalError>;
24
25 impl Error for EvalError {
26     fn description(&self) -> &str {
27         match *self {
28             EvalError::DanglingPointerDeref =>
29                 "dangling pointer was dereferenced",
30             EvalError::InvalidBool =>
31                 "invalid boolean value read",
32             EvalError::InvalidDiscriminant =>
33                 "invalid enum discriminant value read",
34             EvalError::PointerOutOfBounds { .. } =>
35                 "pointer offset outside bounds of allocation",
36             EvalError::ReadPointerAsBytes =>
37                 "a raw memory access tried to access part of a pointer value as raw bytes",
38             EvalError::ReadBytesAsPointer =>
39                 "attempted to interpret some raw bytes as a pointer address",
40             EvalError::InvalidPointerMath =>
41                 "attempted to do math or a comparison on pointers into different allocations",
42             EvalError::ReadUndefBytes =>
43                 "attempted to read undefined bytes",
44             EvalError::InvalidBoolOp(_) =>
45                 "invalid boolean operation",
46             EvalError::Unimplemented(ref msg) => msg,
47         }
48     }
49
50     fn cause(&self) -> Option<&Error> { None }
51 }
52
53 impl fmt::Display for EvalError {
54     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55         match *self {
56             EvalError::PointerOutOfBounds { offset, size, len } => write!(f, "pointer offset ({} + {}) outside bounds ({}) of allocation", offset, size, len),
57             _ => write!(f, "{}", self.description()),
58         }
59     }
60 }