]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/const_eval/error.rs
Rollup merge of #68314 - oli-obk:true_unwind, r=eddyb
[rust.git] / src / librustc_mir / const_eval / error.rs
1 use std::error::Error;
2 use std::fmt;
3
4 use super::InterpCx;
5 use crate::interpret::{ConstEvalErr, InterpErrorInfo, Machine};
6 #[derive(Clone, Debug)]
7 pub enum ConstEvalError {
8     NeedsRfc(String),
9     ConstAccessesStatic,
10 }
11
12 impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalError {
13     fn into(self) -> InterpErrorInfo<'tcx> {
14         err_unsup!(Unsupported(self.to_string())).into()
15     }
16 }
17
18 impl fmt::Display for ConstEvalError {
19     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20         use self::ConstEvalError::*;
21         match *self {
22             NeedsRfc(ref msg) => {
23                 write!(f, "\"{}\" needs an rfc before being allowed inside constants", msg)
24             }
25             ConstAccessesStatic => write!(f, "constant accesses static"),
26         }
27     }
28 }
29
30 impl Error for ConstEvalError {}
31
32 /// Turn an interpreter error into something to report to the user.
33 /// As a side-effect, if RUSTC_CTFE_BACKTRACE is set, this prints the backtrace.
34 /// Should be called only if the error is actually going to to be reported!
35 pub fn error_to_const_error<'mir, 'tcx, M: Machine<'mir, 'tcx>>(
36     ecx: &InterpCx<'mir, 'tcx, M>,
37     mut error: InterpErrorInfo<'tcx>,
38 ) -> ConstEvalErr<'tcx> {
39     error.print_backtrace();
40     let stacktrace = ecx.generate_stacktrace(None);
41     ConstEvalErr { error: error.kind, stacktrace, span: ecx.tcx.span }
42 }