From 4926929a7f57a1591423da70c691cf76d71c9ba0 Mon Sep 17 00:00:00 2001 From: cryptopapi997 Date: Wed, 16 Apr 2025 12:43:19 +0200 Subject: [PATCH 1/8] bump proc-macro2 --- Cargo.lock | 4 ++-- lang/syn/src/idl/defined.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 92db1ffa26..fa50d7b9b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3000,9 +3000,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.89" +version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" dependencies = [ "unicode-ident", ] diff --git a/lang/syn/src/idl/defined.rs b/lang/syn/src/idl/defined.rs index 334387a20c..9b9244afb1 100644 --- a/lang/syn/src/idl/defined.rs +++ b/lang/syn/src/idl/defined.rs @@ -496,7 +496,7 @@ pub fn gen_idl_type( use crate::parser::context::CrateContext; use quote::ToTokens; - let source_path = proc_macro2::Span::call_site().source_file().path(); + let source_path = proc_macro2::Span::call_site().file(); if let Ok(Ok(ctx)) = find_path("lib.rs", &source_path).map(CrateContext::parse) { let name = path.path.segments.last().unwrap().ident.to_string(); let alias = ctx.type_aliases().find(|ty| ty.ident == name); From 7fb5b4039ac88d27de7abdd0145383350c837d64 Mon Sep 17 00:00:00 2001 From: cryptopapi997 Date: Wed, 16 Apr 2025 12:51:42 +0200 Subject: [PATCH 2/8] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34cc9f6c39..df68ca5fbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ The minor version will be incremented upon a breaking change and the patch versi ### Fixes +- idl: Update `proc-macro2` usage for latest nightly ([#3663](https://github.com/solana-foundation/anchor/pull/3663)) - cli, docker: Replace `backpackapp/build` Docker image with `solanafoundation/anchor` ([#3619](https://github.com/coral-xyz/anchor/pull/3619)). ### Breaking From a7e2b4552a8cdde311f306ff02504218c4305702 Mon Sep 17 00:00:00 2001 From: cryptopapi997 Date: Wed, 16 Apr 2025 13:26:47 +0200 Subject: [PATCH 3/8] file() -> local_file() --- lang/syn/src/idl/defined.rs | 142 ++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/lang/syn/src/idl/defined.rs b/lang/syn/src/idl/defined.rs index 9b9244afb1..47614e7e81 100644 --- a/lang/syn/src/idl/defined.rs +++ b/lang/syn/src/idl/defined.rs @@ -495,80 +495,80 @@ pub fn gen_idl_type( use super::{common::find_path, external::get_external_type}; use crate::parser::context::CrateContext; use quote::ToTokens; - - let source_path = proc_macro2::Span::call_site().file(); - if let Ok(Ok(ctx)) = find_path("lib.rs", &source_path).map(CrateContext::parse) { - let name = path.path.segments.last().unwrap().ident.to_string(); - let alias = ctx.type_aliases().find(|ty| ty.ident == name); - if let Some(alias) = alias { - if let Some(segment) = path.path.segments.last() { - if let syn::PathArguments::AngleBracketed(args) = &segment.arguments { - let inners = args - .args - .iter() - .map(|arg| match arg { - syn::GenericArgument::Type(ty) => match ty { - syn::Type::Path(inner_ty) => { - inner_ty.path.to_token_stream().to_string() - } - _ => { - unimplemented!("Inner type not implemented: {ty:?}") + if let Some(source_path) = proc_macro2::Span::call_site().local_file() { + if let Ok(Ok(ctx)) = find_path("lib.rs", &source_path).map(CrateContext::parse) { + let name = path.path.segments.last().unwrap().ident.to_string(); + let alias = ctx.type_aliases().find(|ty| ty.ident == name); + if let Some(alias) = alias { + if let Some(segment) = path.path.segments.last() { + if let syn::PathArguments::AngleBracketed(args) = &segment.arguments { + let inners = args + .args + .iter() + .map(|arg| match arg { + syn::GenericArgument::Type(ty) => match ty { + syn::Type::Path(inner_ty) => { + inner_ty.path.to_token_stream().to_string() + } + _ => { + unimplemented!("Inner type not implemented: {ty:?}") + } + }, + syn::GenericArgument::Const(c) => { + c.to_token_stream().to_string() } - }, - syn::GenericArgument::Const(c) => { - c.to_token_stream().to_string() - } - _ => unimplemented!("Arg not implemented: {arg:?}"), - }) - .collect::>(); - - let outer = match &*alias.ty { - syn::Type::Path(outer_ty) => outer_ty.path.to_token_stream(), - syn::Type::Array(outer_ty) => outer_ty.to_token_stream(), - _ => unimplemented!("Type not implemented: {:?}", alias.ty), - } - .to_string(); - - let resolved_alias = alias - .generics - .params - .iter() - .map(|param| match param { - syn::GenericParam::Const(param) => param.ident.to_string(), - syn::GenericParam::Type(param) => param.ident.to_string(), - _ => panic!("Lifetime parameters are not allowed"), - }) - .enumerate() - .fold(outer, |acc, (i, cur)| { - let inner = &inners[i]; - // The spacing of the `outer` variable can differ between - // versions, e.g. `[T; N]` and `[T ; N]` - acc.replace(&format!(" {cur} "), &format!(" {inner} ")) - .replace(&format!(" {cur},"), &format!(" {inner},")) - .replace(&format!("[{cur} "), &format!("[{inner} ")) - .replace(&format!("[{cur};"), &format!("[{inner};")) - .replace(&format!(" {cur}]"), &format!(" {inner}]")) - }); - if let Ok(ty) = syn::parse_str(&resolved_alias) { - return gen_idl_type(&ty, generic_params); + _ => unimplemented!("Arg not implemented: {arg:?}"), + }) + .collect::>(); + + let outer = match &*alias.ty { + syn::Type::Path(outer_ty) => outer_ty.path.to_token_stream(), + syn::Type::Array(outer_ty) => outer_ty.to_token_stream(), + _ => unimplemented!("Type not implemented: {:?}", alias.ty), + } + .to_string(); + + let resolved_alias = alias + .generics + .params + .iter() + .map(|param| match param { + syn::GenericParam::Const(param) => param.ident.to_string(), + syn::GenericParam::Type(param) => param.ident.to_string(), + _ => panic!("Lifetime parameters are not allowed"), + }) + .enumerate() + .fold(outer, |acc, (i, cur)| { + let inner = &inners[i]; + // The spacing of the `outer` variable can differ between + // versions, e.g. `[T; N]` and `[T ; N]` + acc.replace(&format!(" {cur} "), &format!(" {inner} ")) + .replace(&format!(" {cur},"), &format!(" {inner},")) + .replace(&format!("[{cur} "), &format!("[{inner} ")) + .replace(&format!("[{cur};"), &format!("[{inner};")) + .replace(&format!(" {cur}]"), &format!(" {inner}]")) + }); + if let Ok(ty) = syn::parse_str(&resolved_alias) { + return gen_idl_type(&ty, generic_params); + } } - } - }; + }; - // Non-generic type alias e.g. `type UnixTimestamp = i64` - return gen_idl_type(&*alias.ty, generic_params); - } + // Non-generic type alias e.g. `type UnixTimestamp = i64` + return gen_idl_type(&*alias.ty, generic_params); + } - // Handle external types - let is_external = ctx - .structs() - .map(|s| s.ident.to_string()) - .chain(ctx.enums().map(|e| e.ident.to_string())) - .find(|defined| defined == &name) - .is_none(); - if is_external { - if let Ok(Some(ty)) = get_external_type(&name, source_path) { - return gen_idl_type(&ty, generic_params); + // Handle external types + let is_external = ctx + .structs() + .map(|s| s.ident.to_string()) + .chain(ctx.enums().map(|e| e.ident.to_string())) + .find(|defined| defined == &name) + .is_none(); + if is_external { + if let Ok(Some(ty)) = get_external_type(&name, source_path) { + return gen_idl_type(&ty, generic_params); + } } } } @@ -637,4 +637,4 @@ pub fn gen_idl_type( fn get_first_segment(type_path: &syn::TypePath) -> &syn::PathSegment { type_path.path.segments.first().unwrap() -} +} \ No newline at end of file From f2adbf19a56d1675aa04de005e42e3e66f662bf6 Mon Sep 17 00:00:00 2001 From: cryptopapi997 Date: Wed, 16 Apr 2025 13:27:40 +0200 Subject: [PATCH 4/8] fmt --- lang/syn/src/idl/defined.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lang/syn/src/idl/defined.rs b/lang/syn/src/idl/defined.rs index 47614e7e81..6b49dc5946 100644 --- a/lang/syn/src/idl/defined.rs +++ b/lang/syn/src/idl/defined.rs @@ -495,13 +495,16 @@ pub fn gen_idl_type( use super::{common::find_path, external::get_external_type}; use crate::parser::context::CrateContext; use quote::ToTokens; + if let Some(source_path) = proc_macro2::Span::call_site().local_file() { - if let Ok(Ok(ctx)) = find_path("lib.rs", &source_path).map(CrateContext::parse) { + if let Ok(Ok(ctx)) = find_path("lib.rs", &source_path).map(CrateContext::parse) + { let name = path.path.segments.last().unwrap().ident.to_string(); let alias = ctx.type_aliases().find(|ty| ty.ident == name); if let Some(alias) = alias { if let Some(segment) = path.path.segments.last() { - if let syn::PathArguments::AngleBracketed(args) = &segment.arguments { + if let syn::PathArguments::AngleBracketed(args) = &segment.arguments + { let inners = args .args .iter() @@ -511,7 +514,9 @@ pub fn gen_idl_type( inner_ty.path.to_token_stream().to_string() } _ => { - unimplemented!("Inner type not implemented: {ty:?}") + unimplemented!( + "Inner type not implemented: {ty:?}" + ) } }, syn::GenericArgument::Const(c) => { @@ -522,7 +527,9 @@ pub fn gen_idl_type( .collect::>(); let outer = match &*alias.ty { - syn::Type::Path(outer_ty) => outer_ty.path.to_token_stream(), + syn::Type::Path(outer_ty) => { + outer_ty.path.to_token_stream() + } syn::Type::Array(outer_ty) => outer_ty.to_token_stream(), _ => unimplemented!("Type not implemented: {:?}", alias.ty), } @@ -533,8 +540,12 @@ pub fn gen_idl_type( .params .iter() .map(|param| match param { - syn::GenericParam::Const(param) => param.ident.to_string(), - syn::GenericParam::Type(param) => param.ident.to_string(), + syn::GenericParam::Const(param) => { + param.ident.to_string() + } + syn::GenericParam::Type(param) => { + param.ident.to_string() + } _ => panic!("Lifetime parameters are not allowed"), }) .enumerate() @@ -637,4 +648,4 @@ pub fn gen_idl_type( fn get_first_segment(type_path: &syn::TypePath) -> &syn::PathSegment { type_path.path.segments.first().unwrap() -} \ No newline at end of file +} From 1c44e6da45251efa54ed29b8f069a16c750646cb Mon Sep 17 00:00:00 2001 From: cryptopapi997 Date: Thu, 17 Apr 2025 10:14:54 +0200 Subject: [PATCH 5/8] fmt, remove extra indents --- lang/syn/src/idl/defined.rs | 149 +++++++++++++++++------------------- 1 file changed, 71 insertions(+), 78 deletions(-) diff --git a/lang/syn/src/idl/defined.rs b/lang/syn/src/idl/defined.rs index 6b49dc5946..60aa4168fe 100644 --- a/lang/syn/src/idl/defined.rs +++ b/lang/syn/src/idl/defined.rs @@ -496,90 +496,83 @@ pub fn gen_idl_type( use crate::parser::context::CrateContext; use quote::ToTokens; - if let Some(source_path) = proc_macro2::Span::call_site().local_file() { - if let Ok(Ok(ctx)) = find_path("lib.rs", &source_path).map(CrateContext::parse) - { - let name = path.path.segments.last().unwrap().ident.to_string(); - let alias = ctx.type_aliases().find(|ty| ty.ident == name); - if let Some(alias) = alias { - if let Some(segment) = path.path.segments.last() { - if let syn::PathArguments::AngleBracketed(args) = &segment.arguments - { - let inners = args - .args - .iter() - .map(|arg| match arg { - syn::GenericArgument::Type(ty) => match ty { - syn::Type::Path(inner_ty) => { - inner_ty.path.to_token_stream().to_string() - } - _ => { - unimplemented!( - "Inner type not implemented: {ty:?}" - ) - } - }, - syn::GenericArgument::Const(c) => { - c.to_token_stream().to_string() - } - _ => unimplemented!("Arg not implemented: {arg:?}"), - }) - .collect::>(); + let source_path = match proc_macro2::Span::call_site().local_file() { + Some(source_path) => source_path, + None => return Err(anyhow!("No source path found")), + }; - let outer = match &*alias.ty { - syn::Type::Path(outer_ty) => { - outer_ty.path.to_token_stream() - } - syn::Type::Array(outer_ty) => outer_ty.to_token_stream(), - _ => unimplemented!("Type not implemented: {:?}", alias.ty), - } - .to_string(); - - let resolved_alias = alias - .generics - .params - .iter() - .map(|param| match param { - syn::GenericParam::Const(param) => { - param.ident.to_string() + if let Ok(Ok(ctx)) = find_path("lib.rs", &source_path).map(CrateContext::parse) { + let name = path.path.segments.last().unwrap().ident.to_string(); + let alias = ctx.type_aliases().find(|ty| ty.ident == name); + if let Some(alias) = alias { + if let Some(segment) = path.path.segments.last() { + if let syn::PathArguments::AngleBracketed(args) = &segment.arguments { + let inners = args + .args + .iter() + .map(|arg| match arg { + syn::GenericArgument::Type(ty) => match ty { + syn::Type::Path(inner_ty) => { + inner_ty.path.to_token_stream().to_string() } - syn::GenericParam::Type(param) => { - param.ident.to_string() + _ => { + unimplemented!("Inner type not implemented: {ty:?}") } - _ => panic!("Lifetime parameters are not allowed"), - }) - .enumerate() - .fold(outer, |acc, (i, cur)| { - let inner = &inners[i]; - // The spacing of the `outer` variable can differ between - // versions, e.g. `[T; N]` and `[T ; N]` - acc.replace(&format!(" {cur} "), &format!(" {inner} ")) - .replace(&format!(" {cur},"), &format!(" {inner},")) - .replace(&format!("[{cur} "), &format!("[{inner} ")) - .replace(&format!("[{cur};"), &format!("[{inner};")) - .replace(&format!(" {cur}]"), &format!(" {inner}]")) - }); - if let Ok(ty) = syn::parse_str(&resolved_alias) { - return gen_idl_type(&ty, generic_params); - } + }, + syn::GenericArgument::Const(c) => { + c.to_token_stream().to_string() + } + _ => unimplemented!("Arg not implemented: {arg:?}"), + }) + .collect::>(); + + let outer = match &*alias.ty { + syn::Type::Path(outer_ty) => outer_ty.path.to_token_stream(), + syn::Type::Array(outer_ty) => outer_ty.to_token_stream(), + _ => unimplemented!("Type not implemented: {:?}", alias.ty), + } + .to_string(); + + let resolved_alias = alias + .generics + .params + .iter() + .map(|param| match param { + syn::GenericParam::Const(param) => param.ident.to_string(), + syn::GenericParam::Type(param) => param.ident.to_string(), + _ => panic!("Lifetime parameters are not allowed"), + }) + .enumerate() + .fold(outer, |acc, (i, cur)| { + let inner = &inners[i]; + // The spacing of the `outer` variable can differ between + // versions, e.g. `[T; N]` and `[T ; N]` + acc.replace(&format!(" {cur} "), &format!(" {inner} ")) + .replace(&format!(" {cur},"), &format!(" {inner},")) + .replace(&format!("[{cur} "), &format!("[{inner} ")) + .replace(&format!("[{cur};"), &format!("[{inner};")) + .replace(&format!(" {cur}]"), &format!(" {inner}]")) + }); + if let Ok(ty) = syn::parse_str(&resolved_alias) { + return gen_idl_type(&ty, generic_params); } - }; + } + }; - // Non-generic type alias e.g. `type UnixTimestamp = i64` - return gen_idl_type(&*alias.ty, generic_params); - } + // Non-generic type alias e.g. `type UnixTimestamp = i64` + return gen_idl_type(&*alias.ty, generic_params); + } - // Handle external types - let is_external = ctx - .structs() - .map(|s| s.ident.to_string()) - .chain(ctx.enums().map(|e| e.ident.to_string())) - .find(|defined| defined == &name) - .is_none(); - if is_external { - if let Ok(Some(ty)) = get_external_type(&name, source_path) { - return gen_idl_type(&ty, generic_params); - } + // Handle external types + let is_external = ctx + .structs() + .map(|s| s.ident.to_string()) + .chain(ctx.enums().map(|e| e.ident.to_string())) + .find(|defined| defined == &name) + .is_none(); + if is_external { + if let Ok(Some(ty)) = get_external_type(&name, source_path) { + return gen_idl_type(&ty, generic_params); } } } From daa1b1526de9dfc53be238c2eada2673c4942723 Mon Sep 17 00:00:00 2001 From: cryptopapi997 Date: Thu, 17 Apr 2025 23:04:05 +0200 Subject: [PATCH 6/8] return empty path instead of error --- lang/syn/src/idl/defined.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lang/syn/src/idl/defined.rs b/lang/syn/src/idl/defined.rs index 60aa4168fe..a60ad51401 100644 --- a/lang/syn/src/idl/defined.rs +++ b/lang/syn/src/idl/defined.rs @@ -498,7 +498,8 @@ pub fn gen_idl_type( let source_path = match proc_macro2::Span::call_site().local_file() { Some(source_path) => source_path, - None => return Err(anyhow!("No source path found")), + // If no path was found, just return an empty path and let the find_path function handle it + None => std::path::PathBuf::new(), }; if let Ok(Ok(ctx)) = find_path("lib.rs", &source_path).map(CrateContext::parse) { From acf5d8d974b782e41fc4987123c1cf75157e0e3a Mon Sep 17 00:00:00 2001 From: cryptopapi997 Date: Thu, 17 Apr 2025 23:06:45 +0200 Subject: [PATCH 7/8] clippy --- lang/syn/src/idl/defined.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lang/syn/src/idl/defined.rs b/lang/syn/src/idl/defined.rs index a60ad51401..ac009f95b1 100644 --- a/lang/syn/src/idl/defined.rs +++ b/lang/syn/src/idl/defined.rs @@ -496,12 +496,9 @@ pub fn gen_idl_type( use crate::parser::context::CrateContext; use quote::ToTokens; - let source_path = match proc_macro2::Span::call_site().local_file() { - Some(source_path) => source_path, - // If no path was found, just return an empty path and let the find_path function handle it - None => std::path::PathBuf::new(), - }; - + // If no path was found, just return an empty path and let the find_path function handle it + let source_path = proc_macro2::Span::call_site().local_file().unwrap_or(std::path::PathBuf::new()); + if let Ok(Ok(ctx)) = find_path("lib.rs", &source_path).map(CrateContext::parse) { let name = path.path.segments.last().unwrap().ident.to_string(); let alias = ctx.type_aliases().find(|ty| ty.ident == name); From 5530e72956d46873a7b91eefbe29c2f66952dc5c Mon Sep 17 00:00:00 2001 From: cryptopapi997 Date: Thu, 17 Apr 2025 23:15:56 +0200 Subject: [PATCH 8/8] unwrap_or_default + fmt --- lang/syn/src/idl/defined.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lang/syn/src/idl/defined.rs b/lang/syn/src/idl/defined.rs index ac009f95b1..07786d2db0 100644 --- a/lang/syn/src/idl/defined.rs +++ b/lang/syn/src/idl/defined.rs @@ -497,8 +497,10 @@ pub fn gen_idl_type( use quote::ToTokens; // If no path was found, just return an empty path and let the find_path function handle it - let source_path = proc_macro2::Span::call_site().local_file().unwrap_or(std::path::PathBuf::new()); - + let source_path = proc_macro2::Span::call_site() + .local_file() + .unwrap_or_default(); + if let Ok(Ok(ctx)) = find_path("lib.rs", &source_path).map(CrateContext::parse) { let name = path.path.segments.last().unwrap().ident.to_string(); let alias = ctx.type_aliases().find(|ty| ty.ident == name);