4 use rustc_middle::mir::AssertKind;
5 use rustc_span::Symbol;
8 use crate::interpret::{ConstEvalErr, InterpErrorInfo, Machine};
10 /// The CTFE machine has some custom error kinds.
11 #[derive(Clone, Debug)]
12 pub enum ConstEvalErrKind {
16 AssertFailure(AssertKind<u64>),
17 Panic { msg: Symbol, line: u32, col: u32, file: Symbol },
20 // The errors become `MachineStop` with plain strings when being raised.
21 // `ConstEvalErr` (in `librustc_middle/mir/interpret/error.rs`) knows to
23 impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalErrKind {
24 fn into(self) -> InterpErrorInfo<'tcx> {
25 err_machine_stop!(self.to_string()).into()
29 impl fmt::Display for ConstEvalErrKind {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 use self::ConstEvalErrKind::*;
33 NeedsRfc(ref msg) => {
34 write!(f, "\"{}\" needs an rfc before being allowed inside constants", msg)
36 ConstAccessesStatic => write!(f, "constant accesses static"),
38 write!(f, "modifying a static's initial value from another static's initializer")
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)
48 impl Error for ConstEvalErrKind {}
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!
53 pub fn error_to_const_error<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>>(
54 ecx: &InterpCx<'mir, 'tcx, M>,
55 error: InterpErrorInfo<'tcx>,
56 ) -> ConstEvalErr<'tcx> {
57 error.print_backtrace();
58 let stacktrace = ecx.generate_stacktrace();
59 ConstEvalErr { error: error.kind, stacktrace, span: ecx.cur_span() }