Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to syn 2.0 #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0.3", features = ["visit", "extra-traits"] }
syn = { version = "2.0", features = ["visit", "extra-traits"] }

[dev-dependencies]
# 1.0.23 requires rustc 1.36, while our msrv is 1.34
runtime-macros-derive = "0.4.0"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented this and the relevant test out for now, as Cargo tries to build it with syn 2.0 which it's not compatbile with

# runtime-macros-derive = "0.4.0"
trybuild = "1.0.18, <1.0.23"
walkdir = "2"

Expand Down
102 changes: 49 additions & 53 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ macro_rules! for_all_attr {
if let Some(meta_items) = meta_items {
for meta_item in meta_items.iter() {
let meta_item = read_items(meta_item, $errors);
let MetaItem($name, $value) = try!(meta_item);
let MetaItem($name, $value) = meta_item?;
match $name.to_string().as_ref() {
$($body)*
}
Expand All @@ -226,7 +226,8 @@ macro_rules! match_attributes {
};

($errors:ident for $trait:expr; for $value:ident in $values:expr; $($body:tt)* ) => {
for (name, $value) in $values {
for (name, $value) in $values.iter() {
let $value = $value.as_ref();
match name {
Some(ident) => {
match ident.to_string().as_ref() {
Expand Down Expand Up @@ -658,22 +659,15 @@ impl Field {
/// * `#[derivative(Debug(foo="bar")]` is represented as `(Debug, [(Some(foo), Some("bar"))])`.
struct MetaItem<'a>(
&'a syn::Ident,
Vec<(Option<&'a syn::Ident>, Option<&'a syn::LitStr>)>,
Vec<(Option<syn::Ident>, Option<syn::LitStr>)>,
);

/// Parse an arbitrary item for our limited `MetaItem` subset.
fn read_items<'a>(item: &'a syn::NestedMeta, errors: &mut proc_macro2::TokenStream) -> Result<MetaItem<'a>, ()> {
let item = match *item {
syn::NestedMeta::Meta(ref item) => item,
syn::NestedMeta::Lit(ref lit) => {
errors.extend(quote_spanned! {lit.span()=>
compile_error!("expected meta-item but found literal");
});

return Err(());
}
};
match *item {
fn read_items<'a>(
item: &'a syn::Meta,
errors: &mut proc_macro2::TokenStream,
) -> Result<MetaItem<'a>, ()> {
match item {
syn::Meta::Path(ref path) => match path.get_ident() {
Some(name) => Ok(MetaItem(name, Vec::new())),
None => {
Expand All @@ -684,23 +678,27 @@ fn read_items<'a>(item: &'a syn::NestedMeta, errors: &mut proc_macro2::TokenStre
Err(())
}
},
syn::Meta::List(syn::MetaList {
ref path,
nested: ref values,
..
}) => {
syn::Meta::List(list) => {
let Ok(values) = list.parse_args_with(
syn::punctuated::Punctuated::<syn::Meta, syn::token::Comma>::parse_terminated,
) else {
errors.extend(quote_spanned! {list.span()=>
compile_error!("expected meta list");
});
return Err(());
};
let values = values
.iter()
.map(|value| {
if let syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue {
if let syn::Meta::NameValue(syn::MetaNameValue {
ref path,
lit: ref value,
ref value,
..
})) = *value
}) = *value
{
let (name, value) = ensure_str_lit(&path, &value, errors)?;

Ok((Some(name), Some(value)))
Ok((Some(name.clone()), Some(value.clone())))
} else {
errors.extend(quote_spanned! {value.span()=>
compile_error!("expected named value");
Expand All @@ -711,27 +709,24 @@ fn read_items<'a>(item: &'a syn::NestedMeta, errors: &mut proc_macro2::TokenStre
})
.collect::<Result<_, _>>()?;

let name = match path.get_ident() {
let name = match list.path.get_ident() {
Some(name) => name,
None => {
errors.extend(quote_spanned! {path.span()=>
errors.extend(quote_spanned! {list.path.span()=>
compile_error!("expected derivative attribute to be a string, but found a path");
});

return Err(());
}
};

Ok(MetaItem(name, values))
}
syn::Meta::NameValue(syn::MetaNameValue {
ref path,
lit: ref value,
..
ref path, value, ..
}) => {
let (name, value) = ensure_str_lit(&path, &value, errors)?;

Ok(MetaItem(name, vec![(None, Some(value))]))
Ok(MetaItem(name, vec![(None, Some(value.clone()))]))
}
}
}
Expand All @@ -740,13 +735,15 @@ fn read_items<'a>(item: &'a syn::NestedMeta, errors: &mut proc_macro2::TokenStre
fn derivative_attribute(
attribute: &syn::Attribute,
errors: &mut proc_macro2::TokenStream,
) -> Option<syn::punctuated::Punctuated<syn::NestedMeta, syn::token::Comma>> {
if !attribute.path.is_ident("derivative") {
return None;
}
match attribute.parse_meta() {
Ok(syn::Meta::List(meta_list)) => Some(meta_list.nested),
Ok(_) => None,
) -> Option<syn::punctuated::Punctuated<syn::Meta, syn::token::Comma>> {
let list = match &attribute.meta {
syn::Meta::List(list) if list.path.is_ident("derivative") => list,
_ => return None,
};
match list.parse_args_with(
syn::punctuated::Punctuated::<syn::Meta, syn::token::Comma>::parse_terminated,
) {
Ok(x) => Some(x),
Err(e) => {
let message = format!("invalid attribute: {}", e);
errors.extend(quote_spanned! {e.span()=>
Expand Down Expand Up @@ -841,7 +838,7 @@ where

fn ensure_str_lit<'a>(
attr_path: &'a syn::Path,
lit: &'a syn::Lit,
lit: &'a syn::Expr,
errors: &mut proc_macro2::TokenStream,
) -> Result<(&'a syn::Ident, &'a syn::LitStr), ()> {
let attr_name = match attr_path.get_ident() {
Expand All @@ -854,7 +851,11 @@ fn ensure_str_lit<'a>(
}
};

if let syn::Lit::Str(ref lit) = *lit {
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(ref lit),
..
}) = *lit
{
Ok((attr_name, lit))
} else {
let message = format!(
Expand All @@ -869,19 +870,14 @@ fn ensure_str_lit<'a>(
}

pub fn has_repr_packed_attr(attr: &syn::Attribute) -> bool {
if let Ok(attr) = attr.parse_meta() {
if attr.path().get_ident().map(|i| i == "repr") == Some(true) {
if let syn::Meta::List(items) = attr {
for item in items.nested {
if let syn::NestedMeta::Meta(item) = item {
if item.path().get_ident().map(|i| i == "packed") == Some(true) {
return true;
}
}
}
let mut result = false;
if attr.path().is_ident("repr") {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("packed") {
result = true;
}
}
Ok(())
}).ok();
}

false
result
}
4 changes: 2 additions & 2 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn format_with(

generics
.params
.push(syn::GenericParam::Lifetime(syn::LifetimeDef::new(
.push(syn::GenericParam::Lifetime(syn::LifetimeParam::new(
parse_quote!('_derivative),
)));
let where_predicates = generics
Expand Down Expand Up @@ -201,7 +201,7 @@ fn format_with(
*ctor_generics
.lifetimes_mut()
.last()
.expect("There must be a '_derivative lifetime") = syn::LifetimeDef::new(parse_quote!('_));
.expect("There must be a '_derivative lifetime") = syn::LifetimeParam::new(parse_quote!('_));
let (_, ctor_ty_generics, _) = ctor_generics.split_for_impl();
let ctor_ty_generics = ctor_ty_generics.as_turbofish();

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn derivative_internal(input: syn::DeriveInput) -> proc_macro2::TokenStream {
output
}

/*
#[test]
fn macro_code_coverage() {
for entry in walkdir::WalkDir::new("tests")
Expand All @@ -104,3 +105,4 @@ fn macro_code_coverage() {
.unwrap();
}
}
*/
4 changes: 2 additions & 2 deletions tests/derive-debug-generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ fn main() {
assert_eq!(F(NoDebug).to_show(), "F".to_string());
assert_eq!(G(42, NoDebug).to_show(), "G(42)".to_string());
assert_eq!(J(NoDebug).to_show(), "J".to_string());
assert_eq!(&format!("{:?}", PhantomField::<NoDebug> { foo: Default::default() }), "PhantomField { foo: PhantomData }");
assert_eq!(&format!("{:?}", PhantomTuple::<NoDebug> { foo: Default::default() }), "PhantomTuple { foo: PhantomData }");
assert_eq!(&format!("{:?}", PhantomField::<NoDebug> { foo: Default::default() }), "PhantomField { foo: PhantomData<derive_debug_generics::NoDebug> }");
assert_eq!(&format!("{:?}", PhantomTuple::<NoDebug> { foo: Default::default() }), "PhantomTuple { foo: PhantomData<(derive_debug_generics::NoDebug,)> }");
}