Robustify derive macro attribute parsing (#544)

Missing whitespace in the implementation of `TokenStream::to_string` on
beta and nightly was breaking our parsing of derive macro attributes.

This change makes the parser ignore whitespace, and should make the beta
and nightly builds succeed again.
This commit is contained in:
Michael Sproul 2019-09-28 14:29:14 +10:00 committed by GitHub
parent 2399b9bbe0
commit 3d559d8b41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 28 deletions

View File

@ -6,12 +6,10 @@ use quote::quote;
use syn::{parse_macro_input, DeriveInput}; use syn::{parse_macro_input, DeriveInput};
fn is_slice(field: &syn::Field) -> bool { fn is_slice(field: &syn::Field) -> bool {
for attr in &field.attrs { field.attrs.iter().any(|attr| {
if attr.tts.to_string() == "( as_slice )" { attr.path.is_ident("compare_fields")
return true; && attr.tts.to_string().replace(" ", "") == "(as_slice)"
} })
}
false
} }
#[proc_macro_derive(CompareFields, attributes(compare_fields))] #[proc_macro_derive(CompareFields, attributes(compare_fields))]

View File

@ -53,12 +53,9 @@ fn get_serializable_field_types<'a>(struct_data: &'a syn::DataStruct) -> Vec<&'a
/// ///
/// The field attribute is: `#[ssz(skip_serializing)]` /// The field attribute is: `#[ssz(skip_serializing)]`
fn should_skip_serializing(field: &syn::Field) -> bool { fn should_skip_serializing(field: &syn::Field) -> bool {
for attr in &field.attrs { field.attrs.iter().any(|attr| {
if attr.tts.to_string() == "( skip_serializing )" { attr.path.is_ident("ssz") && attr.tts.to_string().replace(" ", "") == "(skip_serializing)"
return true; })
}
}
false
} }
/// Implements `ssz::Encode` for some `struct`. /// Implements `ssz::Encode` for some `struct`.
@ -149,12 +146,9 @@ pub fn ssz_encode_derive(input: TokenStream) -> TokenStream {
/// ///
/// The field attribute is: `#[ssz(skip_deserializing)]` /// The field attribute is: `#[ssz(skip_deserializing)]`
fn should_skip_deserializing(field: &syn::Field) -> bool { fn should_skip_deserializing(field: &syn::Field) -> bool {
for attr in &field.attrs { field.attrs.iter().any(|attr| {
if attr.tts.to_string() == "( skip_deserializing )" { attr.path.is_ident("ssz") && attr.tts.to_string().replace(" ", "") == "(skip_deserializing)"
return true; })
}
}
false
} }
/// Implements `ssz::Decode` for some `struct`. /// Implements `ssz::Decode` for some `struct`.

View File

@ -9,12 +9,9 @@ use syn::{parse_macro_input, DeriveInput};
/// ///
/// The field attribute is: `#[test_random(default)]` /// The field attribute is: `#[test_random(default)]`
fn should_use_default(field: &syn::Field) -> bool { fn should_use_default(field: &syn::Field) -> bool {
for attr in &field.attrs { field.attrs.iter().any(|attr| {
if attr.tts.to_string() == "( default )" { attr.path.is_ident("test_random") && attr.tts.to_string().replace(" ", "") == "(default)"
return true; })
}
}
false
} }
#[proc_macro_derive(TestRandom, attributes(test_random))] #[proc_macro_derive(TestRandom, attributes(test_random))]

View File

@ -31,10 +31,9 @@ fn get_hashable_named_field_idents<'a>(struct_data: &'a syn::DataStruct) -> Vec<
/// ///
/// The field attribute is: `#[tree_hash(skip_hashing)]` /// The field attribute is: `#[tree_hash(skip_hashing)]`
fn should_skip_hashing(field: &syn::Field) -> bool { fn should_skip_hashing(field: &syn::Field) -> bool {
field field.attrs.iter().any(|attr| {
.attrs attr.path.is_ident("tree_hash") && attr.tts.to_string().replace(" ", "") == "(skip_hashing)"
.iter() })
.any(|attr| attr.into_token_stream().to_string() == "# [ tree_hash ( skip_hashing ) ]")
} }
/// Implements `tree_hash::TreeHash` for some `struct`. /// Implements `tree_hash::TreeHash` for some `struct`.