Skip to content
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

Fixes cfg attributes in runtime macro #6410

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 12 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,17 @@ 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| {
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
Loading