diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 106be37cdc..e0c683f24c 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -3699,6 +3699,7 @@ dependencies = [ "srml-support 2.0.0", "substrate-inherents 2.0.0", "substrate-primitives 2.0.0", + "trybuild 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/substrate/srml/support/src/dispatch.rs b/substrate/srml/support/src/dispatch.rs index d5fabd341c..aa6282c773 100644 --- a/substrate/srml/support/src/dispatch.rs +++ b/substrate/srml/support/src/dispatch.rs @@ -874,6 +874,10 @@ macro_rules! decl_module { { $( $on_finalize:tt )* } { $( $offchain:tt )* } ) => { + $crate::__check_reserved_fn_name! { + $($fn_name)* + } + // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. #[derive(Clone, Copy, PartialEq, Eq)] #[cfg_attr(feature = "std", derive(Debug))] @@ -1201,6 +1205,38 @@ macro_rules! __function_to_metadata { } } +#[macro_export] +#[doc(hidden)] +macro_rules! __check_reserved_fn_name { + (deposit_event $( $rest:ident )*) => { + $crate::__check_reserved_fn_name!(@compile_error deposit_event); + }; + (on_initialize $( $rest:ident )*) => { + $crate::__check_reserved_fn_name!(@compile_error on_initialize); + }; + (on_initialise $( $rest:ident )*) => { + $crate::__check_reserved_fn_name!(@compile_error on_initialise); + }; + (on_finalize $( $rest:ident )*) => { + $crate::__check_reserved_fn_name!(@compile_error on_finalize); + }; + (on_finalise $( $rest:ident )*) => { + $crate::__check_reserved_fn_name!(@compile_error on_finalise); + }; + (offchain_worker $( $rest:ident )*) => { + $crate::__check_reserved_fn_name!(@compile_error offchain_worker); + }; + ($t:ident $( $rest:ident )*) => { + $crate::__check_reserved_fn_name!($( $rest )*); + }; + () => {}; + (@compile_error $ident:ident) => { + compile_error!(concat!("Invalid call fn name: `", stringify!($ident), + "`, name is reserved and doesn't match expected signature, please refer to `decl_module!`", + " documentation to see the appropriate usage, or rename it to an unreserved keyword.")); + }; +} + #[cfg(test)] // Do not complain about unused `dispatch` and `dispatch_aux`. #[allow(dead_code)] diff --git a/substrate/srml/support/src/lib.rs b/substrate/srml/support/src/lib.rs index cdaec09632..4f749d4c89 100644 --- a/substrate/srml/support/src/lib.rs +++ b/substrate/srml/support/src/lib.rs @@ -381,7 +381,7 @@ mod tests { let key2 = 18u32; DoubleMap::insert(key1, key2, vec![1]); - DoubleMap::append(key1, key2, &[2, 3]); + DoubleMap::append(key1, key2, &[2, 3]).unwrap(); assert_eq!(DoubleMap::get(key1, key2), vec![1, 2, 3]); }); } diff --git a/substrate/srml/support/src/unsigned.rs b/substrate/srml/support/src/unsigned.rs index dcdf4b2683..8ea613461a 100644 --- a/substrate/srml/support/src/unsigned.rs +++ b/substrate/srml/support/src/unsigned.rs @@ -84,6 +84,7 @@ mod test_empty_call { pub enum Call { } + #[allow(unused)] pub struct Runtime; impl_outer_validate_unsigned! { diff --git a/substrate/srml/support/test/Cargo.toml b/substrate/srml/support/test/Cargo.toml index 29cc927ff7..44a3b8d884 100644 --- a/substrate/srml/support/test/Cargo.toml +++ b/substrate/srml/support/test/Cargo.toml @@ -4,13 +4,14 @@ version = "2.0.0" authors = ["Parity Technologies "] edition = "2018" -[dev-dependencies] +[dependencies] serde = { version = "1.0", default-features = false, features = ["derive"] } parity-codec = { version = "3.3", default-features = false, features = ["derive"] } runtime_io = { package = "sr-io", path = "../../../core/sr-io", default-features = false } -srml-support = { path = "../", default-features = false } +srml-support = { version = "2", path = "../", default-features = false } inherents = { package = "substrate-inherents", path = "../../../core/inherents", default-features = false } primitives = { package = "substrate-primitives", path = "../../../core/primitives", default-features = false } +trybuild = "1" [features] default = ["std"] diff --git a/substrate/srml/support/test/src/lib.rs b/substrate/srml/support/test/src/lib.rs index 7b23662e68..a7a869cf87 100644 --- a/substrate/srml/support/test/src/lib.rs +++ b/substrate/srml/support/test/src/lib.rs @@ -16,3 +16,9 @@ //! Test crate for srml_support. Allow to make use of `srml_support::decl_storage`. //! See tests directory. + +#[test] +fn reserved_keyword() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/reserved_keyword/*.rs"); +} diff --git a/substrate/srml/support/test/tests/reserved_keyword/on_initialize.rs b/substrate/srml/support/test/tests/reserved_keyword/on_initialize.rs new file mode 100644 index 0000000000..c63153241c --- /dev/null +++ b/substrate/srml/support/test/tests/reserved_keyword/on_initialize.rs @@ -0,0 +1,33 @@ +macro_rules! reserved { + ($($reserved:ident)*) => { + $( + mod $reserved { + pub use srml_support::dispatch::Result; + + pub trait Trait { + type Origin; + type BlockNumber: Into; + } + + pub mod system { + use srml_support::dispatch::Result; + + pub fn ensure_root(_: R) -> Result { + Ok(()) + } + } + + srml_support::decl_module! { + pub struct Module for enum Call where origin: T::Origin { + fn $reserved() -> Result { unreachable!() } + } + } + } + )* + } +} + +reserved!(on_finalize on_initialize on_finalise on_initialise offchain_worker deposit_event); + +fn main() { +} diff --git a/substrate/srml/support/test/tests/reserved_keyword/on_initialize.stderr b/substrate/srml/support/test/tests/reserved_keyword/on_initialize.stderr new file mode 100644 index 0000000000..7a37eb66c3 --- /dev/null +++ b/substrate/srml/support/test/tests/reserved_keyword/on_initialize.stderr @@ -0,0 +1,47 @@ +error: Invalid call fn name: `on_finalize`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword. + --> $DIR/on_initialize.rs:30:1 + | +30 | reserved!(on_finalize on_initialize on_finalise on_initialise offchain_worker deposit_event); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation + | + = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) + +error: Invalid call fn name: `on_initialize`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword. + --> $DIR/on_initialize.rs:30:1 + | +30 | reserved!(on_finalize on_initialize on_finalise on_initialise offchain_worker deposit_event); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation + | + = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) + +error: Invalid call fn name: `on_finalise`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword. + --> $DIR/on_initialize.rs:30:1 + | +30 | reserved!(on_finalize on_initialize on_finalise on_initialise offchain_worker deposit_event); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation + | + = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) + +error: Invalid call fn name: `on_initialise`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword. + --> $DIR/on_initialize.rs:30:1 + | +30 | reserved!(on_finalize on_initialize on_finalise on_initialise offchain_worker deposit_event); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation + | + = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) + +error: Invalid call fn name: `offchain_worker`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword. + --> $DIR/on_initialize.rs:30:1 + | +30 | reserved!(on_finalize on_initialize on_finalise on_initialise offchain_worker deposit_event); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation + | + = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) + +error: Invalid call fn name: `deposit_event`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword. + --> $DIR/on_initialize.rs:30:1 + | +30 | reserved!(on_finalize on_initialize on_finalise on_initialise offchain_worker deposit_event); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation + | + = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)