stable_mir/
error.rs

1//! When things go wrong, we need some error handling.
2//! There are a few different types of errors in StableMIR:
3//!
4//! - [CompilerError]: This represents errors that can be raised when invoking the compiler.
5//! - [Error]: Generic error that represents the reason why a request that could not be fulfilled.
6
7use std::fmt::{Debug, Display, Formatter};
8use std::{fmt, io};
9
10use rustc_smir::bridge::SmirError;
11
12macro_rules! error {
13     ($fmt: literal $(,)?) => { Error(format!($fmt)) };
14     ($fmt: literal, $($arg:tt)*) => { Error(format!($fmt, $($arg)*)) };
15}
16
17pub(crate) use error;
18
19/// An error type used to represent an error that has already been reported by the compiler.
20#[derive(Clone, Copy, PartialEq, Eq)]
21pub enum CompilerError<T> {
22    /// Compilation failed, either due to normal errors or ICE.
23    Failed,
24    /// Compilation was interrupted.
25    Interrupted(T),
26    /// Compilation skipped. This happens when users invoke rustc to retrieve information such as
27    /// --version.
28    Skipped,
29}
30
31/// A generic error to represent an API request that cannot be fulfilled.
32#[derive(Clone, Debug, Eq, PartialEq)]
33pub struct Error(pub(crate) String);
34
35impl SmirError for Error {
36    fn new(msg: String) -> Self {
37        Self(msg)
38    }
39
40    fn from_internal<T: Debug>(err: T) -> Self {
41        Self(format!("{err:?}"))
42    }
43}
44
45impl From<&str> for Error {
46    fn from(value: &str) -> Self {
47        Self(value.into())
48    }
49}
50
51impl Display for Error {
52    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
53        Display::fmt(&self.0, f)
54    }
55}
56
57impl<T> Display for CompilerError<T>
58where
59    T: Display,
60{
61    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
62        match self {
63            CompilerError::Failed => write!(f, "Compilation Failed"),
64            CompilerError::Interrupted(reason) => write!(f, "Compilation Interrupted: {reason}"),
65            CompilerError::Skipped => write!(f, "Compilation Skipped"),
66        }
67    }
68}
69
70impl<T> Debug for CompilerError<T>
71where
72    T: Debug,
73{
74    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
75        match self {
76            CompilerError::Failed => write!(f, "Compilation Failed"),
77            CompilerError::Interrupted(reason) => write!(f, "Compilation Interrupted: {reason:?}"),
78            CompilerError::Skipped => write!(f, "Compilation Skipped"),
79        }
80    }
81}
82
83impl std::error::Error for Error {}
84
85impl<T> std::error::Error for CompilerError<T> where T: Display + Debug {}
86
87impl From<io::Error> for Error {
88    fn from(value: io::Error) -> Self {
89        Error(value.to_string())
90    }
91}