ink_ir/
error.rs

1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Provide macros to simplify error reporting in procedural macros.
16
17pub trait ExtError {
18    /// Returns `self` combined with the other error.
19    fn into_combine(self, another: syn::Error) -> Self;
20}
21
22impl ExtError for syn::Error {
23    fn into_combine(mut self, another: syn::Error) -> Self {
24        self.combine(another);
25        self
26    }
27}
28
29/// Creates a [`syn::Error`] with the format message and infers the
30/// [`Span`](`proc_macro2::Span`) using [`ToTokens`](`quote::ToTokens`).
31///
32/// # Parameters
33///
34/// - The first argument must implement [`quote::ToTokens`] in order to infer a
35///   [`Span`](`proc_macro2::Span`).
36/// - The second argument is a format string.
37/// - The rest are format string arguments.
38///
39/// # Note
40///
41/// On stable Rust this might yield higher quality error span information to the user
42/// than [`format_err`](`crate::format_err`).
43/// - Source: [`syn::Error::new_spanned`](https://docs.rs/syn/1.0.33/syn/struct.Error.html#method.new_spanned)
44/// - Tracking issue: [`#54725`](https://github.com/rust-lang/rust/issues/54725)
45#[macro_export]
46macro_rules! format_err_spanned {
47    ($tokens:expr, $($msg:tt)*) => {
48        ::syn::Error::new_spanned(
49            &$tokens,
50            format_args!($($msg)*)
51        )
52    }
53}
54
55/// Creates a [`syn::Error`] with the format message and infers the
56/// [`Span`](`proc_macro2::Span`) using [`Spanned`](`syn::spanned::Spanned`).
57///
58/// # Parameters
59///
60/// - The first argument must be a type that implements [`syn::spanned::Spanned`].
61/// - The second argument is a format string.
62/// - The rest are format string arguments.
63///
64/// # Note
65///
66/// On stable Rust this might yield worse error span information to the user
67/// than [`format_err_spanned`].
68/// - Source: [`syn::Error::new_spanned`](https://docs.rs/syn/1.0.33/syn/struct.Error.html#method.new_spanned)
69/// - Tracking issue: [`#54725`](https://github.com/rust-lang/rust/issues/54725)
70#[macro_export]
71macro_rules! format_err {
72    ($spanned:expr, $($msg:tt)*) => {
73        ::syn::Error::new(
74            <_ as ::syn::spanned::Spanned>::span(&$spanned),
75            format_args!($($msg)*)
76        )
77    }
78}