eosio_codegen/
error.rs

1pub trait ExtError {
2    /// Returns `self` combined with the other error.
3    fn into_combine(self, another: syn::Error) -> Self;
4}
5
6impl ExtError for syn::Error {
7    fn into_combine(mut self, another: syn::Error) -> Self {
8        self.combine(another);
9        self
10    }
11}
12
13/// Creates a [`syn::Error`] with the format message and infers the
14/// [`Span`](`proc_macro2::Span`) using [`ToTokens`](`quote::ToTokens`).
15///
16/// # Parameters
17///
18/// - The first argument must implement [`quote::ToTokens`] in order to
19///   infer a [`Span`](`proc_macro2::Span`).
20/// - The second argument is a format string.
21/// - The rest are format string arguments.
22///
23/// # Note
24///
25/// On stable Rust this might yield higher quality error span information to the user
26/// than [`format_err`].
27/// - Source:
28/// [`syn::Error::new_spanned`](https://docs.rs/syn/1.0.33/syn/struct.Error.html#method.new_spanned)
29/// - Tracking issue: [`#54725`](https://github.com/rust-lang/rust/issues/54725)
30#[macro_export]
31macro_rules! format_err_spanned {
32    ($tokens:expr, $($msg:tt)*) => {
33        ::syn::Error::new_spanned(
34            &$tokens,
35            format_args!($($msg)*)
36        )
37    }
38}
39
40/// Creates a [`syn::Error`] with the format message and infers the
41/// [`Span`](`proc_macro2::Span`) using [`Spanned`](`syn::spanned::Spanned`).
42///
43/// # Parameters
44///
45/// - The first argument must be a type that implements [`syn::spanned::Spanned`].
46/// - The second argument is a format string.
47/// - The rest are format string arguments.
48///
49/// # Note
50///
51/// On stable Rust this might yield worse error span information to the user
52/// than [`format_err_spanned`].
53/// - Source:
54/// [`syn::Error::new_spanned`](https://docs.rs/syn/1.0.33/syn/struct.Error.html#method.new_spanned)
55/// - Tracking issue: [`#54725`](https://github.com/rust-lang/rust/issues/54725)
56#[macro_export]
57macro_rules! format_err {
58    ($spanned:expr, $($msg:tt)*) => {
59        ::syn::Error::new(
60            <_ as ::syn::spanned::Spanned>::span(&$spanned),
61            format_args!($($msg)*)
62        )
63    }
64}