Skip to content

Commit f4eb80e

Browse files
authored
Upgrade to syn 2.0 (#875)
* WIP updating to syn 2.0.0 * WIP darling compat * Update darling and syn workspace deps * NestedMeta::parse_meta_list * Rename attribute keyword type property to path * Fmt * Update more type to path * Unused darling * Cargo.lock * Add missing syn features
1 parent fd046b0 commit f4eb80e

File tree

8 files changed

+79
-41
lines changed

8 files changed

+79
-41
lines changed

Cargo.lock

+45-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ criterion = "0.4"
3838
codec = { package = "parity-scale-codec", version = "3.4.0", default-features = false }
3939
color-eyre = "0.6.1"
4040
console_error_panic_hook = "0.1.7"
41-
darling = "0.14.4"
41+
darling = "0.20.0"
4242
derivative = "2.2.0"
4343
either = "1.8.1"
4444
frame-metadata = { version = "15.1.0", features = ["v14", "v15-unstable", "std"] }
@@ -61,7 +61,7 @@ scale-decode = "0.5.0"
6161
scale-encode = "0.1.0"
6262
serde = { version = "1.0.159" }
6363
serde_json = { version = "1.0.96" }
64-
syn = "1.0.109"
64+
syn = { version = "2.0.15", features = ["full", "extra-traits"] }
6565
thiserror = "1.0.40"
6666
tokio = { version = "1.28", features = ["macros", "time", "rt-multi-thread"] }
6767
tracing = "0.1.34"

codegen/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ description = "Generate an API for interacting with a substrate node from FRAME
1414

1515
[dependencies]
1616
codec = { package = "parity-scale-codec", workspace = true, features = ["derive"] }
17-
darling = { workspace = true }
1817
frame-metadata = { workspace = true }
1918
heck = { workspace = true }
2019
proc-macro2 = { workspace = true }

codegen/src/types/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ mod type_def;
1111
mod type_def_params;
1212
mod type_path;
1313

14-
use darling::FromMeta;
1514
use proc_macro2::{Ident, Span, TokenStream};
1615
use quote::{quote, ToTokens};
1716
use scale_info::{form::PortableForm, PortableRegistry, Type, TypeDef};
@@ -353,7 +352,7 @@ impl ToTokens for CratePath {
353352

354353
impl From<&str> for CratePath {
355354
fn from(crate_path: &str) -> Self {
356-
Self(syn::Path::from_string(crate_path).unwrap_or_else(|err| {
355+
Self(syn::parse_str(crate_path).unwrap_or_else(|err| {
357356
panic!("failed converting {crate_path:?} to `syn::Path`: {err:?}");
358357
}))
359358
}

examples/examples/custom_type_derives.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
// mapping the type path to the derives which should be added for that type only.
1818
// Note that these derives will be in addition to those specified above in
1919
// `derive_for_all_types`
20-
derive_for_type(type = "frame_support::PalletId", derive = "Eq, Ord, PartialOrd"),
21-
derive_for_type(type = "sp_runtime::ModuleError", derive = "Eq, Hash"),
20+
derive_for_type(path = "frame_support::PalletId", derive = "Eq, Ord, PartialOrd"),
21+
derive_for_type(path = "sp_runtime::ModuleError", derive = "Eq, Hash"),
2222
)]
2323
pub mod polkadot {}
2424

macro/src/lib.rs

+20-14
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//! ```ignore
3232
//! #[subxt::subxt(
3333
//! runtime_metadata_path = "polkadot_metadata.scale",
34-
//! substitute_type(type = "sp_arithmetic::per_things::Perbill", with = "sp_runtime::Perbill")
34+
//! substitute_type(path = "sp_arithmetic::per_things::Perbill", with = "sp_runtime::Perbill")
3535
//! )]
3636
//! pub mod polkadot {}
3737
//! ```
@@ -67,8 +67,8 @@
6767
//! #[subxt::subxt(
6868
//! runtime_metadata_path = "polkadot_metadata.scale",
6969
//! derive_for_all_types = "Eq, PartialEq",
70-
//! derive_for_type(type = "frame_support::PalletId", derive = "Ord, PartialOrd"),
71-
//! derive_for_type(type = "sp_runtime::ModuleError", derive = "Hash"),
70+
//! derive_for_type(path = "frame_support::PalletId", derive = "Ord, PartialOrd"),
71+
//! derive_for_type(path = "sp_runtime::ModuleError", derive = "Hash"),
7272
//! )]
7373
//! pub mod polkadot {}
7474
//! ```
@@ -113,7 +113,7 @@ extern crate proc_macro;
113113

114114
use std::str::FromStr;
115115

116-
use darling::FromMeta;
116+
use darling::{ast::NestedMeta, FromMeta};
117117
use proc_macro::TokenStream;
118118
use proc_macro_error::{abort_call_site, proc_macro_error};
119119
use subxt_codegen::{utils::Uri, CodegenError, DerivesRegistry, TypeSubstitutes};
@@ -158,29 +158,31 @@ struct RuntimeMetadataArgs {
158158

159159
#[derive(Debug, FromMeta)]
160160
struct DeriveForType {
161-
#[darling(rename = "type")]
162-
ty: syn::TypePath,
161+
path: syn::TypePath,
163162
derive: Punctuated<syn::Path, syn::Token![,]>,
164163
}
165164

166165
#[derive(Debug, FromMeta)]
167166
struct AttributesForType {
168-
#[darling(rename = "type")]
169-
ty: syn::TypePath,
167+
path: syn::TypePath,
170168
attributes: Punctuated<OuterAttribute, syn::Token![,]>,
171169
}
172170

173171
#[derive(Debug, FromMeta)]
174172
struct SubstituteType {
175-
#[darling(rename = "type")]
176-
ty: syn::Path,
173+
path: syn::Path,
177174
with: syn::Path,
178175
}
179176

180177
#[proc_macro_attribute]
181178
#[proc_macro_error]
182179
pub fn subxt(args: TokenStream, input: TokenStream) -> TokenStream {
183-
let attr_args = parse_macro_input!(args as syn::AttributeArgs);
180+
let attr_args = match NestedMeta::parse_meta_list(args.into()) {
181+
Ok(v) => v,
182+
Err(e) => {
183+
return TokenStream::from(darling::Error::from(e).write_errors());
184+
}
185+
};
184186
let item_mod = parse_macro_input!(input as syn::ItemMod);
185187
let args = match RuntimeMetadataArgs::from_list(&attr_args) {
186188
Ok(v) => v,
@@ -205,11 +207,15 @@ pub fn subxt(args: TokenStream, input: TokenStream) -> TokenStream {
205207
);
206208

207209
for derives in &args.derive_for_type {
208-
derives_registry.extend_for_type(derives.ty.clone(), derives.derive.iter().cloned(), vec![])
210+
derives_registry.extend_for_type(
211+
derives.path.clone(),
212+
derives.derive.iter().cloned(),
213+
vec![],
214+
)
209215
}
210216
for attributes in &args.attributes_for_type {
211217
derives_registry.extend_for_type(
212-
attributes.ty.clone(),
218+
attributes.path.clone(),
213219
vec![],
214220
attributes.attributes.iter().map(|a| a.0.clone()),
215221
)
@@ -223,7 +229,7 @@ pub fn subxt(args: TokenStream, input: TokenStream) -> TokenStream {
223229
let substitute_args_res: Result<(), _> = args.substitute_type.into_iter().try_for_each(|sub| {
224230
sub.with
225231
.try_into()
226-
.and_then(|with| type_substitutes.insert(sub.ty, with))
232+
.and_then(|with| type_substitutes.insert(sub.path, with))
227233
});
228234

229235
if let Err(err) = substitute_args_res {

testing/ui-tests/src/correct/generic_params.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct DoesntImplEncodeDecodeAsType(u16);
2222
#[subxt::subxt(
2323
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
2424
substitute_type(
25-
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
25+
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
2626
// Discarding both params:
2727
with = "crate::CustomAddress"
2828
)
@@ -32,7 +32,7 @@ pub mod node_runtime {}
3232
#[subxt::subxt(
3333
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
3434
substitute_type(
35-
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
35+
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
3636
// Discarding second param:
3737
with = "crate::Generic<A>"
3838
)
@@ -42,7 +42,7 @@ pub mod node_runtime2 {}
4242
#[subxt::subxt(
4343
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
4444
substitute_type(
45-
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
45+
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
4646
// Discarding first param:
4747
with = "crate::Generic<B>"
4848
)
@@ -52,7 +52,7 @@ pub mod node_runtime3 {}
5252
#[subxt::subxt(
5353
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
5454
substitute_type(
55-
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
55+
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
5656
// Swapping params:
5757
with = "crate::Second<B, A>"
5858
)
@@ -62,7 +62,7 @@ pub mod node_runtime4 {}
6262
#[subxt::subxt(
6363
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
6464
substitute_type(
65-
type = "sp_runtime::multiaddress::MultiAddress",
65+
path = "sp_runtime::multiaddress::MultiAddress",
6666
// Ignore input params and just use concrete types on output:
6767
with = "crate::Second<bool, ::std::vec::Vec<u8>>"
6868
)
@@ -72,7 +72,7 @@ pub mod node_runtime5 {}
7272
#[subxt::subxt(
7373
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
7474
substitute_type(
75-
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
75+
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
7676
// We can put a static type in, too:
7777
with = "crate::Second<B, u16>"
7878
)
@@ -82,7 +82,7 @@ pub mod node_runtime6 {}
8282
#[subxt::subxt(
8383
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
8484
substitute_type(
85-
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
85+
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
8686
// Check that things can be wrapped in our Static type:
8787
with = "::subxt::utils::Static<crate::DoesntImplEncodeDecodeAsType>"
8888
)
@@ -92,7 +92,7 @@ pub mod node_runtime7 {}
9292
#[subxt::subxt(
9393
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
9494
substitute_type(
95-
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
95+
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
9696
// Recursive type param substitution should work too (swapping out nested A and B):
9797
with = "::subxt::utils::Static<crate::Second<A, B>>"
9898
)

testing/ui-tests/src/incorrect/substitute_path_not_absolute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[subxt::subxt(
22
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
33
substitute_type(
4-
type = "sp_arithmetic::per_things::Perbill",
4+
path = "sp_arithmetic::per_things::Perbill",
55
with = "sp_runtime::Perbill"
66
)
77
)]

0 commit comments

Comments
 (0)