-
Notifications
You must be signed in to change notification settings - Fork 1k
sp-api
: Propagate deprecation information to metadata from impl_runtime_apis!
blocks
#6394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 7 commits
3f72c47
d94da40
d0ad06c
a7ae5ef
bd7948c
0879417
e031a86
735f201
c88f407
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 | ||
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json | ||
|
||
title: Propagate deprecation information from `impl_runtime_apis!` blocks | ||
|
||
doc: | ||
- audience: Runtime User, Runtime Dev | ||
description: | | ||
Prior to this PR, the metadata for runtime APIs would not propagate deprecation information | ||
from `#[deprecated]` attributes inside `impl_runtime_apis!` blocks. Now such information will be | ||
propagated into the metadata and will override deprecation information from `decl_runtime_apis!` if | ||
it was present. | ||
|
||
crates: | ||
- name: sp-api-proc-macro | ||
bump: none |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
|
||
use proc_macro2::TokenStream as TokenStream2; | ||
use quote::quote; | ||
use syn::{parse_quote, spanned::Spanned, ItemImpl, ItemTrait, Result}; | ||
use syn::{parse_quote, spanned::Spanned, ImplItem, ItemImpl, ItemTrait, Result}; | ||
|
||
use crate::utils::{ | ||
extract_api_version, extract_impl_trait, filter_cfg_attributes, generate_crate_access, | ||
|
@@ -131,12 +131,13 @@ pub fn generate_decl_runtime_metadata<'a>( | |
methods.push(quote!( | ||
#( #attrs )* | ||
if #version <= impl_version { | ||
let deprecation = if let Some(deprecation) = impl_deprecations.remove(&#method_name) { deprecation } else { #deprecation }; | ||
Some(#crate_::metadata_ir::RuntimeApiMethodMetadataIR { | ||
name: #method_name, | ||
inputs: #crate_::vec![ #( #inputs, )* ], | ||
output: #output, | ||
docs: #docs, | ||
deprecation_info: #deprecation, | ||
deprecation_info: deprecation, | ||
}) | ||
} else { | ||
None | ||
|
@@ -173,9 +174,13 @@ pub fn generate_decl_runtime_metadata<'a>( | |
#crate_::frame_metadata_enabled! { | ||
#( #attrs )* | ||
#[inline(always)] | ||
pub fn runtime_metadata #impl_generics (impl_version: u32) -> #crate_::metadata_ir::RuntimeApiMetadataIR | ||
pub fn runtime_metadata #impl_generics ( | ||
impl_version: u32, | ||
mut impl_deprecations: #crate_::scale_info::prelude::collections::BTreeMap<&str, #crate_::metadata_ir::ItemDeprecationInfoIR> | ||
pkhry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) -> #crate_::metadata_ir::RuntimeApiMetadataIR | ||
#where_clause | ||
{ | ||
let deprecation = if let Some(deprecation) = impl_deprecations.remove(&#trait_name) { deprecation } else { #deprecation }; | ||
pkhry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
#crate_::metadata_ir::RuntimeApiMetadataIR { | ||
name: #trait_name, | ||
methods: [ #( #methods, )* ] | ||
|
@@ -223,6 +228,24 @@ pub fn generate_impl_runtime_metadata(impls: &[ItemImpl]) -> Result<TokenStream2 | |
.expect("Trait path should always contain at least one item; qed") | ||
.ident; | ||
|
||
let mut deprecations = vec![]; | ||
|
||
if impl_.attrs.iter().any(|x| x.path().is_ident("deprecated")) { | ||
let deprecation = crate::utils::get_deprecation(&crate_, &impl_.attrs)?; | ||
deprecations.push(quote! {(stringify!(#trait_name_ident), #deprecation)}); | ||
pkhry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}; | ||
|
||
for method in &impl_.items { | ||
if let ImplItem::Fn(method) = method { | ||
if method.attrs.iter().any(|x| x.path().is_ident("deprecated")) { | ||
let name = &method.sig.ident; | ||
pkhry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
dbg!(name); | ||
pkhry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
let deprecation = crate::utils::get_deprecation(&crate_, &method.attrs)?; | ||
deprecations.push(quote! {(stringify!(#name), #deprecation)}); | ||
|
||
}; | ||
} | ||
} | ||
|
||
// Extract the generics from the trait to pass to the `runtime_metadata` | ||
// function on the hidden module. | ||
let generics = trait_ | ||
|
@@ -256,6 +279,10 @@ pub fn generate_impl_runtime_metadata(impls: &[ItemImpl]) -> Result<TokenStream2 | |
quote! { #trait_::VERSION } | ||
}; | ||
|
||
let map = quote! { | ||
#crate_::scale_info::prelude::collections::BTreeMap::from([ #( #deprecations, )*]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
}; | ||
|
||
// Handle the case where eg `#[cfg_attr(feature = "foo", api_version(4))]` is | ||
// present by using that version only when the feature is enabled and falling | ||
// back to the above version if not. | ||
|
@@ -264,14 +291,14 @@ pub fn generate_impl_runtime_metadata(impls: &[ItemImpl]) -> Result<TokenStream2 | |
let cfg_version = cfg_version.1; | ||
quote! {{ | ||
if cfg!(feature = #cfg_feature) { | ||
#trait_::runtime_metadata::#generics(#cfg_version) | ||
#trait_::runtime_metadata::#generics(#cfg_version, #map) | ||
} else { | ||
#trait_::runtime_metadata::#generics(#base_version) | ||
#trait_::runtime_metadata::#generics(#base_version, #map) | ||
} | ||
}} | ||
} else { | ||
quote! { | ||
#trait_::runtime_metadata::#generics(#base_version) | ||
#trait_::runtime_metadata::#generics(#base_version, #map) | ||
} | ||
} | ||
}; | ||
|
Uh oh!
There was an error while loading. Please reload this page.