Update syn crate from 1.0.109 to 2.0.101 (#31301)

Nearly all generated by Zed Agent + Claude Opus 4. I just wrote the test
`Args` struct and pointed it at the [2.0 release
notes](https://github.com/dtolnay/syn/releases/tag/2.0.0).

Release Notes:

- N/A
This commit is contained in:
Michael Sloan 2025-05-23 13:31:25 -06:00 committed by GitHub
parent 208f525a11
commit f3c2e71ca7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 155 additions and 90 deletions

10
Cargo.lock generated
View file

@ -4351,7 +4351,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 1.0.109", "syn 2.0.101",
"workspace-hack", "workspace-hack",
] ]
@ -7125,7 +7125,7 @@ dependencies = [
"gpui", "gpui",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 1.0.109", "syn 2.0.101",
"workspace-hack", "workspace-hack",
] ]
@ -14757,7 +14757,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"sqlez", "sqlez",
"sqlformat", "sqlformat",
"syn 1.0.109", "syn 2.0.101",
"workspace-hack", "workspace-hack",
] ]
@ -16842,7 +16842,7 @@ name = "ui_macros"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"quote", "quote",
"syn 1.0.109", "syn 2.0.101",
"workspace-hack", "workspace-hack",
] ]
@ -17099,7 +17099,7 @@ name = "util_macros"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"quote", "quote",
"syn 1.0.109", "syn 2.0.101",
"workspace-hack", "workspace-hack",
] ]

View file

@ -550,7 +550,7 @@ streaming-iterator = "0.1"
strsim = "0.11" strsim = "0.11"
strum = { version = "0.27.0", features = ["derive"] } strum = { version = "0.27.0", features = ["derive"] }
subtle = "2.5.0" subtle = "2.5.0"
syn = { version = "1.0.72", features = ["full", "extra-traits"] } syn = { version = "2.0.101", features = ["full", "extra-traits"] }
sys-locale = "0.3.1" sys-locale = "0.3.1"
sysinfo = "0.31.0" sysinfo = "0.31.0"
take-until = "0.2.0" take-until = "0.2.0"

View file

