Use error reporting provided by Meta

This commit is contained in:
David Tolnay 2023-03-18 14:28:18 -07:00
parent 07c66e7259
commit 39aaeb00ff
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 3 additions and 15 deletions

View File

@ -14,7 +14,7 @@ proc-macro = true
[dependencies] [dependencies]
proc-macro2 = "1.0" proc-macro2 = "1.0"
quote = "1.0" quote = "1.0"
syn = "2.0" syn = "2.0.1"
[package.metadata.docs.rs] [package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"] targets = ["x86_64-unknown-linux-gnu"]

View File

@ -57,13 +57,13 @@ pub fn get(input: &[Attribute]) -> Result<Attrs> {
if attr.path().is_ident("error") { if attr.path().is_ident("error") {
parse_error_attribute(&mut attrs, attr)?; parse_error_attribute(&mut attrs, attr)?;
} else if attr.path().is_ident("source") { } else if attr.path().is_ident("source") {
require_empty_attribute(attr)?; attr.meta.require_path_only()?;
if attrs.source.is_some() { if attrs.source.is_some() {
return Err(Error::new_spanned(attr, "duplicate #[source] attribute")); return Err(Error::new_spanned(attr, "duplicate #[source] attribute"));
} }
attrs.source = Some(attr); attrs.source = Some(attr);
} else if attr.path().is_ident("backtrace") { } else if attr.path().is_ident("backtrace") {
require_empty_attribute(attr)?; attr.meta.require_path_only()?;
if attrs.backtrace.is_some() { if attrs.backtrace.is_some() {
return Err(Error::new_spanned(attr, "duplicate #[backtrace] attribute")); return Err(Error::new_spanned(attr, "duplicate #[backtrace] attribute"));
} }
@ -193,18 +193,6 @@ fn parse_token_expr(input: ParseStream, mut begin_expr: bool) -> Result<TokenStr
Ok(TokenStream::from_iter(tokens)) Ok(TokenStream::from_iter(tokens))
} }
fn require_empty_attribute(attr: &Attribute) -> Result<()> {
let error_span = match &attr.meta {
Meta::Path(_) => return Ok(()),
Meta::List(meta) => meta.delimiter.span().open(),
Meta::NameValue(meta) => meta.eq_token.span,
};
Err(Error::new(
error_span,
"unexpected token in thiserror attribute",
))
}
impl ToTokens for Display<'_> { impl ToTokens for Display<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) { fn to_tokens(&self, tokens: &mut TokenStream) {
let fmt = &self.fmt; let fmt = &self.fmt;