referrerpolicy=no-referrer-when-downgrade

frame_support_procedural/pallet/expand/
inherent.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use crate::{pallet::Def, COUNTER};
19use proc_macro2::TokenStream;
20use quote::quote;
21use syn::{spanned::Spanned, Ident};
22
23pub fn expand_inherents(def: &mut Def) -> TokenStream {
24	let count = COUNTER.with(|counter| counter.borrow_mut().inc());
25	let macro_ident = Ident::new(&format!("__is_inherent_part_defined_{}", count), def.item.span());
26
27	let maybe_compile_error = if def.inherent.is_none() {
28		quote! {
29			compile_error!(concat!(
30				"`",
31				stringify!($pallet_name),
32				"` does not have #[pallet::inherent] defined, perhaps you should \
33				remove `Inherent` from construct_runtime?",
34			));
35		}
36	} else {
37		TokenStream::new()
38	};
39
40	quote! {
41		#[doc(hidden)]
42		pub mod __substrate_inherent_check {
43			#[macro_export]
44			#[doc(hidden)]
45			macro_rules! #macro_ident {
46				($pallet_name:ident) => {
47					#maybe_compile_error
48				}
49			}
50
51			#[doc(hidden)]
52			pub use #macro_ident as is_inherent_part_defined;
53		}
54	}
55}