Warn on missing pallet::call_index (#12894)

* Warn on missing call_index

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Suppress camel case warning

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Simplify code

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Disallow warnings in pallet-ui tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Add pallet UI test

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update Pallet UI

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* fmt

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Use module instead of function

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update pallet-ui

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This commit is contained in:
Oliver Tale-Yazdi
2022-12-15 11:38:21 +01:00
committed by GitHub
parent 619714243b
commit 9ac1bbe402
12 changed files with 115 additions and 27 deletions
@@ -54,6 +54,29 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
.map(|fn_name| format!("Create a call with the variant `{}`.", fn_name))
.collect::<Vec<_>>();
let mut warning_structs = Vec::new();
let mut warning_names = Vec::new();
// Emit a warning for each call that is missing `call_index` when not in dev-mode.
for method in &methods {
if method.explicit_call_index || def.dev_mode {
continue
}
let name = syn::Ident::new(&format!("{}", method.name), method.name.span());
let warning: syn::ItemStruct = syn::parse_quote!(
#[deprecated(note = r"
Implicit call indices are deprecated in favour of explicit ones.
Please ensure that all calls have the `pallet::call_index` attribute or that the
`dev-mode` of the pallet is enabled. For more info see:
<https://github.com/paritytech/substrate/pull/12891> and
<https://github.com/paritytech/substrate/pull/11381>.")]
#[allow(non_camel_case_types)]
struct #name;
);
warning_names.push(name);
warning_structs.push(warning);
}
let fn_weight = methods.iter().map(|method| &method.weight);
let fn_doc = methods.iter().map(|method| &method.docs).collect::<Vec<_>>();
@@ -178,6 +201,14 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
.collect::<Vec<_>>();
quote::quote_spanned!(span =>
mod warnings {
#(
#warning_structs
// This triggers each deprecated warning once.
const _: Option<#warning_names> = None;
)*
}
#[doc(hidden)]
pub mod __substrate_call_check {
#[macro_export]
@@ -59,6 +59,8 @@ pub struct CallVariantDef {
pub weight: syn::Expr,
/// Call index of the dispatchable.
pub call_index: u8,
/// Whether an explicit call index was specified.
pub explicit_call_index: bool,
/// Docs, used for metadata.
pub docs: Vec<syn::Lit>,
/// Attributes annotated at the top of the dispatchable function.
@@ -243,6 +245,7 @@ impl CallDef {
FunctionAttr::CallIndex(idx) => idx,
_ => unreachable!("checked during creation of the let binding"),
});
let explicit_call_index = call_index.is_some();
let final_index = match call_index {
Some(i) => i,
@@ -296,6 +299,7 @@ impl CallDef {
name: method.sig.ident.clone(),
weight,
call_index: final_index,
explicit_call_index,
args,
docs,
attrs: method.attrs.clone(),