Skip to content

Commit 6da7d36

Browse files
gupnikbkchr
andauthored
Fixes cfg attributes in runtime macro (#6410)
Fixes #6209 This PR adds the support for cfg attributes in the runtime macro. --------- Co-authored-by: Bastian Köcher <[email protected]>
1 parent 1e3b8e1 commit 6da7d36

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

substrate/frame/support/procedural/src/runtime/parse/pallet.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
};
2222
use frame_support_procedural_tools::get_doc_literals;
2323
use quote::ToTokens;
24-
use syn::{punctuated::Punctuated, token, Error};
24+
use syn::{punctuated::Punctuated, spanned::Spanned, token, Error};
2525

2626
impl Pallet {
2727
pub fn try_from(
@@ -78,7 +78,18 @@ impl Pallet {
7878
})
7979
.collect();
8080

81-
let cfg_pattern = vec![];
81+
let cfg_pattern = item
82+
.attrs
83+
.iter()
84+
.filter(|attr| attr.path().segments.first().map_or(false, |s| s.ident == "cfg"))
85+
.map(|attr| {
86+
attr.parse_args_with(|input: syn::parse::ParseStream| {
87+
let input = input.parse::<proc_macro2::TokenStream>()?;
88+
cfg_expr::Expression::parse(&input.to_string())
89+
.map_err(|e| syn::Error::new(attr.span(), e.to_string()))
90+
})
91+
})
92+
.collect::<syn::Result<Vec<_>>>()?;
8293

8394
let docs = get_doc_literals(&item.attrs);
8495

substrate/frame/support/test/tests/pallet.rs

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -799,20 +799,43 @@ where
799799
}
800800
}
801801

802-
frame_support::construct_runtime!(
803-
pub struct Runtime {
804-
// Exclude part `Storage` in order not to check its metadata in tests.
805-
System: frame_system exclude_parts { Pallet, Storage },
806-
Example: pallet,
807-
Example2: pallet2 exclude_parts { Call },
808-
#[cfg(feature = "frame-feature-testing")]
809-
Example3: pallet3,
810-
Example4: pallet4 use_parts { Call },
802+
#[frame_support::runtime]
803+
mod runtime {
804+
#[runtime::runtime]
805+
#[runtime::derive(
806+
RuntimeCall,
807+
RuntimeEvent,
808+
RuntimeError,
809+
RuntimeOrigin,
810+
RuntimeFreezeReason,
811+
RuntimeHoldReason,
812+
RuntimeSlashReason,
813+
RuntimeLockId,
814+
RuntimeTask
815+
)]
816+
pub struct Runtime;
811817

812-
#[cfg(feature = "frame-feature-testing-2")]
813-
Example5: pallet5,
814-
}
815-
);
818+
#[runtime::pallet_index(0)]
819+
pub type System = frame_system + Call + Event<T>;
820+
821+
#[runtime::pallet_index(1)]
822+
pub type Example = pallet;
823+
824+
#[runtime::pallet_index(2)]
825+
#[runtime::disable_call]
826+
pub type Example2 = pallet2;
827+
828+
#[cfg(feature = "frame-feature-testing")]
829+
#[runtime::pallet_index(3)]
830+
pub type Example3 = pallet3;
831+
832+
#[runtime::pallet_index(4)]
833+
pub type Example4 = pallet4;
834+
835+
#[cfg(feature = "frame-feature-testing-2")]
836+
#[runtime::pallet_index(5)]
837+
pub type Example5 = pallet5;
838+
}
816839

817840
// Test that the part `RuntimeCall` is excluded from Example2 and included in Example4.
818841
fn _ensure_call_is_correctly_excluded_and_included(call: RuntimeCall) {
@@ -1847,6 +1870,16 @@ fn metadata() {
18471870
error: None,
18481871
docs: vec![" Test that the supertrait check works when we pass some parameter to the `frame_system::Config`."],
18491872
},
1873+
PalletMetadata {
1874+
index: 4,
1875+
name: "Example4",
1876+
storage: None,
1877+
calls: Some(meta_type::<pallet4::Call<Runtime>>().into()),
1878+
event: None,
1879+
constants: vec![],
1880+
error: None,
1881+
docs: vec![],
1882+
},
18501883
#[cfg(feature = "frame-feature-testing-2")]
18511884
PalletMetadata {
18521885
index: 5,

0 commit comments

Comments
 (0)