]>
Commit | Line | Data |
---|---|---|
dfeec247 XL |
1 | use std::error::Error; |
2 | use std::fmt; | |
3 | ||
ba9703b0 | 4 | use rustc_middle::mir::AssertKind; |
74b04a01 XL |
5 | use rustc_span::Symbol; |
6 | ||
dfeec247 | 7 | use super::InterpCx; |
ba9703b0 | 8 | use crate::interpret::{ConstEvalErr, InterpErrorInfo, Machine}; |
74b04a01 XL |
9 | |
10 | /// The CTFE machine has some custom error kinds. | |
dfeec247 | 11 | #[derive(Clone, Debug)] |
74b04a01 | 12 | pub enum ConstEvalErrKind { |
dfeec247 XL |
13 | NeedsRfc(String), |
14 | ConstAccessesStatic, | |
ba9703b0 | 15 | ModifiedGlobal, |
74b04a01 XL |
16 | AssertFailure(AssertKind<u64>), |
17 | Panic { msg: Symbol, line: u32, col: u32, file: Symbol }, | |
dfeec247 XL |
18 | } |
19 | ||
74b04a01 | 20 | // The errors become `MachineStop` with plain strings when being raised. |
ba9703b0 | 21 | // `ConstEvalErr` (in `librustc_middle/mir/interpret/error.rs`) knows to |
74b04a01 XL |
22 | // handle these. |
23 | impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalErrKind { | |
dfeec247 | 24 | fn into(self) -> InterpErrorInfo<'tcx> { |
ba9703b0 | 25 | err_machine_stop!(self.to_string()).into() |
dfeec247 XL |
26 | } |
27 | } | |
28 | ||
74b04a01 | 29 | impl fmt::Display for ConstEvalErrKind { |
dfeec247 | 30 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
74b04a01 | 31 | use self::ConstEvalErrKind::*; |
dfeec247 XL |
32 | match *self { |
33 | NeedsRfc(ref msg) => { | |
34 | write!(f, "\"{}\" needs an rfc before being allowed inside constants", msg) | |
35 | } | |
36 | ConstAccessesStatic => write!(f, "constant accesses static"), | |
ba9703b0 XL |
37 | ModifiedGlobal => { |
38 | write!(f, "modifying a static's initial value from another static's initializer") | |
39 | } | |
74b04a01 XL |
40 | AssertFailure(ref msg) => write!(f, "{:?}", msg), |
41 | Panic { msg, line, col, file } => { | |
42 | write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col) | |
43 | } | |
dfeec247 XL |
44 | } |
45 | } | |
46 | } | |
47 | ||
74b04a01 | 48 | impl Error for ConstEvalErrKind {} |
dfeec247 XL |
49 | |
50 | /// Turn an interpreter error into something to report to the user. | |
51 | /// As a side-effect, if RUSTC_CTFE_BACKTRACE is set, this prints the backtrace. | |
52 | /// Should be called only if the error is actually going to to be reported! | |
ba9703b0 | 53 | pub fn error_to_const_error<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>>( |
dfeec247 | 54 | ecx: &InterpCx<'mir, 'tcx, M>, |
f9f354fc | 55 | error: InterpErrorInfo<'tcx>, |
dfeec247 XL |
56 | ) -> ConstEvalErr<'tcx> { |
57 | error.print_backtrace(); | |
ba9703b0 | 58 | let stacktrace = ecx.generate_stacktrace(); |
dfeec247 XL |
59 | ConstEvalErr { error: error.kind, stacktrace, span: ecx.tcx.span } |
60 | } |