diff --git a/substrate/frame/support/procedural/src/construct_runtime/mod.rs b/substrate/frame/support/procedural/src/construct_runtime/mod.rs index fc799c923b..4644c217cf 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/mod.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/mod.rs @@ -62,6 +62,7 @@ impl Module { fn complete_modules(decl: impl Iterator) -> syn::Result> { let mut indices = HashMap::new(); let mut last_index: Option = None; + let mut names = HashMap::new(); decl .map(|module| { @@ -88,6 +89,14 @@ fn complete_modules(decl: impl Iterator) -> syn::Resul return Err(err); } + if let Some(used_module) = names.insert(module.name.clone(), module.name.span()) { + let msg = "Two modules with the same name!"; + + let mut err = syn::Error::new(used_module, &msg); + err.combine(syn::Error::new(module.name.span(), &msg)); + return Err(err); + } + Ok(Module { name: module.name, index: final_index, diff --git a/substrate/frame/support/test/tests/construct_runtime_ui/conflicting_module_name.rs b/substrate/frame/support/test/tests/construct_runtime_ui/conflicting_module_name.rs new file mode 100644 index 0000000000..bc242a57a4 --- /dev/null +++ b/substrate/frame/support/test/tests/construct_runtime_ui/conflicting_module_name.rs @@ -0,0 +1,15 @@ +use frame_support::construct_runtime; + +construct_runtime! { + pub enum Runtime where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic + { + System: system::{Module}, + Balance: balances::{Module}, + Balance: balances::{Module}, + } +} + +fn main() {} diff --git a/substrate/frame/support/test/tests/construct_runtime_ui/conflicting_module_name.stderr b/substrate/frame/support/test/tests/construct_runtime_ui/conflicting_module_name.stderr new file mode 100644 index 0000000000..f5b999db66 --- /dev/null +++ b/substrate/frame/support/test/tests/construct_runtime_ui/conflicting_module_name.stderr @@ -0,0 +1,11 @@ +error: Two modules with the same name! + --> $DIR/conflicting_module_name.rs:10:3 + | +10 | Balance: balances::{Module}, + | ^^^^^^^ + +error: Two modules with the same name! + --> $DIR/conflicting_module_name.rs:11:3 + | +11 | Balance: balances::{Module}, + | ^^^^^^^