Skip to main content

rustc_public/
error.rs

1//! When things go wrong, we need some error handling.
2//! There are a few different types of errors in rustc_public:
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_public_bridge::bridge;
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(#[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for CompilerError<T> {
    #[inline]
    fn clone(&self) -> CompilerError<T> {
        match self {
            CompilerError::Failed => CompilerError::Failed,
            CompilerError::Interrupted(__self_0) =>
                CompilerError::Interrupted(::core::clone::Clone::clone(__self_0)),
            CompilerError::Skipped => CompilerError::Skipped,
        }
    }
}Clone, #[automatically_derived]
impl<T: ::core::marker::Copy> ::core::marker::Copy for CompilerError<T> { }Copy, #[automatically_derived]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for CompilerError<T> {
    #[inline]
    fn eq(&self, other: &CompilerError<T>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (CompilerError::Interrupted(__self_0),
                    CompilerError::Interrupted(__arg1_0)) =>
                    __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl<T: ::core::cmp::Eq> ::core::cmp::Eq for CompilerError<T> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<T>;
    }
}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(#[automatically_derived]
impl ::core::clone::Clone for Error {
    #[inline]
    fn clone(&self) -> Error { Error(::core::clone::Clone::clone(&self.0)) }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Error {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Error",
            &&self.0)
    }
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for Error {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<String>;
    }
}Eq, #[automatically_derived]
impl ::core::cmp::PartialEq for Error {
    #[inline]
    fn eq(&self, other: &Error) -> bool { self.0 == other.0 }
}PartialEq)]
33pub struct Error(pub(crate) String);
34
35impl bridge::Error for Error {
36    fn new(msg: String) -> Self {
37        Self(msg)
38    }
39
40    fn from_internal<T: Debug>(err: T) -> Self {
41        Self(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", err))
    })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 => f.write_fmt(format_args!("Compilation Failed"))write!(f, "Compilation Failed"),
64            CompilerError::Interrupted(reason) => f.write_fmt(format_args!("Compilation Interrupted: {0}", reason))write!(f, "Compilation Interrupted: {reason}"),
65            CompilerError::Skipped => f.write_fmt(format_args!("Compilation 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 => f.write_fmt(format_args!("Compilation Failed"))write!(f, "Compilation Failed"),
77            CompilerError::Interrupted(reason) => f.write_fmt(format_args!("Compilation Interrupted: {0:?}", reason))write!(f, "Compilation Interrupted: {reason:?}"),
78            CompilerError::Skipped => f.write_fmt(format_args!("Compilation 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}