Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit a2037bd

Browse files
committed
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.
1 parent 7c195bf commit a2037bd

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

primitives/api/proc-macro/src/runtime_metadata.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,18 @@ pub fn generate_decl_runtime_metadata(decl: &ItemTrait) -> TokenStream2 {
162162
ty.default = None;
163163
}
164164

165-
let where_clause = where_clause
166-
.iter()
167-
.map(|ty| quote!(#ty: #crate_::scale_info::TypeInfo + 'static));
165+
where_clause
166+
.into_iter()
167+
.map(|ty| parse_quote!(#ty: #crate_::scale_info::TypeInfo + 'static))
168+
.for_each(|w| generics.make_where_clause().predicates.push(w));
169+
170+
let (impl_generics, _, where_clause) = generics.split_for_impl();
168171

169172
quote!(
170173
#( #attrs )*
171174
#[inline(always)]
172-
pub fn runtime_metadata #generics () -> #crate_::metadata_ir::RuntimeApiMetadataIR
173-
where #( #where_clause, )*
175+
pub fn runtime_metadata #impl_generics () -> #crate_::metadata_ir::RuntimeApiMetadataIR
176+
#where_clause
174177
{
175178
#crate_::metadata_ir::RuntimeApiMetadataIR {
176179
name: #trait_name,

primitives/api/test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ codec = { package = "parity-scale-codec", version = "3.2.2" }
2323
sp-state-machine = { version = "0.13.0", path = "../../state-machine" }
2424
trybuild = "1.0.74"
2525
rustversion = "1.0.6"
26+
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
2627

2728
[dev-dependencies]
2829
criterion = "0.4.0"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use codec::{Decode, Encode};
2+
use scale_info::TypeInfo;
3+
use sp_runtime::traits::{Block as BlockT, GetNodeBlockType};
4+
use substrate_test_runtime_client::runtime::Block;
5+
6+
struct Runtime {}
7+
impl GetNodeBlockType for Runtime {
8+
type NodeBlock = Block;
9+
}
10+
11+
pub trait CustomTrait: Encode + Decode + TypeInfo {}
12+
13+
#[derive(Encode, Decode, TypeInfo)]
14+
pub struct SomeImpl;
15+
impl CustomTrait for SomeImpl {}
16+
17+
#[derive(Encode, Decode, TypeInfo)]
18+
pub struct SomeOtherType<C: CustomTrait>(C);
19+
20+
sp_api::decl_runtime_apis! {
21+
pub trait Api<A> where A: CustomTrait {
22+
fn test() -> A;
23+
fn test2() -> SomeOtherType<A>;
24+
}
25+
}
26+
27+
sp_api::impl_runtime_apis! {
28+
impl self::Api<Block, SomeImpl> for Runtime {
29+
fn test() -> SomeImpl { SomeImpl }
30+
fn test2() -> SomeOtherType<SomeImpl> { SomeOtherType(SomeImpl) }
31+
}
32+
33+
impl sp_api::Core<Block> for Runtime {
34+
fn version() -> sp_version::RuntimeVersion {
35+
unimplemented!()
36+
}
37+
fn execute_block(_: Block) {
38+
unimplemented!()
39+
}
40+
fn initialize_block(_: &<Block as BlockT>::Header) {
41+
unimplemented!()
42+
}
43+
}
44+
}
45+
46+
fn main() {}

0 commit comments

Comments
 (0)