From 7942e547cc6d6e4abd6f74dcfe16299527649213 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Mon, 21 Nov 2022 15:06:16 +0100 Subject: [PATCH 1/6] refactor: Simplify collecting type substitutes --- codegen/src/api/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/codegen/src/api/mod.rs b/codegen/src/api/mod.rs index df31e4ad02..1ccef80d28 100644 --- a/codegen/src/api/mod.rs +++ b/codegen/src/api/mod.rs @@ -204,9 +204,7 @@ impl RuntimeGenerator { }) .collect::>(); - for (path, substitute) in item_mod_ir.type_substitutes().iter() { - type_substitutes.insert(path.to_string(), substitute.clone()); - } + type_substitutes.extend(item_mod_ir.type_substitutes().into_iter()); let type_gen = TypeGenerator::new( &self.metadata.types, From d2af9841dd022a226ef82f3b3f3e62684f81dd93 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Mon, 21 Nov 2022 15:16:55 +0100 Subject: [PATCH 2/6] Simplify ItemMod::from --- codegen/src/ir.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/codegen/src/ir.rs b/codegen/src/ir.rs index a2b8f10a77..37603846b7 100644 --- a/codegen/src/ir.rs +++ b/codegen/src/ir.rs @@ -26,16 +26,13 @@ impl From for ItemMod { abort!(module, "out-of-line subxt modules are not supported",) } }; - let items = items - .into_iter() - .map(>::from) - .collect::>(); + Self { vis: module.vis, mod_token: module.mod_token, ident: module.ident, brace, - items, + items: items.into_iter().map(From::from).collect(), } } } From 97ed7108a30e000d7172e91f68a55d2962678a6c Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Mon, 21 Nov 2022 15:32:59 +0100 Subject: [PATCH 3/6] Preserve inner Rust items when expanding the subxt::subxt macro --- codegen/src/api/mod.rs | 7 ++++- codegen/src/ir.rs | 13 ++++++++ .../src/correct/rust-items-preserved.rs | 30 +++++++++++++++++++ testing/ui-tests/src/lib.rs | 1 + 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 testing/ui-tests/src/correct/rust-items-preserved.rs diff --git a/codegen/src/api/mod.rs b/codegen/src/api/mod.rs index 1ccef80d28..5c22ba6346 100644 --- a/codegen/src/api/mod.rs +++ b/codegen/src/api/mod.rs @@ -300,7 +300,7 @@ impl RuntimeGenerator { } }; - let mod_ident = item_mod_ir.ident; + let mod_ident = &item_mod_ir.ident; let pallets_with_constants: Vec<_> = pallets_with_mod_names .iter() .filter_map(|(pallet, pallet_mod_name)| { @@ -322,9 +322,14 @@ impl RuntimeGenerator { }) .collect(); + let rust_items: Vec<_> = item_mod_ir.rust_items().collect(); + quote! { #[allow(dead_code, unused_imports, non_camel_case_types)] pub mod #mod_ident { + // Preserve any Rust items that were previously defined in the adorned module + #( #rust_items ) * + // Make it easy to access the root via `root_mod` at different levels: use super::#mod_ident as root_mod; // Identify the pallets composing the static metadata by name. diff --git a/codegen/src/ir.rs b/codegen/src/ir.rs index 37603846b7..0fc8df6823 100644 --- a/codegen/src/ir.rs +++ b/codegen/src/ir.rs @@ -54,6 +54,10 @@ impl ItemMod { }) .collect() } + + pub fn rust_items<'a>(&'a self) -> impl Iterator { + self.items.iter().filter_map(Item::as_rust) + } } #[allow(clippy::large_enum_variant)] @@ -63,6 +67,15 @@ pub enum Item { Subxt(SubxtItem), } +impl Item { + pub fn as_rust(&self) -> Option<&syn::Item> { + match self { + Item::Rust(item) => Some(item), + _ => None, + } + } +} + impl From for Item { fn from(item: syn::Item) -> Self { if let syn::Item::Use(ref use_) = item { diff --git a/testing/ui-tests/src/correct/rust-items-preserved.rs b/testing/ui-tests/src/correct/rust-items-preserved.rs new file mode 100644 index 0000000000..99a31f4d20 --- /dev/null +++ b/testing/ui-tests/src/correct/rust-items-preserved.rs @@ -0,0 +1,30 @@ +#[subxt::subxt(runtime_metadata_path = "../../../artifacts/polkadot_metadata.scale")] +pub mod node_runtime { + pub struct SomeStruct; + pub enum SomeEnum { + A, + B, + } + pub trait SomeTrait { + fn some_func(&self) -> u32; + } + impl SomeTrait for SomeStruct { + fn some_func(&self) -> u32 { + 1 + } + } + impl SomeTrait for SomeEnum { + fn some_func(&self) -> u32 { + 2 + } + } +} + +fn main() { + use node_runtime::SomeTrait; + + let unit = node_runtime::SomeStruct; + assert_eq!(unit.some_func(), 1); + let enumeration = node_runtime::SomeEnum::A; + assert_eq!(enumeration.some_func(), 2); +} diff --git a/testing/ui-tests/src/lib.rs b/testing/ui-tests/src/lib.rs index fbcc0a9d10..aec57c8acf 100644 --- a/testing/ui-tests/src/lib.rs +++ b/testing/ui-tests/src/lib.rs @@ -50,4 +50,5 @@ fn ui_tests() { fn ui_fail() { let t = trybuild::TestCases::new(); t.compile_fail("src/incorrect/*.rs"); + t.pass("src/correct/*.rs"); } From fa49d6b86b67af67b42940f77443b8665f11521a Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Mon, 21 Nov 2022 15:47:22 +0100 Subject: [PATCH 4/6] No named lifetimes --- codegen/src/ir.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/src/ir.rs b/codegen/src/ir.rs index 0fc8df6823..6050bb7bf9 100644 --- a/codegen/src/ir.rs +++ b/codegen/src/ir.rs @@ -55,7 +55,7 @@ impl ItemMod { .collect() } - pub fn rust_items<'a>(&'a self) -> impl Iterator { + pub fn rust_items(&self) -> impl Iterator { self.items.iter().filter_map(Item::as_rust) } } From 13b9d7ed09b57fbaf47cc3d7b3d7c9d4e9dcb383 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Wed, 23 Nov 2022 11:09:36 +0100 Subject: [PATCH 5/6] Update codegen/src/api/mod.rs Co-authored-by: Andrew Jones --- codegen/src/api/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/src/api/mod.rs b/codegen/src/api/mod.rs index 5c22ba6346..a35f7ecd20 100644 --- a/codegen/src/api/mod.rs +++ b/codegen/src/api/mod.rs @@ -322,7 +322,7 @@ impl RuntimeGenerator { }) .collect(); - let rust_items: Vec<_> = item_mod_ir.rust_items().collect(); + let rust_items = item_mod_ir.rust_items(); quote! { #[allow(dead_code, unused_imports, non_camel_case_types)] From 81937b4136edcc2273e21eb76b8916f7671f7070 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Wed, 23 Nov 2022 11:13:48 +0100 Subject: [PATCH 6/6] Move passing UI tests under `ui_tests` --- testing/ui-tests/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/testing/ui-tests/src/lib.rs b/testing/ui-tests/src/lib.rs index aec57c8acf..8c3e30d30a 100644 --- a/testing/ui-tests/src/lib.rs +++ b/testing/ui-tests/src/lib.rs @@ -25,6 +25,8 @@ fn ui_tests() { let mut m = MetadataTestRunner::default(); let t = trybuild::TestCases::new(); + t.pass("src/correct/*.rs"); + // Check that storage maps with no keys are handled properly. t.pass(&m.path_to_ui_test_for_metadata( "storage_map_no_keys", @@ -50,5 +52,4 @@ fn ui_tests() { fn ui_fail() { let t = trybuild::TestCases::new(); t.compile_fail("src/incorrect/*.rs"); - t.pass("src/correct/*.rs"); }