From a2037bdedef0aa812017efee1d0fa63d4f5fcbae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Mon, 29 May 2023 11:26:38 +0100 Subject: [PATCH] sp-api: Set correct where bound in the generated code (#14252) The where bound for the `create_metadata` function wasn't correct. This pr fixes this by using the where bound declared at the type declaration augmented with the manual where bound. --- Cargo.lock | 1 + .../api/proc-macro/src/runtime_metadata.rs | 13 ++++-- primitives/api/test/Cargo.toml | 1 + .../ui/positive_cases/custom_where_bound.rs | 46 +++++++++++++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 primitives/api/test/tests/ui/positive_cases/custom_where_bound.rs diff --git a/Cargo.lock b/Cargo.lock index 7bffd9e169159..0f39783f6d4f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10632,6 +10632,7 @@ dependencies = [ "parity-scale-codec", "rustversion", "sc-block-builder", + "scale-info", "sp-api", "sp-consensus", "sp-core", diff --git a/primitives/api/proc-macro/src/runtime_metadata.rs b/primitives/api/proc-macro/src/runtime_metadata.rs index ae78fb52dbd5a..458248cbd3d16 100644 --- a/primitives/api/proc-macro/src/runtime_metadata.rs +++ b/primitives/api/proc-macro/src/runtime_metadata.rs @@ -162,15 +162,18 @@ pub fn generate_decl_runtime_metadata(decl: &ItemTrait) -> TokenStream2 { ty.default = None; } - let where_clause = where_clause - .iter() - .map(|ty| quote!(#ty: #crate_::scale_info::TypeInfo + 'static)); + where_clause + .into_iter() + .map(|ty| parse_quote!(#ty: #crate_::scale_info::TypeInfo + 'static)) + .for_each(|w| generics.make_where_clause().predicates.push(w)); + + let (impl_generics, _, where_clause) = generics.split_for_impl(); quote!( #( #attrs )* #[inline(always)] - pub fn runtime_metadata #generics () -> #crate_::metadata_ir::RuntimeApiMetadataIR - where #( #where_clause, )* + pub fn runtime_metadata #impl_generics () -> #crate_::metadata_ir::RuntimeApiMetadataIR + #where_clause { #crate_::metadata_ir::RuntimeApiMetadataIR { name: #trait_name, diff --git a/primitives/api/test/Cargo.toml b/primitives/api/test/Cargo.toml index 5b6c144ef3f6b..3ac4ad094e267 100644 --- a/primitives/api/test/Cargo.toml +++ b/primitives/api/test/Cargo.toml @@ -23,6 +23,7 @@ codec = { package = "parity-scale-codec", version = "3.2.2" } sp-state-machine = { version = "0.13.0", path = "../../state-machine" } trybuild = "1.0.74" rustversion = "1.0.6" +scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } [dev-dependencies] criterion = "0.4.0" diff --git a/primitives/api/test/tests/ui/positive_cases/custom_where_bound.rs b/primitives/api/test/tests/ui/positive_cases/custom_where_bound.rs new file mode 100644 index 0000000000000..22a176256ec6a --- /dev/null +++ b/primitives/api/test/tests/ui/positive_cases/custom_where_bound.rs @@ -0,0 +1,46 @@ +use codec::{Decode, Encode}; +use scale_info::TypeInfo; +use sp_runtime::traits::{Block as BlockT, GetNodeBlockType}; +use substrate_test_runtime_client::runtime::Block; + +struct Runtime {} +impl GetNodeBlockType for Runtime { + type NodeBlock = Block; +} + +pub trait CustomTrait: Encode + Decode + TypeInfo {} + +#[derive(Encode, Decode, TypeInfo)] +pub struct SomeImpl; +impl CustomTrait for SomeImpl {} + +#[derive(Encode, Decode, TypeInfo)] +pub struct SomeOtherType(C); + +sp_api::decl_runtime_apis! { + pub trait Api where A: CustomTrait { + fn test() -> A; + fn test2() -> SomeOtherType; + } +} + +sp_api::impl_runtime_apis! { + impl self::Api for Runtime { + fn test() -> SomeImpl { SomeImpl } + fn test2() -> SomeOtherType { SomeOtherType(SomeImpl) } + } + + impl sp_api::Core for Runtime { + fn version() -> sp_version::RuntimeVersion { + unimplemented!() + } + fn execute_block(_: Block) { + unimplemented!() + } + fn initialize_block(_: &::Header) { + unimplemented!() + } + } +} + +fn main() {}