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

sp-api: Set correct where bound in the generated code #14252

Merged
merged 1 commit into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

13 changes: 8 additions & 5 deletions primitives/api/proc-macro/src/runtime_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions primitives/api/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
46 changes: 46 additions & 0 deletions primitives/api/test/tests/ui/positive_cases/custom_where_bound.rs
Original file line number Diff line number Diff line change
@@ -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: CustomTrait>(C);

sp_api::decl_runtime_apis! {
pub trait Api<A> where A: CustomTrait {
fn test() -> A;
fn test2() -> SomeOtherType<A>;
}
}

sp_api::impl_runtime_apis! {
impl self::Api<Block, SomeImpl> for Runtime {
fn test() -> SomeImpl { SomeImpl }
fn test2() -> SomeOtherType<SomeImpl> { SomeOtherType(SomeImpl) }
}

impl sp_api::Core<Block> for Runtime {
fn version() -> sp_version::RuntimeVersion {
unimplemented!()
}
fn execute_block(_: Block) {
unimplemented!()
}
fn initialize_block(_: &<Block as BlockT>::Header) {
unimplemented!()
}
}
}

fn main() {}