@ -183,7 +183,7 @@ pub(crate) fn get_simple_attribute_field(ast: &DeriveInput, name: &'static str)
syn::Data::Struct(data_struct) => data_struct syn::Data::Struct(data_struct) => data_struct
.fields .fields
.iter() .iter()
.find(|field| field.attrs.iter().any(|attr| attr.path.is_ident(name))) .find(|field| field.attrs.iter().any(|attr| attr.path().is_ident(name)))
.map(|field| field.ident.clone().unwrap()), .map(|field| field.ident.clone().unwrap()),
syn::Data::Enum(_) => None, syn::Data::Enum(_) => None,
syn::Data::Union(_) => None, syn::Data::Union(_) => None,

View file

@ -3,46 +3,60 @@ use proc_macro2::Ident;
use quote::{format_ident, quote}; use quote::{format_ident, quote};
use std::mem; use std::mem;
use syn::{ use syn::{
AttributeArgs, FnArg, ItemFn, Lit, Meta, MetaList, NestedMeta, PathSegment, Type, parse_quote, self, Expr, ExprLit, FnArg, ItemFn, Lit, Meta, MetaList, PathSegment, Token, Type,
parse::{Parse, ParseStream},
parse_quote,
punctuated::Punctuated,
spanned::Spanned, spanned::Spanned,
}; };
pub fn test(args: TokenStream, function: TokenStream) -> TokenStream { struct Args {
let args = syn::parse_macro_input!(args as AttributeArgs); seeds: Vec<u64>,
try_test(args, function).unwrap_or_else(|err| err) max_retries: usize,
max_iterations: usize,
on_failure_fn_name: proc_macro2::TokenStream,
} }
fn try_test(args: Vec<NestedMeta>, function: TokenStream) -> Result<TokenStream, TokenStream> { impl Parse for Args {
fn parse(input: ParseStream) -> Result<Self, syn::Error> {
let mut seeds = Vec::<u64>::new(); let mut seeds = Vec::<u64>::new();
let mut max_retries = 0; let mut max_retries = 0;
let mut num_iterations = 1; let mut max_iterations = 1;
let mut on_failure_fn_name = quote!(None); let mut on_failure_fn_name = quote!(None);
for arg in args { let metas = Punctuated::<Meta, Token![,]>::parse_terminated(input)?;
let NestedMeta::Meta(arg) = arg else {
return Err(error_with_message("unexpected literal", arg));
};
for meta in metas {
let ident = { let ident = {
let meta_path = match &arg { let meta_path = match &meta {
Meta::NameValue(meta) => &meta.path, Meta::NameValue(meta) => &meta.path,
Meta::List(list) => &list.path, Meta::List(list) => &list.path,
Meta::Path(path) => return Err(error_with_message("invalid path argument", path)), Meta::Path(path) => {
return Err(syn::Error::new(path.span(), "invalid path argument"));
}
}; };
let Some(ident) = meta_path.get_ident() else { let Some(ident) = meta_path.get_ident() else {
return Err(error_with_message("unexpected path", meta_path)); return Err(syn::Error::new(meta_path.span(), "unexpected path"));
}; };
ident.to_string() ident.to_string()
}; };
match (&arg, ident.as_str()) { match (&meta, ident.as_str()) {
(Meta::NameValue(meta), "retries") => max_retries = parse_usize(&meta.lit)?, (Meta::NameValue(meta), "retries") => {
(Meta::NameValue(meta), "iterations") => num_iterations = parse_usize(&meta.lit)?, max_retries = parse_usize_from_expr(&meta.value)?
}
(Meta::NameValue(meta), "iterations") => {
max_iterations = parse_usize_from_expr(&meta.value)?
}
(Meta::NameValue(meta), "on_failure") => { (Meta::NameValue(meta), "on_failure") => {
let Lit::Str(name) = &meta.lit else { let Expr::Lit(ExprLit {
return Err(error_with_message( lit: Lit::Str(name),
..
}) = &meta.value
else {
return Err(syn::Error::new(
meta.value.span(),
"on_failure argument must be a string", "on_failure argument must be a string",
&meta.lit,
)); ));
}; };
let segments = name let segments = name
@ -56,23 +70,65 @@ fn try_test(args: Vec<NestedMeta>, function: TokenStream) -> Result<TokenStream,
}; };
on_failure_fn_name = quote!(Some(#path)); on_failure_fn_name = quote!(Some(#path));
} }
(Meta::NameValue(meta), "seed") => seeds = vec![parse_usize(&meta.lit)? as u64], (Meta::NameValue(meta), "seed") => {
seeds = vec![parse_usize_from_expr(&meta.value)? as u64]
}
(Meta::List(list), "seeds") => seeds = parse_u64_array(&list)?, (Meta::List(list), "seeds") => seeds = parse_u64_array(&list)?,
(Meta::Path(path), _) => { (Meta::Path(_), _) => {
return Err(error_with_message("invalid path argument", path)); return Err(syn::Error::new(meta.span(), "invalid path argument"));
} }
(_, _) => { (_, _) => {
return Err(error_with_message("invalid argument name", arg)); return Err(syn::Error::new(meta.span(), "invalid argument name"));
} }
} }
} }
let seeds = quote!( #(#seeds),* );
let mut inner_fn = syn::parse::<ItemFn>(function).map_err(error_to_stream)?; Ok(Args {
seeds,
max_retries,
max_iterations: max_iterations,
on_failure_fn_name,
})
}
}
pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
let args = syn::parse_macro_input!(args as Args);
let mut inner_fn = match syn::parse::<ItemFn>(function) {
Ok(f) => f,
Err(err) => return error_to_stream(err),
};
let inner_fn_attributes = mem::take(&mut inner_fn.attrs); let inner_fn_attributes = mem::take(&mut inner_fn.attrs);
let inner_fn_name = format_ident!("_{}", inner_fn.sig.ident); let inner_fn_name = format_ident!("_{}", inner_fn.sig.ident);
let outer_fn_name = mem::replace(&mut inner_fn.sig.ident, inner_fn_name.clone()); let outer_fn_name = mem::replace(&mut inner_fn.sig.ident, inner_fn_name.clone());
let result = generate_test_function(
args,
inner_fn,
inner_fn_attributes,
inner_fn_name,
outer_fn_name,
);
match result {
Ok(tokens) => tokens,
Err(tokens) => tokens,
}
}
fn generate_test_function(
args: Args,
inner_fn: ItemFn,
inner_fn_attributes: Vec<syn::Attribute>,
inner_fn_name: Ident,
outer_fn_name: Ident,
) -> Result<TokenStream, TokenStream> {
let seeds = &args.seeds;
let max_retries = args.max_retries;
let num_iterations = args.max_iterations;
let on_failure_fn_name = &args.on_failure_fn_name;
let seeds = quote!( #(#seeds),* );
let mut outer_fn: ItemFn = if inner_fn.sig.asyncness.is_some() { let mut outer_fn: ItemFn = if inner_fn.sig.asyncness.is_some() {
// Pass to the test function the number of app contexts that it needs, // Pass to the test function the number of app contexts that it needs,
// based on its parameter list. // based on its parameter list.
@ -230,25 +286,37 @@ fn try_test(args: Vec<NestedMeta>, function: TokenStream) -> Result<TokenStream,
Ok(TokenStream::from(quote!(#outer_fn))) Ok(TokenStream::from(quote!(#outer_fn)))
} }
fn parse_usize(literal: &Lit) -> Result<usize, TokenStream> { fn parse_usize_from_expr(expr: &Expr) -> Result<usize, syn::Error> {
let Lit::Int(int) = &literal else { let Expr::Lit(ExprLit {
return Err(error_with_message("expected an usize", literal)); lit: Lit::Int(int), ..
}) = expr
else {
return Err(syn::Error::new(expr.span(), "expected an integer"));
}; };
int.base10_parse().map_err(error_to_stream) int.base10_parse()
.map_err(|_| syn::Error::new(int.span(), "failed to parse integer"))
} }
fn parse_u64_array(meta_list: &MetaList) -> Result<Vec<u64>, TokenStream> { fn parse_u64_array(meta_list: &MetaList) -> Result<Vec<u64>, syn::Error> {
meta_list let mut result = Vec::new();
.nested let tokens = &meta_list.tokens;
.iter() let parser = |input: ParseStream| {
.map(|meta| { let exprs = Punctuated::<Expr, Token![,]>::parse_terminated(input)?;
if let NestedMeta::Lit(literal) = &meta { for expr in exprs {
parse_usize(literal).map(|value| value as u64) if let Expr::Lit(ExprLit {
lit: Lit::Int(int), ..
}) = expr
{
let value: usize = int.base10_parse()?;
result.push(value as u64);
} else { } else {
Err(error_with_message("expected an integer", meta.span())) return Err(syn::Error::new(expr.span(), "expected an integer"));
} }
}) }
.collect() Ok(())
};
syn::parse::Parser::parse2(parser, tokens.clone())?;
Ok(result)
} }
fn error_with_message(message: &str, spanned: impl Spanned) -> TokenStream { fn error_with_message(message: &str, spanned: impl Spanned) -> TokenStream {

View file

@ -16,25 +16,20 @@ pub fn derive_refineable(input: TokenStream) -> TokenStream {
.. ..
} = parse_macro_input!(input); } = parse_macro_input!(input);
let refineable_attr = attrs.iter().find(|attr| attr.path.is_ident("refineable")); let refineable_attr = attrs.iter().find(|attr| attr.path().is_ident("refineable"));
let mut impl_debug_on_refinement = false; let mut impl_debug_on_refinement = false;
let mut refinement_traits_to_derive = vec![]; let mut refinement_traits_to_derive = vec![];
if let Some(refineable_attr) = refineable_attr { if let Some(refineable_attr) = refineable_attr {
if let Ok(syn::Meta::List(meta_list)) = refineable_attr.parse_meta() { let _ = refineable_attr.parse_nested_meta(|meta| {
for nested in meta_list.nested { if meta.path.is_ident("Debug") {
let syn::NestedMeta::Meta(syn::Meta::Path(path)) = nested else {
continue;
};
if path.is_ident("Debug") {
impl_debug_on_refinement = true; impl_debug_on_refinement = true;
} else { } else {
refinement_traits_to_derive.push(path); refinement_traits_to_derive.push(meta.path);
}
}
} }
Ok(())
});
} }
let refinement_ident = format_ident!("{}Refinement", ident); let refinement_ident = format_ident!("{}Refinement", ident);
@ -325,7 +320,9 @@ pub fn derive_refineable(input: TokenStream) -> TokenStream {
} }
fn is_refineable_field(f: &Field) -> bool { fn is_refineable_field(f: &Field) -> bool {
f.attrs.iter().any(|attr| attr.path.is_ident("refineable")) f.attrs
.iter()
.any(|attr| attr.path().is_ident("refineable"))
} }
fn is_optional_field(f: &Field) -> bool { fn is_optional_field(f: &Field) -> bool {

View file

@ -23,7 +23,7 @@ enum DynamicSpacingValue {
impl Parse for DynamicSpacingInput { impl Parse for DynamicSpacingInput {
fn parse(input: ParseStream) -> syn::Result<Self> { fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(DynamicSpacingInput { Ok(DynamicSpacingInput {
values: input.parse_terminated(DynamicSpacingValue::parse)?, values: input.parse_terminated(DynamicSpacingValue::parse, Token![,])?,
}) })
} }
} }