Skip to content

Commit

Permalink
Update proc macros to syn 2
Browse files Browse the repository at this point in the history
Summary: Release notes: https://github.com/dtolnay/syn/releases/tag/2.0.0

Reviewed By: zertosh

Differential Revision: D44771540

fbshipit-source-id: 194c2c3649a7e71f8be04357b1fea19d0d49aef1
  • Loading branch information
David Tolnay authored and facebook-github-bot committed Apr 7, 2023
1 parent ed12ca5 commit 697ad1f
Show file tree
Hide file tree
Showing 24 changed files with 88 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ smallvec = { version = "1.10", features = ["const_generics", "const_new", "serde
static_assertions = "1.1.0"
strsim = "0.10.0"
structopt = "0.3.0"
syn = { version = "1.0.27", features = ["extra-traits", "full"] }
syn = { version = "2", features = ["extra-traits", "full"] }
sync_wrapper = "0.1.0"
sys-info = "0.9.1"
sysinfo = "0.26.8"
Expand Down
2 changes: 1 addition & 1 deletion allocative/allocative_derive/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ rust_library(
deps = [
"fbsource//third-party/rust:proc-macro2",
"fbsource//third-party/rust:quote",
"fbsource//third-party/rust:syn1",
"fbsource//third-party/rust:syn",
],
)
2 changes: 1 addition & 1 deletion allocative/allocative_derive/src/derive_allocative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ fn extract_attrs(attrs: &[Attribute]) -> syn::Result<AllocativeAttrs> {
let mut opts = AllocativeAttrs::default();

for attr in attrs.iter() {
if !attr.path.is_ident("allocative") {
if !attr.path().is_ident("allocative") {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion app/buck2_build_api_derive/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ rust_library(
"fbsource//third-party/rust:convert_case",
"fbsource//third-party/rust:proc-macro2",
"fbsource//third-party/rust:quote",
"fbsource//third-party/rust:syn1",
"fbsource//third-party/rust:syn",
],
)
14 changes: 9 additions & 5 deletions app/buck2_build_api_derive/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ProviderCodegen {
.attrs
.clone()
.into_iter()
.partition(|a| !a.path.is_ident(PROVIDER_IDENT));
.partition(|a| !a.path().is_ident(PROVIDER_IDENT));
field.attrs = attrs;
if provider_attr.len() > 1 {
return Err(syn::Error::new_spanned(
Expand Down Expand Up @@ -136,11 +136,15 @@ impl ProviderCodegen {
let mut doc_lines = vec![];

for attr in attrs {
if attr.path.is_ident("doc") {
if let Ok(syn::Meta::NameValue(syn::MetaNameValue {
lit: syn::Lit::Str(s),
if attr.path().is_ident("doc") {
if let syn::Meta::NameValue(syn::MetaNameValue {
value:
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(s),
..
}),
..
})) = attr.parse_meta()
}) = &attr.meta
{
doc_lines.push(s.value());
}
Expand Down
2 changes: 1 addition & 1 deletion app/buck2_query_derive/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rust_library(
"fbsource//third-party/rust:itertools",
"fbsource//third-party/rust:proc-macro2",
"fbsource//third-party/rust:quote",
"fbsource//third-party/rust:syn1",
"fbsource//third-party/rust:syn",
"fbsource//third-party/rust:textwrap",
],
)
40 changes: 18 additions & 22 deletions app/buck2_query_derive/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use proc_macro2::TokenStream;
use syn::parse::Result;
use syn::spanned::Spanned;
use syn::Error;
use syn::Expr;
use syn::ExprLit;
use syn::FnArg;
use syn::Generics;
use syn::Ident;
Expand All @@ -22,7 +24,6 @@ use syn::ItemImpl;
use syn::Lit;
use syn::Meta;
use syn::MetaNameValue;
use syn::NestedMeta;
use syn::Pat;
use syn::PatIdent;
use syn::PatType;
Expand All @@ -48,10 +49,14 @@ impl DocString {
fn parse(attrs: &[syn::Attribute]) -> Option<Self> {
let mut docs = Vec::new();
for attr in attrs {
if attr.path.is_ident("doc") {
if let Ok(Meta::NameValue(MetaNameValue {
lit: Lit::Str(s), ..
})) = attr.parse_meta()
if attr.path().is_ident("doc") {
if let Meta::NameValue(MetaNameValue {
value:
Expr::Lit(ExprLit {
lit: Lit::Str(s), ..
}),
..
}) = &attr.meta
{
docs.push(s.value());
}
Expand Down Expand Up @@ -164,7 +169,7 @@ impl syn::parse::Parse for Module {
let binary_op_path = Path::from(Ident::new("binary_op", Span::call_site()));
for item in items {
match item {
ImplItem::Method(method) => {
ImplItem::Fn(method) => {
let sig = &method.sig;
let span = sig.span();

Expand Down Expand Up @@ -228,24 +233,15 @@ impl syn::parse::Parse for Module {
let docs = DocString::parse(&original_attrs);

for attr in original_attrs {
if attr.path == binary_op_path {
let meta = attr.parse_meta()?;
fn op_parse(meta: Meta) -> Result<Path> {
if let Meta::List(meta) = &meta {
if meta.nested.len() == 1 {
let inner = meta.nested.first().unwrap();
if let NestedMeta::Meta(Meta::Path(path)) = inner {
return Ok(path.clone());
}
}
}

Err(Error::new_spanned(
meta,
if *attr.path() == binary_op_path {
if let Ok(path) = attr.parse_args::<Path>() {
binary_op = Some(path);
} else {
return Err(Error::new_spanned(
&attr.meta,
"#[query_module] attribute `binary_op` should receive a single argument identifying the binary op like `#[binary_op(BinaryOp::Intersect)]`",
))
));
}
binary_op = Some(op_parse(meta)?);
} else {
method.attrs.push(attr)
}
Expand Down
2 changes: 1 addition & 1 deletion gazebo/dupe_derive/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ rust_library(
deps = [
"fbsource//third-party/rust:proc-macro2",
"fbsource//third-party/rust:quote",
"fbsource//third-party/rust:syn1",
"fbsource//third-party/rust:syn",
],
)
2 changes: 1 addition & 1 deletion gazebo/dupe_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ proc-macro = true

[dependencies]
proc-macro2 = "1.0"
syn = {version = "1.0.27", features = ["extra-traits"]}
syn = { version = "2", features = ["extra-traits"] }
quote = "1.0.3"
2 changes: 1 addition & 1 deletion gazebo/gazebo_derive/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ rust_library(
deps = [
"fbsource//third-party/rust:proc-macro2",
"fbsource//third-party/rust:quote",
"fbsource//third-party/rust:syn1",
"fbsource//third-party/rust:syn",
],
)
2 changes: 1 addition & 1 deletion gazebo/gazebo_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ proc-macro = true

[dependencies]
proc-macro2 = "1.0"
syn = {version = "1.0.27", features = ["extra-traits"]}
syn = { version = "2", features = ["extra-traits"] }
quote = "1.0.3"
2 changes: 1 addition & 1 deletion shim/third-party/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ smallvec = { version = "1.10", features = ["const_generics", "const_new", "serde
static_assertions = "1.1.0"
strsim = "0.10.0"
structopt = "0.3.23"
syn = { version = "1.0.27", features = ["extra-traits", "full"] }
syn = { version = "2", features = ["extra-traits", "full", "visit"] }
sync_wrapper = "0.1.0"
sys-info = "0.9.1"
sysinfo = "0.26.8"
Expand Down
2 changes: 1 addition & 1 deletion starlark-rust/starlark_derive/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ rust_library(
deps = [
"fbsource//third-party/rust:proc-macro2",
"fbsource//third-party/rust:quote",
"fbsource//third-party/rust:syn1",
"fbsource//third-party/rust:syn",
"//buck2/gazebo/dupe:dupe",
],
)
2 changes: 1 addition & 1 deletion starlark-rust/starlark_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ proc-macro = true

[dependencies]
proc-macro2 = "1.0"
syn = { version = "1.0.96", features = ["full", "extra-traits", "visit"] }
syn = { version = "2", features = ["full", "extra-traits", "visit"] }
dupe = { workspace = true }
quote = "1.0"
3 changes: 2 additions & 1 deletion starlark-rust/starlark_derive/src/any_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ fn type_param_bound_replace_lifetimes_with_static(
path: path_replace_lifetimes_with_static(&trait_bound.path)?,
}))
}
_ => Ok(bound.clone()),
}
}

Expand All @@ -123,7 +124,7 @@ pub(crate) fn derive_provides_static_type(
fn derive_provides_static_type_impl(
input: proc_macro::TokenStream,
) -> syn::Result<proc_macro::TokenStream> {
let input = syn::parse_macro_input::parse::<DeriveInput>(input)?;
let input: DeriveInput = syn::parse(input)?;

let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
Expand Down
41 changes: 14 additions & 27 deletions starlark-rust/starlark_derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ use proc_macro::TokenStream;
use proc_macro2::Ident;
use quote::quote;
use syn::parse_macro_input;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::Attribute;
use syn::Data;
use syn::DeriveInput;
use syn::Error;
use syn::Meta;
use syn::NestedMeta;
use syn::Result;
use syn::Token;
use syn::Type;

pub fn derive_attrs(input: TokenStream) -> TokenStream {
Expand Down Expand Up @@ -91,8 +91,6 @@ impl Field {
}
}

static STARLARK_ATTR_ERR_MSG: &str = "valid starlark attributes are {skip}";

fn expand_attrs_derive(data: Data, name: Ident) -> Result<proc_macro2::TokenStream> {
let fields: Vec<_> = match data {
Data::Struct(s) => Ok(s.fields.iter().cloned().collect()),
Expand All @@ -116,28 +114,17 @@ fn expand_attrs_derive(data: Data, name: Ident) -> Result<proc_macro2::TokenStre
starlark_args: vec![],
ty: field.ty,
}),
Some(attr) => match attr.parse_meta() {
Ok(Meta::List(lst)) => {
let starlark_args = lst
.nested
.iter()
.map(|m| match m {
NestedMeta::Meta(Meta::Path(p)) => p
.get_ident()
.ok_or_else(|| Error::new(m.span(), STARLARK_ATTR_ERR_MSG))
.map(|i| i.clone()),
_ => Err(Error::new(m.span(), STARLARK_ATTR_ERR_MSG)),
})
.collect::<Result<_>>()?;
Ok(Field {
ident: field.ident.unwrap(),
starlark_args,
ty: field.ty,
})
}
Ok(_) => Err(Error::new(attr.span(), "starlark attr must parse as list")),
Err(e) => Err(e),
},
Some(attr) => {
let starlark_args = attr
.parse_args_with(Punctuated::<Ident, Token![,]>::parse_terminated)?
.into_iter()
.collect();
Ok(Field {
ident: field.ident.unwrap(),
starlark_args,
ty: field.ty,
})
}
}
})
.filter(|f| f.as_ref().map(|f| !f.skip()).unwrap_or(true))
Expand Down Expand Up @@ -191,7 +178,7 @@ fn field_attr<'a, I: ?Sized>(field: &'a syn::Field, path: &I) -> Option<&'a Attr
where
Ident: PartialEq<I>,
{
field.attrs.iter().find(|a| a.path.is_ident(path))
field.attrs.iter().find(|a| a.path().is_ident(path))
}

pub fn starlark_attrs() -> TokenStream {
Expand Down
2 changes: 1 addition & 1 deletion starlark-rust/starlark_derive/src/coerce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fn check_repr(input: &DeriveInput) -> syn::Result<()> {
};

for attr in &input.attrs {
if attr.path.is_ident("repr") {
if attr.path().is_ident("repr") {
if let Err(error) = attr.parse_args_with(|input: ParseStream| {
while !input.is_empty() {
let path = input.call(Path::parse_mod_style)?;
Expand Down
10 changes: 8 additions & 2 deletions starlark-rust/starlark_derive/src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::Attribute;
use syn::DeriveInput;
use syn::Expr;
use syn::ExprLit;
use syn::MetaNameValue;
use syn::Token;

Expand Down Expand Up @@ -118,7 +120,11 @@ fn get_attrs(attr: Attribute) -> syn::Result<HashMap<String, String>> {
match &arg {
MetaNameValue {
path,
lit: syn::Lit::Str(s),
value:
Expr::Lit(ExprLit {
lit: syn::Lit::Str(s),
..
}),
..
} => {
let ident = path.get_ident().unwrap();
Expand Down Expand Up @@ -146,7 +152,7 @@ fn get_attrs(attr: Attribute) -> syn::Result<HashMap<String, String>> {

fn parse_custom_attributes(attrs: Vec<Attribute>) -> syn::Result<HashMap<String, String>> {
for attr in attrs {
if attr.path.is_ident(STARLARK_DOCS_ATTRS) {
if attr.path().is_ident(STARLARK_DOCS_ATTRS) {
return get_attrs(attr);
}
}
Expand Down
4 changes: 2 additions & 2 deletions starlark-rust/starlark_derive/src/freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn extract_options(attrs: &[Attribute]) -> syn::Result<FreezeDeriveOptions> {
let mut opts = FreezeDeriveOptions::default();

for attr in attrs.iter() {
if !attr.path.is_ident("freeze") {
if !attr.path().is_ident("freeze") {
continue;
}

Expand Down Expand Up @@ -196,7 +196,7 @@ fn is_identity(attrs: &[Attribute]) -> syn::Result<bool> {
syn::custom_keyword!(identity);

for attr in attrs.iter() {
if !attr.path.is_ident("freeze") {
if !attr.path().is_ident("freeze") {
continue;
}

Expand Down
8 changes: 4 additions & 4 deletions starlark-rust/starlark_derive/src/module/parse/fun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn parse_starlark_fn_param_attr(
tokens: &Attribute,
param_attrs: &mut FnParamAttrs,
) -> syn::Result<()> {
assert!(tokens.path.is_ident("starlark"));
assert!(tokens.path().is_ident("starlark"));
let parse = |parser: ParseStream| -> syn::Result<()> {
let mut first = true;
while !parser.is_empty() {
Expand Down Expand Up @@ -141,7 +141,7 @@ fn parse_starlark_fn_param_attr(
fn parse_fn_param_attrs(attrs: Vec<Attribute>) -> syn::Result<FnParamAttrs> {
let mut param_attrs = FnParamAttrs::default();
for attr in attrs {
if attr.path.is_ident("starlark") {
if attr.path().is_ident("starlark") {
parse_starlark_fn_param_attr(&attr, &mut param_attrs)?;
} else {
param_attrs.unused_attrs.push(attr);
Expand All @@ -152,7 +152,7 @@ fn parse_fn_param_attrs(attrs: Vec<Attribute>) -> syn::Result<FnParamAttrs> {

/// Parse `#[starlark(...)]` fn attribute.
fn parse_starlark_fn_attr(tokens: &Attribute, attrs: &mut FnAttrs) -> syn::Result<()> {
assert!(tokens.path.is_ident("starlark"));
assert!(tokens.path().is_ident("starlark"));
let parse = |parser: ParseStream| -> syn::Result<()> {
let mut first = true;
while !parser.is_empty() {
Expand Down Expand Up @@ -202,7 +202,7 @@ fn parse_starlark_fn_attr(tokens: &Attribute, attrs: &mut FnAttrs) -> syn::Resul
fn parse_fn_attrs(span: Span, xs: Vec<Attribute>) -> syn::Result<FnAttrs> {
let mut res = FnAttrs::default();
for x in xs {
if x.path.is_ident("starlark") {
if x.path().is_ident("starlark") {
parse_starlark_fn_attr(&x, &mut res)?;
} else if let Some(ds) = is_attribute_docstring(&x) {
match &mut res.docstring {
Expand Down
Loading

0 comments on commit 697ad1f

Please sign in to comment.