Skip to content

Commit

Permalink
Upgrade to syn 2.0 (#875)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ascjones authored and tadeohepperle committed May 4, 2023
1 parent b937398 commit 2d83019
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 41 deletions.
56 changes: 45 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ criterion = "0.4"
codec = { package = "parity-scale-codec", version = "3.4.0", default-features = false }
color-eyre = "0.6.1"
console_error_panic_hook = "0.1.7"
darling = "0.14.4"
darling = "0.20.0"
derivative = "2.2.0"
either = "1.8.1"
frame-metadata = { version = "15.1.0", features = ["v14", "v15-unstable", "std"] }
Expand All @@ -61,7 +61,7 @@ scale-decode = "0.5.0"
scale-encode = "0.1.0"
serde = { version = "1.0.159" }
serde_json = { version = "1.0.96" }
syn = "1.0.109"
syn = { version = "2.0.15", features = ["full", "extra-traits"] }
thiserror = "1.0.40"
tokio = { version = "1.28", features = ["macros", "time", "rt-multi-thread"] }
tracing = "0.1.34"
Expand Down
1 change: 0 additions & 1 deletion codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ description = "Generate an API for interacting with a substrate node from FRAME

[dependencies]
codec = { package = "parity-scale-codec", workspace = true, features = ["derive"] }
darling = { workspace = true }
frame-metadata = { workspace = true }
heck = { workspace = true }
proc-macro2 = { workspace = true }
Expand Down
3 changes: 1 addition & 2 deletions codegen/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod type_def;
mod type_def_params;
mod type_path;

use darling::FromMeta;
use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, ToTokens};
use scale_info::{form::PortableForm, PortableRegistry, Type, TypeDef};
Expand Down Expand Up @@ -353,7 +352,7 @@ impl ToTokens for CratePath {

impl From<&str> for CratePath {
fn from(crate_path: &str) -> Self {
Self(syn::Path::from_string(crate_path).unwrap_or_else(|err| {
Self(syn::parse_str(crate_path).unwrap_or_else(|err| {
panic!("failed converting {crate_path:?} to `syn::Path`: {err:?}");
}))
}
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/custom_type_derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// mapping the type path to the derives which should be added for that type only.
// Note that these derives will be in addition to those specified above in
// `derive_for_all_types`
derive_for_type(type = "frame_support::PalletId", derive = "Eq, Ord, PartialOrd"),
derive_for_type(type = "sp_runtime::ModuleError", derive = "Eq, Hash"),
derive_for_type(path = "frame_support::PalletId", derive = "Eq, Ord, PartialOrd"),
derive_for_type(path = "sp_runtime::ModuleError", derive = "Eq, Hash"),
)]
pub mod polkadot {}

Expand Down
34 changes: 20 additions & 14 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//! ```ignore
//! #[subxt::subxt(
//! runtime_metadata_path = "polkadot_metadata.scale",
//! substitute_type(type = "sp_arithmetic::per_things::Perbill", with = "sp_runtime::Perbill")
//! substitute_type(path = "sp_arithmetic::per_things::Perbill", with = "sp_runtime::Perbill")
//! )]
//! pub mod polkadot {}
//! ```
Expand Down Expand Up @@ -67,8 +67,8 @@
//! #[subxt::subxt(
//! runtime_metadata_path = "polkadot_metadata.scale",
//! derive_for_all_types = "Eq, PartialEq",
//! derive_for_type(type = "frame_support::PalletId", derive = "Ord, PartialOrd"),
//! derive_for_type(type = "sp_runtime::ModuleError", derive = "Hash"),
//! derive_for_type(path = "frame_support::PalletId", derive = "Ord, PartialOrd"),
//! derive_for_type(path = "sp_runtime::ModuleError", derive = "Hash"),
//! )]
//! pub mod polkadot {}
//! ```
Expand Down Expand Up @@ -113,7 +113,7 @@ extern crate proc_macro;

use std::str::FromStr;

use darling::FromMeta;
use darling::{ast::NestedMeta, FromMeta};
use proc_macro::TokenStream;
use proc_macro_error::{abort_call_site, proc_macro_error};
use subxt_codegen::{utils::Uri, CodegenError, DerivesRegistry, TypeSubstitutes};
Expand Down Expand Up @@ -158,29 +158,31 @@ struct RuntimeMetadataArgs {

#[derive(Debug, FromMeta)]
struct DeriveForType {
#[darling(rename = "type")]
ty: syn::TypePath,
path: syn::TypePath,
derive: Punctuated<syn::Path, syn::Token![,]>,
}

#[derive(Debug, FromMeta)]
struct AttributesForType {
#[darling(rename = "type")]
ty: syn::TypePath,
path: syn::TypePath,
attributes: Punctuated<OuterAttribute, syn::Token![,]>,
}

#[derive(Debug, FromMeta)]
struct SubstituteType {
#[darling(rename = "type")]
ty: syn::Path,
path: syn::Path,
with: syn::Path,
}

#[proc_macro_attribute]
#[proc_macro_error]
pub fn subxt(args: TokenStream, input: TokenStream) -> TokenStream {
let attr_args = parse_macro_input!(args as syn::AttributeArgs);
let attr_args = match NestedMeta::parse_meta_list(args.into()) {
Ok(v) => v,
Err(e) => {
return TokenStream::from(darling::Error::from(e).write_errors());
}
};
let item_mod = parse_macro_input!(input as syn::ItemMod);
let args = match RuntimeMetadataArgs::from_list(&attr_args) {
Ok(v) => v,
Expand All @@ -205,11 +207,15 @@ pub fn subxt(args: TokenStream, input: TokenStream) -> TokenStream {
);

for derives in &args.derive_for_type {
derives_registry.extend_for_type(derives.ty.clone(), derives.derive.iter().cloned(), vec![])
derives_registry.extend_for_type(
derives.path.clone(),
derives.derive.iter().cloned(),
vec![],
)
}
for attributes in &args.attributes_for_type {
derives_registry.extend_for_type(
attributes.ty.clone(),
attributes.path.clone(),
vec![],
attributes.attributes.iter().map(|a| a.0.clone()),
)
Expand All @@ -223,7 +229,7 @@ pub fn subxt(args: TokenStream, input: TokenStream) -> TokenStream {
let substitute_args_res: Result<(), _> = args.substitute_type.into_iter().try_for_each(|sub| {
sub.with
.try_into()
.and_then(|with| type_substitutes.insert(sub.ty, with))
.and_then(|with| type_substitutes.insert(sub.path, with))
});

if let Err(err) = substitute_args_res {
Expand Down
16 changes: 8 additions & 8 deletions testing/ui-tests/src/correct/generic_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct DoesntImplEncodeDecodeAsType(u16);
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata_small.scale",
substitute_type(
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
// Discarding both params:
with = "crate::CustomAddress"
)
Expand All @@ -32,7 +32,7 @@ pub mod node_runtime {}
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata_small.scale",
substitute_type(
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
// Discarding second param:
with = "crate::Generic<A>"
)
Expand All @@ -42,7 +42,7 @@ pub mod node_runtime2 {}
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata_small.scale",
substitute_type(
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
// Discarding first param:
with = "crate::Generic<B>"
)
Expand All @@ -52,7 +52,7 @@ pub mod node_runtime3 {}
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata_small.scale",
substitute_type(
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
// Swapping params:
with = "crate::Second<B, A>"
)
Expand All @@ -62,7 +62,7 @@ pub mod node_runtime4 {}
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata_small.scale",
substitute_type(
type = "sp_runtime::multiaddress::MultiAddress",
path = "sp_runtime::multiaddress::MultiAddress",
// Ignore input params and just use concrete types on output:
with = "crate::Second<bool, ::std::vec::Vec<u8>>"
)
Expand All @@ -72,7 +72,7 @@ pub mod node_runtime5 {}
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata_small.scale",
substitute_type(
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
// We can put a static type in, too:
with = "crate::Second<B, u16>"
)
Expand All @@ -82,7 +82,7 @@ pub mod node_runtime6 {}
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata_small.scale",
substitute_type(
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
// Check that things can be wrapped in our Static type:
with = "::subxt::utils::Static<crate::DoesntImplEncodeDecodeAsType>"
)
Expand All @@ -92,7 +92,7 @@ pub mod node_runtime7 {}
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata_small.scale",
substitute_type(
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
path = "sp_runtime::multiaddress::MultiAddress<A, B>",
// Recursive type param substitution should work too (swapping out nested A and B):
with = "::subxt::utils::Static<crate::Second<A, B>>"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata_tiny.scale",
substitute_type(
type = "sp_arithmetic::per_things::Perbill",
path = "sp_arithmetic::per_things::Perbill",
with = "sp_runtime::Perbill"
)
)]
Expand Down

0 comments on commit 2d83019

Please sign in to comment.