Skip to content

Commit

Permalink
Fixes cfg attributes in runtime macro (#6410)
Browse files Browse the repository at this point in the history
Fixes #6209

This PR adds the support for cfg attributes in the runtime macro.

---------

Co-authored-by: Bastian Köcher <[email protected]>
  • Loading branch information
gupnik and bkchr authored Nov 25, 2024
1 parent 1e3b8e1 commit 6da7d36
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
15 changes: 13 additions & 2 deletions substrate/frame/support/procedural/src/runtime/parse/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
};
use frame_support_procedural_tools::get_doc_literals;
use quote::ToTokens;
use syn::{punctuated::Punctuated, token, Error};
use syn::{punctuated::Punctuated, spanned::Spanned, token, Error};

impl Pallet {
pub fn try_from(
Expand Down Expand Up @@ -78,7 +78,18 @@ impl Pallet {
})
.collect();

let cfg_pattern = vec![];
let cfg_pattern = item
.attrs
.iter()
.filter(|attr| attr.path().segments.first().map_or(false, |s| s.ident == "cfg"))
.map(|attr| {
attr.parse_args_with(|input: syn::parse::ParseStream| {
let input = input.parse::<proc_macro2::TokenStream>()?;
cfg_expr::Expression::parse(&input.to_string())
.map_err(|e| syn::Error::new(attr.span(), e.to_string()))
})
})
.collect::<syn::Result<Vec<_>>>()?;

let docs = get_doc_literals(&item.attrs);

Expand Down
59 changes: 46 additions & 13 deletions substrate/frame/support/test/tests/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,20 +799,43 @@ where
}
}

frame_support::construct_runtime!(
pub struct Runtime {
// Exclude part `Storage` in order not to check its metadata in tests.
System: frame_system exclude_parts { Pallet, Storage },
Example: pallet,
Example2: pallet2 exclude_parts { Call },
#[cfg(feature = "frame-feature-testing")]
Example3: pallet3,
Example4: pallet4 use_parts { Call },
#[frame_support::runtime]
mod runtime {
#[runtime::runtime]
#[runtime::derive(
RuntimeCall,
RuntimeEvent,
RuntimeError,
RuntimeOrigin,
RuntimeFreezeReason,
RuntimeHoldReason,
RuntimeSlashReason,
RuntimeLockId,
RuntimeTask
)]
pub struct Runtime;

#[cfg(feature = "frame-feature-testing-2")]
Example5: pallet5,
}
);
#[runtime::pallet_index(0)]
pub type System = frame_system + Call + Event<T>;

#[runtime::pallet_index(1)]
pub type Example = pallet;

#[runtime::pallet_index(2)]
#[runtime::disable_call]
pub type Example2 = pallet2;

#[cfg(feature = "frame-feature-testing")]
#[runtime::pallet_index(3)]
pub type Example3 = pallet3;

#[runtime::pallet_index(4)]
pub type Example4 = pallet4;

#[cfg(feature = "frame-feature-testing-2")]
#[runtime::pallet_index(5)]
pub type Example5 = pallet5;
}

// Test that the part `RuntimeCall` is excluded from Example2 and included in Example4.
fn _ensure_call_is_correctly_excluded_and_included(call: RuntimeCall) {
Expand Down Expand Up @@ -1847,6 +1870,16 @@ fn metadata() {
error: None,
docs: vec![" Test that the supertrait check works when we pass some parameter to the `frame_system::Config`."],
},
PalletMetadata {
index: 4,
name: "Example4",
storage: None,
calls: Some(meta_type::<pallet4::Call<Runtime>>().into()),
event: None,
constants: vec![],
error: None,
docs: vec![],
},
#[cfg(feature = "frame-feature-testing-2")]
PalletMetadata {
index: 5,
Expand Down

0 comments on commit 6da7d36

Please sign in to comment.