diff --git a/substrate/.gitignore b/substrate/.gitignore index d9ba8ac2b4..28885a4830 100644 --- a/substrate/.gitignore +++ b/substrate/.gitignore @@ -20,3 +20,4 @@ nohup.out rls*.log *.orig *.rej +**/wip/*.stderr diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 3658699187..17bf92b259 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -1024,6 +1024,11 @@ name = "glob" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "glob" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "globset" version = "0.4.3" @@ -3269,6 +3274,7 @@ dependencies = [ "substrate-state-machine 2.0.0", "substrate-test-client 2.0.0", "syn 0.15.32 (registry+https://github.com/rust-lang/crates.io-index)", + "trybuild 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -5010,6 +5016,14 @@ dependencies = [ "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "toml" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "traitobject" version = "0.1.0" @@ -5065,6 +5079,19 @@ name = "try-lock" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "trybuild" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "twofish" version = "0.2.0" @@ -5631,6 +5658,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum get_if_addrs 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "abddb55a898d32925f3148bd281174a68eeb68bbfd9a5938a57b18f506ee4ef7" "checksum get_if_addrs-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d04f9fb746cf36b191c00f3ede8bde9c8e64f9f4b05ae2694a9ccf5e3f5ab48" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" +"checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" "checksum globset 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ef4feaabe24a0a658fd9cf4a9acf6ed284f045c77df0f49020ba3245cfb7b454" "checksum h2 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "85ab6286db06040ddefb71641b50017c06874614001a134b423783e2db2920bd" "checksum hash-db 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ba7fb417e5c470acdd61068c79767d0e65962e70836cf6c9dfd2409f06345ce0" @@ -5888,12 +5916,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "66268575b80f4a4a710ef83d087fdfeeabdce9b74c797535fbac18a2cb906e92" "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" +"checksum toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "87c5890a989fa47ecdc7bcb4c63a77a82c18f306714104b1decfd722db17b39e" "checksum traitobject 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079" "checksum trie-bench 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ba20f7d9865497ea46511860b43e05a44f4ac9a76ee089d34cd80a839a690264" "checksum trie-db 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1ba73747fd3a64ab531274c04cb588dfa9d30d972d62990831e63fbce2cfec59" "checksum trie-root 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)" = "cfa2e20c4f1418ac2e71ddc418e35e1b56e34022e2146209ffdbf1b2de8b1bd9" "checksum trie-standardmap 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e4e24277af05f38f3aaf03ac78e3a154be83f13db9c8ef0cb95bb1aa764a477b" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" +"checksum trybuild 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0b0df728de48978b759da185ed7ef76676ef0c878ae4800c9e90024c998dc75b" "checksum twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712d261e83e727c8e2dbb75dacac67c36e35db36a958ee504f2164fc052434e1" "checksum twox-hash 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "09871da9f15424236082e0b220fd404a4eb6bebc7205c67653701229234ac64c" "checksum typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887" diff --git a/substrate/core/sr-api-macros/Cargo.toml b/substrate/core/sr-api-macros/Cargo.toml index 5aace4b62a..a87b53b732 100644 --- a/substrate/core/sr-api-macros/Cargo.toml +++ b/substrate/core/sr-api-macros/Cargo.toml @@ -24,6 +24,7 @@ substrate-primitives = { path = "../primitives" } criterion = "0.2" consensus_common = { package = "substrate-consensus-common", path = "../consensus/common" } codec = { package = "parity-codec", version = "3.5.1" } +trybuild = "1.0" [[bench]] name = "bench" diff --git a/substrate/core/sr-api-macros/src/compile_fail_tests.rs b/substrate/core/sr-api-macros/src/compile_fail_tests.rs deleted file mode 100644 index e562f8b2fe..0000000000 --- a/substrate/core/sr-api-macros/src/compile_fail_tests.rs +++ /dev/null @@ -1,412 +0,0 @@ -// Copyright 2018-2019 Parity Technologies (UK) Ltd. -// This file is part of Substrate. - -// Substrate is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Substrate is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Substrate. If not, see . - -//! Compile fail tests. - -mod declaring_own_block { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate sr_primitives as runtime_primitives; - - use runtime_primitives::traits::Block as BlockT; - - decl_runtime_apis! { - pub trait Api { - fn test(); - } - } - - fn main() {} - ``` - */ -} - -mod declaring_own_block_with_different_name { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate sr_primitives as runtime_primitives; - - use runtime_primitives::traits::Block as BlockT; - - decl_runtime_apis! { - pub trait Api { - fn test(); - } - } - - fn main() {} - ``` - */ -} - -mod adding_self_parameter { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate sr_primitives as runtime_primitives; - - decl_runtime_apis! { - pub trait Api { - fn test(&self); - } - } - - fn main() {} - ``` - */ -} - -mod adding_at_parameter { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate sr_primitives as runtime_primitives; - - decl_runtime_apis! { - pub trait Api { - fn test(at: u64); - } - } - - fn main() {} - ``` - */ -} - -mod invalid_api_version { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate sr_primitives as runtime_primitives; - - decl_runtime_apis! { - #[api_version] - pub trait Api { - fn test(data: u64); - } - } - - fn main() {} - ``` - */ -} - -mod invalid_api_version_2 { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate sr_primitives as runtime_primitives; - - decl_runtime_apis! { - #[api_version("1")] - pub trait Api { - fn test(data: u64); - } - } - - fn main() {} - ``` - */ -} - -mod invalid_api_version_3 { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate sr_primitives as runtime_primitives; - - decl_runtime_apis! { - #[api_version()] - pub trait Api { - fn test(data: u64); - } - } - - fn main() {} - ``` - */ -} - -mod missing_block_generic_parameter { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate substrate_test_client as test_client; - extern crate sr_primitives as runtime_primitives; - extern crate substrate_primitives as primitives; - - use runtime_primitives::traits::GetNodeBlockType; - use test_client::runtime::Block; - - /// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` - /// trait are done by the `construct_runtime!` macro in a real runtime. - struct Runtime {} - impl GetNodeBlockType for Runtime { - type NodeBlock = Block; - } - - decl_runtime_apis! { - pub trait Api { - fn test(data: u64); - } - } - - impl_runtime_apis! { - impl self::Api for Runtime { - fn test(data: u64) { - unimplemented!() - } - } - } - - fn main() {} - ``` - */ -} - -mod missing_path_for_trait { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate substrate_test_client as test_client; - extern crate sr_primitives as runtime_primitives; - extern crate substrate_primitives as primitives; - - use runtime_primitives::traits::GetNodeBlockType; - use test_client::runtime::Block; - - /// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` - /// trait are done by the `construct_runtime!` macro in a real runtime. - struct Runtime {} - impl GetNodeBlockType for Runtime { - type NodeBlock = Block; - } - - decl_runtime_apis! { - pub trait Api { - fn test(data: u64); - } - } - - impl_runtime_apis! { - impl Api for Runtime { - fn test(data: u64) { - unimplemented!() - } - } - } - - fn main() {} - ``` - */ -} - -mod empty_impl_runtime_apis_call { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate substrate_test_client as test_client; - extern crate sr_primitives as runtime_primitives; - extern crate substrate_primitives as primitives; - - use runtime_primitives::traits::GetNodeBlockType; - use test_client::runtime::Block; - - /// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` - /// trait are done by the `construct_runtime!` macro in a real runtime. - struct Runtime {} - impl GetNodeBlockType for Runtime { - type NodeBlock = Block; - } - - decl_runtime_apis! { - pub trait Api { - fn test(data: u64); - } - } - - impl_runtime_apis! {} - - fn main() {} - ``` - */ -} - -mod type_reference_in_impl_runtime_apis_call { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate substrate_test_client as test_client; - extern crate sr_primitives as runtime_primitives; - extern crate substrate_primitives as primitives; - - use runtime_primitives::traits::GetNodeBlockType; - use test_client::runtime::Block; - - /// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` - /// trait are done by the `construct_runtime!` macro in a real runtime. - struct Runtime {} - impl GetNodeBlockType for Runtime { - type NodeBlock = Block; - } - - decl_runtime_apis! { - pub trait Api { - fn test(data: u64); - } - } - - impl_runtime_apis! { - impl self::Api for Runtime { - fn test(data: &u64) { - unimplemented!() - } - } - } - - fn main() {} - ``` - */ -} - -mod impl_incorrect_method_signature { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate substrate_test_client as test_client; - extern crate sr_primitives as runtime_primitives; - extern crate substrate_primitives as primitives; - - use runtime_primitives::traits::GetNodeBlockType; - use test_client::runtime::Block; - - /// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` - /// trait are done by the `construct_runtime!` macro in a real runtime. - struct Runtime {} - impl GetNodeBlockType for Runtime { - type NodeBlock = Block; - } - - decl_runtime_apis! { - pub trait Api { - fn test(data: u64); - } - } - - impl_runtime_apis! { - impl self::Api for Runtime { - fn test(data: String) {} - } - } - - fn main() {} - ``` - */ -} - -mod impl_two_traits_with_same_name { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate substrate_test_client as test_client; - extern crate sr_primitives as runtime_primitives; - extern crate substrate_primitives as primitives; - - use runtime_primitives::traits::GetNodeBlockType; - use test_client::runtime::Block; - - /// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` - /// trait are done by the `construct_runtime!` macro in a real runtime. - struct Runtime {} - impl GetNodeBlockType for Runtime { - type NodeBlock = Block; - } - - decl_runtime_apis! { - pub trait Api { - fn test(data: u64); - } - } - - mod second { - decl_runtime_apis! { - pub trait Api { - fn test2(data: u64); - } - } - } - - impl_runtime_apis! { - impl self::Api for Runtime { - fn test(data: u64) {} - } - - impl second::Api for Runtime { - fn test2(data: u64) {} - } - } - - fn main() {} - ``` - */ -} - -mod changed_at_unknown_version { - /*! - ```compile_fail - #[macro_use] - extern crate client; - extern crate substrate_test_client as test_client; - extern crate sr_primitives as runtime_primitives; - extern crate substrate_primitives as primitives; - - use runtime_primitives::traits::GetNodeBlockType; - use test_client::runtime::Block; - - /// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` - /// trait are done by the `construct_runtime!` macro in a real runtime. - struct Runtime {} - impl GetNodeBlockType for Runtime { - type NodeBlock = Block; - } - - decl_runtime_apis! { - pub trait Api { - #[changed_in(2)] - fn test(data: u64); - fn test(data: u64); - } - } - - fn main() {} - ``` - */ -} diff --git a/substrate/core/sr-api-macros/src/lib.rs b/substrate/core/sr-api-macros/src/lib.rs index 72e143eb1a..c5f8941002 100644 --- a/substrate/core/sr-api-macros/src/lib.rs +++ b/substrate/core/sr-api-macros/src/lib.rs @@ -24,7 +24,6 @@ use proc_macro::TokenStream; mod impl_runtime_apis; mod decl_runtime_apis; mod utils; -mod compile_fail_tests; /// Tags given trait implementations as runtime apis. /// diff --git a/substrate/core/sr-api-macros/tests/trybuild.rs b/substrate/core/sr-api-macros/tests/trybuild.rs new file mode 100644 index 0000000000..e04c67737a --- /dev/null +++ b/substrate/core/sr-api-macros/tests/trybuild.rs @@ -0,0 +1,5 @@ +#[test] +fn ui() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/ui/*.rs"); +} diff --git a/substrate/core/sr-api-macros/tests/ui/adding_at_parameter.rs b/substrate/core/sr-api-macros/tests/ui/adding_at_parameter.rs new file mode 100644 index 0000000000..d4757e256f --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/adding_at_parameter.rs @@ -0,0 +1,9 @@ +use client::decl_runtime_apis; + +decl_runtime_apis! { + pub trait Api { + fn test(at: u64); + } +} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/adding_at_parameter.stderr b/substrate/core/sr-api-macros/tests/ui/adding_at_parameter.stderr new file mode 100644 index 0000000000..1c7e07a418 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/adding_at_parameter.stderr @@ -0,0 +1,5 @@ +error: `decl_runtime_apis!` adds automatically a parameter `at: &BlockId`. Please rename/remove your parameter. + --> $DIR/adding_at_parameter.rs:5:11 + | +5 | fn test(at: u64); + | ^^ diff --git a/substrate/core/sr-api-macros/tests/ui/adding_self_parameter.rs b/substrate/core/sr-api-macros/tests/ui/adding_self_parameter.rs new file mode 100644 index 0000000000..fb048211ad --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/adding_self_parameter.rs @@ -0,0 +1,9 @@ +use client::decl_runtime_apis; + +decl_runtime_apis! { + pub trait Api { + fn test(&self); + } +} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/adding_self_parameter.stderr b/substrate/core/sr-api-macros/tests/ui/adding_self_parameter.stderr new file mode 100644 index 0000000000..e7249e9f73 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/adding_self_parameter.stderr @@ -0,0 +1,5 @@ +error: Self values are not supported. + --> $DIR/adding_self_parameter.rs:5:11 + | +5 | fn test(&self); + | ^ diff --git a/substrate/core/sr-api-macros/tests/ui/changed_in_unknown_version.rs b/substrate/core/sr-api-macros/tests/ui/changed_in_unknown_version.rs new file mode 100644 index 0000000000..27aa60c624 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/changed_in_unknown_version.rs @@ -0,0 +1,20 @@ +use runtime_primitives::traits::GetNodeBlockType; +use test_client::runtime::Block; +use client::decl_runtime_apis; + +/// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` +/// trait are done by the `construct_runtime!` macro in a real runtime. +struct Runtime {} +impl GetNodeBlockType for Runtime { + type NodeBlock = Block; +} + +decl_runtime_apis! { + pub trait Api { + #[changed_in(2)] + fn test(data: u64); + fn test(data: u64); + } +} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/changed_in_unknown_version.stderr b/substrate/core/sr-api-macros/tests/ui/changed_in_unknown_version.stderr new file mode 100644 index 0000000000..c62befbab3 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/changed_in_unknown_version.stderr @@ -0,0 +1,5 @@ +error: `changed_in` version can not be greater than the `api_version` + --> $DIR/changed_in_unknown_version.rs:15:3 + | +15 | fn test(data: u64); + | ^^ diff --git a/substrate/core/sr-api-macros/tests/ui/declaring_old_block.rs b/substrate/core/sr-api-macros/tests/ui/declaring_old_block.rs new file mode 100644 index 0000000000..6b7f380ef2 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/declaring_old_block.rs @@ -0,0 +1,10 @@ +use runtime_primitives::traits::Block as BlockT; +use client::decl_runtime_apis; + +decl_runtime_apis! { + pub trait Api { + fn test(); + } +} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/declaring_old_block.stderr b/substrate/core/sr-api-macros/tests/ui/declaring_old_block.stderr new file mode 100644 index 0000000000..2ab1cc675d --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/declaring_old_block.stderr @@ -0,0 +1,19 @@ +error: `Block: BlockT` generic parameter will be added automatically by the `decl_runtime_apis!` macro! + --> $DIR/declaring_old_block.rs:5:16 + | +5 | pub trait Api { + | ^^^^^ + +error: `Block: BlockT` generic parameter will be added automatically by the `decl_runtime_apis!` macro! If you try to use a different trait than the substrate `Block` trait, please rename it locally. + --> $DIR/declaring_old_block.rs:5:23 + | +5 | pub trait Api { + | ^^^^^^ + +warning: unused import: `runtime_primitives::traits::Block as BlockT` + --> $DIR/declaring_old_block.rs:1:5 + | +1 | use runtime_primitives::traits::Block as BlockT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: #[warn(unused_imports)] on by default diff --git a/substrate/core/sr-api-macros/tests/ui/declaring_own_block_with_different_name.rs b/substrate/core/sr-api-macros/tests/ui/declaring_own_block_with_different_name.rs new file mode 100644 index 0000000000..1371295cc0 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/declaring_own_block_with_different_name.rs @@ -0,0 +1,10 @@ +use runtime_primitives::traits::Block as BlockT; +use client::decl_runtime_apis; + +decl_runtime_apis! { + pub trait Api { + fn test(); + } +} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/declaring_own_block_with_different_name.stderr b/substrate/core/sr-api-macros/tests/ui/declaring_own_block_with_different_name.stderr new file mode 100644 index 0000000000..cf5fe0f53f --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/declaring_own_block_with_different_name.stderr @@ -0,0 +1,13 @@ +error: `Block: BlockT` generic parameter will be added automatically by the `decl_runtime_apis!` macro! If you try to use a different trait than the substrate `Block` trait, please rename it locally. + --> $DIR/declaring_own_block_with_different_name.rs:5:19 + | +5 | pub trait Api { + | ^^^^^^ + +warning: unused import: `runtime_primitives::traits::Block as BlockT` + --> $DIR/declaring_own_block_with_different_name.rs:1:5 + | +1 | use runtime_primitives::traits::Block as BlockT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: #[warn(unused_imports)] on by default diff --git a/substrate/core/sr-api-macros/tests/ui/empty_impl_runtime_apis_call.rs b/substrate/core/sr-api-macros/tests/ui/empty_impl_runtime_apis_call.rs new file mode 100644 index 0000000000..4cf56bf54b --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/empty_impl_runtime_apis_call.rs @@ -0,0 +1,20 @@ +use runtime_primitives::traits::GetNodeBlockType; +use test_client::runtime::Block; +use client::{decl_runtime_apis, impl_runtime_apis}; + +/// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` +/// trait are done by the `construct_runtime!` macro in a real runtime. +struct Runtime {} +impl GetNodeBlockType for Runtime { + type NodeBlock = Block; +} + +decl_runtime_apis! { + pub trait Api { + fn test(data: u64); + } +} + +impl_runtime_apis! {} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/empty_impl_runtime_apis_call.stderr b/substrate/core/sr-api-macros/tests/ui/empty_impl_runtime_apis_call.stderr new file mode 100644 index 0000000000..61527a3480 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/empty_impl_runtime_apis_call.stderr @@ -0,0 +1,5 @@ +error: No api implementation given! + --> $DIR/empty_impl_runtime_apis_call.rs:18:1 + | +18 | impl_runtime_apis! {} + | ^^^^^^^^^^^^^^^^^^^^^ diff --git a/substrate/core/sr-api-macros/tests/ui/impl_incorrect_method_signature.rs b/substrate/core/sr-api-macros/tests/ui/impl_incorrect_method_signature.rs new file mode 100644 index 0000000000..91ffdd798a --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/impl_incorrect_method_signature.rs @@ -0,0 +1,24 @@ +use runtime_primitives::traits::GetNodeBlockType; +use test_client::runtime::Block; +use client::{decl_runtime_apis, impl_runtime_apis}; + +/// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` +/// trait are done by the `construct_runtime!` macro in a real runtime. +struct Runtime {} +impl GetNodeBlockType for Runtime { + type NodeBlock = Block; +} + +decl_runtime_apis! { + pub trait Api { + fn test(data: u64); + } +} + +impl_runtime_apis! { + impl self::Api for Runtime { + fn test(data: String) {} + } +} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/impl_incorrect_method_signature.stderr b/substrate/core/sr-api-macros/tests/ui/impl_incorrect_method_signature.stderr new file mode 100644 index 0000000000..6d5d484efe --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/impl_incorrect_method_signature.stderr @@ -0,0 +1,13 @@ +error[E0053]: method `test` has an incompatible type for trait + --> $DIR/impl_incorrect_method_signature.rs:20:17 + | +14 | fn test(data: u64); + | --- type in trait +... +20 | fn test(data: String) {} + | ^^^^^^ expected u64, found struct `std::string::String` + | + = note: expected type `fn(u64)` + found type `fn(std::string::String)` + +For more information about this error, try `rustc --explain E0053`. diff --git a/substrate/core/sr-api-macros/tests/ui/impl_two_traits_with_same_name.rs b/substrate/core/sr-api-macros/tests/ui/impl_two_traits_with_same_name.rs new file mode 100644 index 0000000000..0871b0ff33 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/impl_two_traits_with_same_name.rs @@ -0,0 +1,36 @@ +use runtime_primitives::traits::GetNodeBlockType; +use test_client::runtime::Block; +use client::{decl_runtime_apis, impl_runtime_apis}; + +/// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` +/// trait are done by the `construct_runtime!` macro in a real runtime. +struct Runtime {} +impl GetNodeBlockType for Runtime { + type NodeBlock = Block; +} + +decl_runtime_apis! { + pub trait Api { + fn test(data: u64); + } +} + +mod second { + decl_runtime_apis! { + pub trait Api { + fn test2(data: u64); + } + } +} + +impl_runtime_apis! { + impl self::Api for Runtime { + fn test(data: u64) {} + } + + impl second::Api for Runtime { + fn test2(data: u64) {} + } +} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/impl_two_traits_with_same_name.stderr b/substrate/core/sr-api-macros/tests/ui/impl_two_traits_with_same_name.stderr new file mode 100644 index 0000000000..355db2864b --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/impl_two_traits_with_same_name.stderr @@ -0,0 +1,25 @@ +error: Two traits with the same name detected! The trait name is used to generate its ID. Please rename one trait at the declaration! + --> $DIR/impl_two_traits_with_same_name.rs:31:15 + | +31 | impl second::Api for Runtime { + | ^^^ + +error: cannot find macro `decl_runtime_apis!` in this scope + --> $DIR/impl_two_traits_with_same_name.rs:19:2 + | +19 | decl_runtime_apis! { + | ^^^^^^^^^^^^^^^^^ + +error[E0433]: failed to resolve: could not find `runtime_decl_for_Api` in `second` + --> $DIR/impl_two_traits_with_same_name.rs:26:1 + | +26 | / impl_runtime_apis! { +27 | | impl self::Api for Runtime { +28 | | fn test(data: u64) {} +29 | | } +... | +33 | | } +34 | | } + | |_^ could not find `runtime_decl_for_Api` in `second` + +For more information about this error, try `rustc --explain E0433`. diff --git a/substrate/core/sr-api-macros/tests/ui/invalid_api_version.rs b/substrate/core/sr-api-macros/tests/ui/invalid_api_version.rs new file mode 100644 index 0000000000..b5afa1d699 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/invalid_api_version.rs @@ -0,0 +1,10 @@ +use client::decl_runtime_apis; + +decl_runtime_apis! { + #[api_version] + pub trait Api { + fn test(data: u64); + } +} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/invalid_api_version.stderr b/substrate/core/sr-api-macros/tests/ui/invalid_api_version.stderr new file mode 100644 index 0000000000..e7d6aa0ed1 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/invalid_api_version.stderr @@ -0,0 +1,29 @@ +error: can't qualify macro invocation with `pub` + --> $DIR/invalid_api_version.rs:3:1 + | +3 | / decl_runtime_apis! { +4 | | #[api_version] +5 | | pub trait Api { +6 | | fn test(data: u64); +7 | | } +8 | | } + | |_^ + | + = help: try adjusting the macro to put `pub` inside the invocation + +error: Unexpected `api_version` attribute. The supported format is `api_version(1)` + --> $DIR/invalid_api_version.rs:3:1 + | +3 | / decl_runtime_apis! { +4 | | #[api_version] +5 | | pub trait Api { +6 | | fn test(data: u64); +7 | | } +8 | | } + | |_^ + +error: Unexpected `api_version` attribute. The supported format is `api_version(1)` + --> $DIR/invalid_api_version.rs:4:4 + | +4 | #[api_version] + | ^^^^^^^^^^^ diff --git a/substrate/core/sr-api-macros/tests/ui/invalid_api_version_2.rs b/substrate/core/sr-api-macros/tests/ui/invalid_api_version_2.rs new file mode 100644 index 0000000000..7158630793 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/invalid_api_version_2.rs @@ -0,0 +1,11 @@ +use client::decl_runtime_apis; + +decl_runtime_apis! { + #[api_version("1")] + pub trait Api { + fn test(data: u64); + } +} + +fn main() {} + diff --git a/substrate/core/sr-api-macros/tests/ui/invalid_api_version_2.stderr b/substrate/core/sr-api-macros/tests/ui/invalid_api_version_2.stderr new file mode 100644 index 0000000000..3e46efb258 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/invalid_api_version_2.stderr @@ -0,0 +1,29 @@ +error: can't qualify macro invocation with `pub` + --> $DIR/invalid_api_version_2.rs:3:1 + | +3 | / decl_runtime_apis! { +4 | | #[api_version("1")] +5 | | pub trait Api { +6 | | fn test(data: u64); +7 | | } +8 | | } + | |_^ + | + = help: try adjusting the macro to put `pub` inside the invocation + +error: Unexpected `api_version` attribute. The supported format is `api_version(1)` + --> $DIR/invalid_api_version_2.rs:3:1 + | +3 | / decl_runtime_apis! { +4 | | #[api_version("1")] +5 | | pub trait Api { +6 | | fn test(data: u64); +7 | | } +8 | | } + | |_^ + +error: Unexpected `api_version` attribute. The supported format is `api_version(1)` + --> $DIR/invalid_api_version_2.rs:4:4 + | +4 | #[api_version("1")] + | ^^^^^^^^^^^ diff --git a/substrate/core/sr-api-macros/tests/ui/invalid_api_version_3.rs b/substrate/core/sr-api-macros/tests/ui/invalid_api_version_3.rs new file mode 100644 index 0000000000..6f365b146b --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/invalid_api_version_3.rs @@ -0,0 +1,10 @@ +use client::decl_runtime_apis; + +decl_runtime_apis! { + #[api_version()] + pub trait Api { + fn test(data: u64); + } +} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/invalid_api_version_3.stderr b/substrate/core/sr-api-macros/tests/ui/invalid_api_version_3.stderr new file mode 100644 index 0000000000..661221f28e --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/invalid_api_version_3.stderr @@ -0,0 +1,29 @@ +error: can't qualify macro invocation with `pub` + --> $DIR/invalid_api_version_3.rs:3:1 + | +3 | / decl_runtime_apis! { +4 | | #[api_version()] +5 | | pub trait Api { +6 | | fn test(data: u64); +7 | | } +8 | | } + | |_^ + | + = help: try adjusting the macro to put `pub` inside the invocation + +error: Unexpected `api_version` attribute. The supported format is `api_version(1)` + --> $DIR/invalid_api_version_3.rs:3:1 + | +3 | / decl_runtime_apis! { +4 | | #[api_version()] +5 | | pub trait Api { +6 | | fn test(data: u64); +7 | | } +8 | | } + | |_^ + +error: Unexpected `api_version` attribute. The supported format is `api_version(1)` + --> $DIR/invalid_api_version_3.rs:4:4 + | +4 | #[api_version()] + | ^^^^^^^^^^^ diff --git a/substrate/core/sr-api-macros/tests/ui/missing_block_generic_parameter.rs b/substrate/core/sr-api-macros/tests/ui/missing_block_generic_parameter.rs new file mode 100644 index 0000000000..eafe53e23b --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/missing_block_generic_parameter.rs @@ -0,0 +1,26 @@ +use runtime_primitives::traits::GetNodeBlockType; +use test_client::runtime::Block; +use client::{decl_runtime_apis, impl_runtime_apis}; + +/// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` +/// trait are done by the `construct_runtime!` macro in a real runtime. +struct Runtime {} +impl GetNodeBlockType for Runtime { + type NodeBlock = Block; +} + +decl_runtime_apis! { + pub trait Api { + fn test(data: u64); + } +} + +impl_runtime_apis! { + impl self::Api for Runtime { + fn test(data: u64) { + unimplemented!() + } + } +} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/missing_block_generic_parameter.stderr b/substrate/core/sr-api-macros/tests/ui/missing_block_generic_parameter.stderr new file mode 100644 index 0000000000..5c8563a8b8 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/missing_block_generic_parameter.stderr @@ -0,0 +1,13 @@ +error: Missing `Block` generic parameter. + --> $DIR/missing_block_generic_parameter.rs:19:13 + | +19 | impl self::Api for Runtime { + | ^^^ + +error[E0107]: wrong number of type arguments: expected 1, found 0 + --> $DIR/missing_block_generic_parameter.rs:19:7 + | +19 | impl self::Api for Runtime { + | ^^^^^^^^^ expected 1 type argument + +For more information about this error, try `rustc --explain E0107`. diff --git a/substrate/core/sr-api-macros/tests/ui/missing_path_for_trait.rs b/substrate/core/sr-api-macros/tests/ui/missing_path_for_trait.rs new file mode 100644 index 0000000000..cbf339e73b --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/missing_path_for_trait.rs @@ -0,0 +1,26 @@ +use runtime_primitives::traits::GetNodeBlockType; +use test_client::runtime::Block; +use client::{decl_runtime_apis, impl_runtime_apis}; + +/// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` +/// trait are done by the `construct_runtime!` macro in a real runtime. +struct Runtime {} +impl GetNodeBlockType for Runtime { + type NodeBlock = Block; +} + +decl_runtime_apis! { + pub trait Api { + fn test(data: u64); + } +} + +impl_runtime_apis! { + impl Api for Runtime { + fn test(data: u64) { + unimplemented!() + } + } +} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/missing_path_for_trait.stderr b/substrate/core/sr-api-macros/tests/ui/missing_path_for_trait.stderr new file mode 100644 index 0000000000..4018712e3f --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/missing_path_for_trait.stderr @@ -0,0 +1,5 @@ +error: The implemented trait has to be referenced with a path, e.g. `impl client::Core for Runtime`. + --> $DIR/missing_path_for_trait.rs:19:7 + | +19 | impl Api for Runtime { + | ^^^ diff --git a/substrate/core/sr-api-macros/tests/ui/type_reference_in_impl_runtime_apis_call.rs b/substrate/core/sr-api-macros/tests/ui/type_reference_in_impl_runtime_apis_call.rs new file mode 100644 index 0000000000..014b7bd1e8 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/type_reference_in_impl_runtime_apis_call.rs @@ -0,0 +1,26 @@ +use runtime_primitives::traits::GetNodeBlockType; +use test_client::runtime::Block; +use client::{decl_runtime_apis, impl_runtime_apis}; + +/// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` +/// trait are done by the `construct_runtime!` macro in a real runtime. +struct Runtime {} +impl GetNodeBlockType for Runtime { + type NodeBlock = Block; +} + +decl_runtime_apis! { + pub trait Api { + fn test(data: u64); + } +} + +impl_runtime_apis! { + impl self::Api for Runtime { + fn test(data: &u64) { + unimplemented!() + } + } +} + +fn main() {} diff --git a/substrate/core/sr-api-macros/tests/ui/type_reference_in_impl_runtime_apis_call.stderr b/substrate/core/sr-api-macros/tests/ui/type_reference_in_impl_runtime_apis_call.stderr new file mode 100644 index 0000000000..12ec399972 --- /dev/null +++ b/substrate/core/sr-api-macros/tests/ui/type_reference_in_impl_runtime_apis_call.stderr @@ -0,0 +1,13 @@ +error[E0053]: method `test` has an incompatible type for trait + --> $DIR/type_reference_in_impl_runtime_apis_call.rs:20:17 + | +14 | fn test(data: u64); + | --- type in trait +... +20 | fn test(data: &u64) { + | ^^^^ expected u64, found &u64 + | + = note: expected type `fn(u64)` + found type `fn(&u64)` + +For more information about this error, try `rustc --explain E0053`.