Struct Error
struct Error { ... }
Error returned when a Syn parser cannot parse the input tokens.
Error reporting in proc macros
The correct way to report errors back to the compiler from a procedural
macro is by emitting an appropriately spanned invocation of
compile_error! in the generated code. This produces a better diagnostic
message than simply panicking the macro.
When parsing macro input, the parse_macro_input! macro handles the
conversion to compile_error! automatically.
# extern crate proc_macro;
#
use TokenStream;
use ;
use ;
# const IGNORE: &str = stringify! ;
; 0 }]
}
For errors that arise later than the initial parsing stage, the
.to_compile_error() or .into_compile_error() methods can be used to
perform an explicit conversion to compile_error!.
# extern crate proc_macro;
#
# use TokenStream;
# use ;
#
# const IGNORE: &str = stringify! ;
#
#
Implementations
impl Error
fn new<T: Display>(span: Span, message: T) -> SelfUsually the
ParseStream::errormethod will be used instead, which automatically uses the correct span from the current position of the parse stream.Use
Error::newwhen the error needs to be triggered on some span other than where the parse stream is currently positioned.Example
use ; use ParseStream; // Parses input that looks like `name = "string"` where the key must be // the identifier `name` and the value may be any string literal. // Returns the string literal.fn new_spanned<T: ToTokens, U: Display>(tokens: T, message: U) -> SelfCreates an error with the specified message spanning the given syntax tree node.
Unlike the
Error::newconstructor, this constructor takes an argumenttokenswhich is a syntax tree node. This allows the resultingErrorto attempt to span all tokens inside oftokens. While you would typically be able to use theSpannedtrait with the aboveError::newconstructor, implementation limitations today mean thatError::new_spannedmay provide a higher-quality error message on stable Rust.When in doubt it's recommended to stick to
Error::new(orParseStream::error)!fn span(self: &Self) -> SpanThe source location of the error.
Spans are not thread-safe so this function returns
Span::call_site()if called from a different thread than the one on which theErrorwas originally created.fn to_compile_error(self: &Self) -> TokenStreamRender the error as an invocation of
compile_error!.The
parse_macro_input!macro provides a convenient way to invoke this method correctly in a procedural macro.fn into_compile_error(self: Self) -> TokenStreamRender the error as an invocation of
compile_error!.Example
# extern crate proc_macro; # use TokenStream; use ; # const _: &str = stringify! ;fn combine(self: &mut Self, another: Error)Add another error message to self such that when
to_compile_error()is called, both errors will be emitted together.
impl Clone for Error
fn clone(self: &Self) -> Self
impl Debug for Error
fn fmt(self: &Self, formatter: &mut Formatter<'_>) -> Result
impl Display for Error
fn fmt(self: &Self, formatter: &mut Formatter<'_>) -> Result
impl Error for Error
impl Extend for Error
fn extend<T: IntoIterator<Item = Error>>(self: &mut Self, iter: T)
impl Freeze for Error
impl From for Error
fn from(err: LexError) -> Self
impl IntoIterator for Error
fn into_iter(self: Self) -> <Self as >::IntoIter
impl RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl UnsafeUnpin for Error
impl UnwindSafe for Error
impl<T> Any for Error
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Error
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Error
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Error
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Error
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Error
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T> ToString for Error
fn to_string(self: &Self) -> String
impl<T, U> Into for Error
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Error
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Error
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>