diff --git a/.travis.yml b/.travis.yml index 88f072f..fa3f004 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ language: rust matrix: include: - - rust: 1.31.0 + - rust: 1.56.1 - rust: stable - rust: beta - rust: nightly diff --git a/Cargo.lock b/Cargo.lock index eda7252..30d399a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "addr2line" version = "0.14.1" @@ -135,7 +137,7 @@ version = "0.5.9" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.10", ] [[package]] @@ -183,7 +185,7 @@ checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.60", "synstructure", ] @@ -292,18 +294,18 @@ checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" [[package]] name = "proc-macro2" -version = "1.0.24" +version = "1.0.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +checksum = "ba466839c78239c09faf015484e5cc04860f88242cff4d03eb038f04b4699b73" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] name = "quote" -version = "1.0.8" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" dependencies = [ "proc-macro2", ] @@ -405,7 +407,7 @@ checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.60", ] [[package]] @@ -441,6 +443,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "syn" +version = "2.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aad1363ed6d37b84299588d62d3a7d95b5a5c2d9aad5c85609fda12afaa1f40" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "synstructure" version = "0.12.4" @@ -449,7 +462,7 @@ checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.60", "unicode-xid", ] @@ -491,6 +504,12 @@ dependencies = [ "once_cell", ] +[[package]] +name = "unicode-ident" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" + [[package]] name = "unicode-width" version = "0.1.8" diff --git a/Cargo.toml b/Cargo.toml index f83a5a1..09dadbc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ proc-macro = true [dependencies] proc-macro2 = "1" quote = "1" -syn = "1" +syn = "2" [features] default = ["std"] diff --git a/src/lib.rs b/src/lib.rs index 3eb1a09..9187b69 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ //! //! ```rust //! use derive_new::new; -//! +//! //! fn main() {} //! ``` //! @@ -31,7 +31,7 @@ //! //! ```rust //! use derive_new::new; -//! +//! //! #[derive(new)] //! struct Bar { //! a: i32, @@ -49,7 +49,7 @@ //! //! ```rust //! use derive_new::new; -//! +//! //! #[derive(new)] //! struct Foo { //! x: bool, @@ -69,7 +69,7 @@ //! //! ```rust //! use derive_new::new; -//! +//! //! use std::marker::PhantomData; //! //! #[derive(new)] @@ -91,7 +91,7 @@ //! //! ```rust //! use derive_new::new; -//! +//! //! #[derive(new)] //! enum Enum { //! FirstVariant, @@ -124,7 +124,7 @@ fn path_to_string(path: &syn::Path) -> String { use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; -use syn::Token; +use syn::{Token, punctuated::Punctuated}; #[proc_macro_derive(new, attributes(new))] pub fn derive(input: TokenStream) -> TokenStream { @@ -164,7 +164,7 @@ fn new_for_enum(ast: &syn::DeriveInput, data: &syn::DataEnum) -> proc_macro2::To fn new_impl( ast: &syn::DeriveInput, - fields: Option<&syn::punctuated::Punctuated>, + fields: Option<&Punctuated>, named: bool, variant: Option<&syn::Ident>, ) -> proc_macro2::TokenStream { @@ -227,9 +227,11 @@ fn collect_parent_lint_attrs(attrs: &[syn::Attribute]) -> Vec { fn is_cfg_attr_lint(item: &syn::Meta) -> bool { if let syn::Meta::List(ref l) = *item { - if l.path.is_ident("cfg_attr") && l.nested.len() == 2 { - if let syn::NestedMeta::Meta(ref item) = l.nested[1] { - return is_lint(item); + if l.path.is_ident("cfg_attr") { + if let Ok(nested) = + l.parse_args_with(Punctuated::::parse_terminated) + { + return nested.len() == 2 && is_lint(&nested[1]); } } } @@ -238,9 +240,7 @@ fn collect_parent_lint_attrs(attrs: &[syn::Attribute]) -> Vec { attrs .iter() - .filter_map(|a| a.parse_meta().ok().map(|m| (m, a))) - .filter(|&(ref m, _)| is_lint(m) || is_cfg_attr_lint(m)) - .map(|p| p.1) + .filter(|a| is_lint(&a.meta) || is_cfg_attr_lint(&a.meta)) .cloned() .collect() } @@ -259,30 +259,23 @@ impl FieldAttr { } pub fn parse(attrs: &[syn::Attribute]) -> Option { - use syn::{AttrStyle, Meta, NestedMeta}; - let mut result = None; for attr in attrs.iter() { match attr.style { - AttrStyle::Outer => {} + syn::AttrStyle::Outer => {} _ => continue, } let last_attr_path = attr - .path + .path() .segments - .iter() .last() .expect("Expected at least one segment where #[segment[::segment*](..)]"); if (*last_attr_path).ident != "new" { continue; } - let meta = match attr.parse_meta() { - Ok(meta) => meta, - Err(_) => continue, - }; - let list = match meta { - Meta::List(l) => l, - _ if meta.path().is_ident("new") => { + let list = match attr.meta { + syn::Meta::List(ref l) => l, + _ if attr.path().is_ident("new") => { panic!("Invalid #[new] attribute, expected #[new(..)]") } _ => continue, @@ -290,17 +283,20 @@ impl FieldAttr { if result.is_some() { panic!("Expected at most one #[new] attribute"); } - for item in list.nested.iter() { - match *item { - NestedMeta::Meta(Meta::Path(ref path)) => { + for item in list + .parse_args_with(Punctuated::::parse_terminated) + .unwrap_or_else(|err| panic!("Invalid #[new] attribute: {}", err)) + { + match item { + syn::Meta::Path(path) => { if path.is_ident("default") { result = Some(FieldAttr::Default); } else { panic!("Invalid #[new] attribute: #[new({})]", path_to_string(&path)); } } - NestedMeta::Meta(Meta::NameValue(ref kv)) => { - if let syn::Lit::Str(ref s) = kv.lit { + syn::Meta::NameValue(kv) => { + if let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(ref s), .. }) = kv.value { if kv.path.is_ident("value") { let tokens = lit_str_to_token_stream(s).ok().expect(&format!( "Invalid expression in #[new]: `{}`", @@ -314,12 +310,9 @@ impl FieldAttr { panic!("Non-string literal value in #[new] attribute"); } } - NestedMeta::Meta(Meta::List(ref l)) => { + syn::Meta::List(l) => { panic!("Invalid #[new] attribute: #[new({}(..))]", path_to_string(&l.path)); } - NestedMeta::Lit(_) => { - panic!("Invalid #[new] attribute: literal value in #[new(..)]"); - } } } }