diff options
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | Cargo.toml.orig | 5 | ||||
-rw-r--r-- | METADATA | 8 | ||||
-rw-r--r-- | src/ast.rs | 44 | ||||
-rw-r--r-- | src/attr.rs | 17 | ||||
-rw-r--r-- | src/expand.rs | 9 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/valid.rs | 4 |
9 files changed, 62 insertions, 33 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 30d59f6..16e6ee7 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "94e62a81bc6da67efb06a45a8f45caf49768d5e4" + "sha1": "42b537acf08de385dcf6138f24e3274ff8a18148" } } @@ -13,7 +13,7 @@ [package] edition = "2018" name = "thiserror-impl" -version = "1.0.16" +version = "1.0.20" authors = ["David Tolnay <dtolnay@gmail.com>"] description = "Implementation detail of the `thiserror` crate" license = "MIT OR Apache-2.0" @@ -31,5 +31,3 @@ version = "1.0" [dependencies.syn] version = "1.0.11" -[badges.travis-ci] -repository = "dtolnay/thiserror" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 46363da..ebcfe35 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "thiserror-impl" -version = "1.0.16" +version = "1.0.20" authors = ["David Tolnay <dtolnay@gmail.com>"] edition = "2018" license = "MIT OR Apache-2.0" @@ -10,9 +10,6 @@ repository = "https://github.com/dtolnay/thiserror" [lib] proc-macro = true -[badges] -travis-ci = { repository = "dtolnay/thiserror" } - [dependencies] proc-macro2 = "1.0" quote = "1.0" @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/thiserror-impl/thiserror-impl-1.0.16.crate" + value: "https://static.crates.io/crates/thiserror-impl/thiserror-impl-1.0.20.crate" } - version: "1.0.16" + version: "1.0.20" license_type: NOTICE last_upgrade_date { year: 2020 - month: 5 - day: 6 + month: 8 + day: 27 } } @@ -1,4 +1,5 @@ use crate::attr::{self, Attrs}; +use proc_macro2::Span; use syn::{ Data, DataEnum, DataStruct, DeriveInput, Error, Fields, Generics, Ident, Index, Member, Result, Type, @@ -55,7 +56,8 @@ impl<'a> Input<'a> { impl<'a> Struct<'a> { fn from_syn(node: &'a DeriveInput, data: &'a DataStruct) -> Result<Self> { let mut attrs = attr::get(&node.attrs)?; - let fields = Field::multiple_from_syn(&data.fields)?; + let span = attrs.span().unwrap_or_else(Span::call_site); + let fields = Field::multiple_from_syn(&data.fields, span)?; if let Some(display) = &mut attrs.display { display.expand_shorthand(&fields); } @@ -72,11 +74,12 @@ impl<'a> Struct<'a> { impl<'a> Enum<'a> { fn from_syn(node: &'a DeriveInput, data: &'a DataEnum) -> Result<Self> { let attrs = attr::get(&node.attrs)?; + let span = attrs.span().unwrap_or_else(Span::call_site); let variants = data .variants .iter() .map(|node| { - let mut variant = Variant::from_syn(node)?; + let mut variant = Variant::from_syn(node, span)?; if let display @ None = &mut variant.attrs.display { *display = attrs.display.clone(); } @@ -99,35 +102,50 @@ impl<'a> Enum<'a> { } impl<'a> Variant<'a> { - fn from_syn(node: &'a syn::Variant) -> Result<Self> { + fn from_syn(node: &'a syn::Variant, span: Span) -> Result<Self> { + let attrs = attr::get(&node.attrs)?; + let span = attrs.span().unwrap_or(span); Ok(Variant { original: node, - attrs: attr::get(&node.attrs)?, + attrs, ident: node.ident.clone(), - fields: Field::multiple_from_syn(&node.fields)?, + fields: Field::multiple_from_syn(&node.fields, span)?, }) } } impl<'a> Field<'a> { - fn multiple_from_syn(fields: &'a Fields) -> Result<Vec<Self>> { + fn multiple_from_syn(fields: &'a Fields, span: Span) -> Result<Vec<Self>> { fields .iter() .enumerate() - .map(|(i, field)| Field::from_syn(i, field)) + .map(|(i, field)| Field::from_syn(i, field, span)) .collect() } - fn from_syn(i: usize, node: &'a syn::Field) -> Result<Self> { + fn from_syn(i: usize, node: &'a syn::Field, span: Span) -> Result<Self> { Ok(Field { original: node, attrs: attr::get(&node.attrs)?, - member: node - .ident - .clone() - .map(Member::Named) - .unwrap_or_else(|| Member::Unnamed(Index::from(i))), + member: node.ident.clone().map(Member::Named).unwrap_or_else(|| { + Member::Unnamed(Index { + index: i as u32, + span, + }) + }), ty: &node.ty, }) } } + +impl Attrs<'_> { + pub fn span(&self) -> Option<Span> { + if let Some(display) = &self.display { + Some(display.fmt.span()) + } else if let Some(transparent) = &self.transparent { + Some(transparent.span) + } else { + None + } + } +} diff --git a/src/attr.rs b/src/attr.rs index 30ecfc8..1ab1e28 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -1,4 +1,4 @@ -use proc_macro2::{Delimiter, Group, TokenStream, TokenTree}; +use proc_macro2::{Delimiter, Group, Span, TokenStream, TokenTree}; use quote::{format_ident, quote, ToTokens}; use std::iter::FromIterator; use syn::parse::{Nothing, ParseStream}; @@ -12,7 +12,7 @@ pub struct Attrs<'a> { pub source: Option<&'a Attribute>, pub backtrace: Option<&'a Attribute>, pub from: Option<&'a Attribute>, - pub transparent: Option<&'a Attribute>, + pub transparent: Option<Transparent<'a>>, } #[derive(Clone)] @@ -23,6 +23,12 @@ pub struct Display<'a> { pub has_bonus_display: bool, } +#[derive(Copy, Clone)] +pub struct Transparent<'a> { + pub original: &'a Attribute, + pub span: Span, +} + pub fn get(input: &[Attribute]) -> Result<Attrs> { let mut attrs = Attrs { display: None, @@ -66,14 +72,17 @@ fn parse_error_attribute<'a>(attrs: &mut Attrs<'a>, attr: &'a Attribute) -> Resu syn::custom_keyword!(transparent); attr.parse_args_with(|input: ParseStream| { - if input.parse::<Option<transparent>>()?.is_some() { + if let Some(kw) = input.parse::<Option<transparent>>()? { if attrs.transparent.is_some() { return Err(Error::new_spanned( attr, "duplicate #[error(transparent)] attribute", )); } - attrs.transparent = Some(attr); + attrs.transparent = Some(Transparent { + original: attr, + span: kw.span, + }); return Ok(()); } diff --git a/src/expand.rs b/src/expand.rs index 3da8f41..9235e8f 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -113,7 +113,9 @@ fn impl_struct(input: Struct) -> TokenStream { }; let display_impl = display_body.map(|body| { quote! { + #[allow(unused_qualifications)] impl #impl_generics std::fmt::Display for #ty #ty_generics #where_clause { + #[allow(clippy::used_underscore_binding)] fn fmt(&self, __formatter: &mut std::fmt::Formatter) -> std::fmt::Result { #body } @@ -126,6 +128,7 @@ fn impl_struct(input: Struct) -> TokenStream { let from = from_field.ty; let body = from_initializer(from_field, backtrace_field); quote! { + #[allow(unused_qualifications)] impl #impl_generics std::convert::From<#from> for #ty #ty_generics #where_clause { #[allow(deprecated)] fn from(source: #from) -> Self { @@ -138,6 +141,7 @@ fn impl_struct(input: Struct) -> TokenStream { let error_trait = spanned_error_trait(input.original); quote! { + #[allow(unused_qualifications)] impl #impl_generics #error_trait for #ty #ty_generics #where_clause { #source_method #backtrace_method @@ -293,10 +297,11 @@ fn impl_enum(input: Enum) -> TokenStream { } }); Some(quote! { + #[allow(unused_qualifications)] impl #impl_generics std::fmt::Display for #ty #ty_generics #where_clause { fn fmt(&self, __formatter: &mut std::fmt::Formatter) -> std::fmt::Result { #use_as_display - #[allow(unused_variables, deprecated)] + #[allow(unused_variables, deprecated, clippy::used_underscore_binding)] match #void_deref self { #(#arms,)* } @@ -314,6 +319,7 @@ fn impl_enum(input: Enum) -> TokenStream { let from = from_field.ty; let body = from_initializer(from_field, backtrace_field); Some(quote! { + #[allow(unused_qualifications)] impl #impl_generics std::convert::From<#from> for #ty #ty_generics #where_clause { #[allow(deprecated)] fn from(source: #from) -> Self { @@ -326,6 +332,7 @@ fn impl_enum(input: Enum) -> TokenStream { let error_trait = spanned_error_trait(input.original); quote! { + #[allow(unused_qualifications)] impl #impl_generics #error_trait for #ty #ty_generics #where_clause { #source_method #backtrace_method @@ -1,4 +1,4 @@ -#![allow(clippy::block_in_if_condition_stmt, clippy::range_plus_one)] +#![allow(clippy::blocks_in_if_conditions, clippy::range_plus_one)] extern crate proc_macro; diff --git a/src/valid.rs b/src/valid.rs index 0d5ba36..ab20423 100644 --- a/src/valid.rs +++ b/src/valid.rs @@ -19,7 +19,7 @@ impl Struct<'_> { if let Some(transparent) = self.attrs.transparent { if self.fields.len() != 1 { return Err(Error::new_spanned( - transparent, + transparent.original, "#[error(transparent)] requires exactly one field", )); } @@ -165,7 +165,7 @@ fn check_field_attrs(fields: &[Field]) -> Result<()> { } if let Some(transparent) = field.attrs.transparent { return Err(Error::new_spanned( - transparent, + transparent.original, "#[error(transparent)] needs to go outside the enum or struct, not on an individual field", )); } |