diff --git a/compiler/erg_common/error.rs b/compiler/erg_common/error.rs index 3914b602..61fee15b 100644 --- a/compiler/erg_common/error.rs +++ b/compiler/erg_common/error.rs @@ -564,6 +564,19 @@ pub trait ErrorDisplay { } } +#[macro_export] +macro_rules! impl_display_and_error { + ($Strc: ident) => { + impl std::fmt::Display for $Strc { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + $crate::error::ErrorDisplay::format(self, f) + } + } + + impl std::error::Error for $Strc {} + }; +} + pub trait MultiErrorDisplay: Stream { fn fmt_all_stderr(&self) { for err in self.iter() { diff --git a/compiler/erg_compiler/error.rs b/compiler/erg_compiler/error.rs index d9f353ff..02d903de 100644 --- a/compiler/erg_compiler/error.rs +++ b/compiler/erg_compiler/error.rs @@ -8,7 +8,10 @@ use erg_common::error::{ErrorCore, ErrorDisplay, ErrorKind::*, Location, MultiEr use erg_common::set::Set; use erg_common::traits::{Locational, Stream}; use erg_common::vis::Visibility; -use erg_common::{fmt_iter, fmt_option_map, fmt_vec, impl_stream_for_wrapper, switch_lang, Str}; +use erg_common::{ + fmt_iter, fmt_option_map, fmt_vec, impl_display_and_error, impl_stream_for_wrapper, + switch_lang, Str, +}; use erg_parser::error::{ParserRunnerError, ParserRunnerErrors}; @@ -100,6 +103,8 @@ pub struct CompileError { pub caused_by: AtomicStr, } +impl_display_and_error!(CompileError); + impl From for CompileError { fn from(err: ParserRunnerError) -> Self { Self { diff --git a/compiler/erg_parser/error.rs b/compiler/erg_parser/error.rs index 07e7cbf9..e0696e5a 100644 --- a/compiler/erg_parser/error.rs +++ b/compiler/erg_parser/error.rs @@ -6,7 +6,7 @@ use erg_common::color::{RED, RESET}; use erg_common::config::Input; use erg_common::error::{ErrorCore, ErrorDisplay, ErrorKind::*, Location, MultiErrorDisplay}; use erg_common::traits::Stream; -use erg_common::{impl_stream_for_wrapper, switch_lang}; +use erg_common::{impl_display_and_error, impl_stream_for_wrapper, switch_lang}; #[derive(Debug)] pub struct LexError(ErrorCore); @@ -144,6 +144,8 @@ pub struct ParserRunnerError { pub input: Input, } +impl_display_and_error!(ParserRunnerError); + impl ErrorDisplay for ParserRunnerError { fn core(&self) -> &ErrorCore { &self.core