From 18df38a4b63d39508c323e1042ccbcac20e22cd0 Mon Sep 17 00:00:00 2001 From: Sam Johnson <sam@durosoft.com> Date: Fri, 16 Jun 2023 09:06:28 -0400 Subject: [PATCH] update macro_magic to 0.4.1 (#14356) * update to macro_magic 0.4.0 * remove deprecated syntax and related doc comments * upgrade to macro_magic v0.4.1 * fix import issue * fix UI tests * resolve frame_support path properly * add docs note about importing * fix typo * Update frame/support/procedural/src/lib.rs Co-authored-by: Guillaume Yu Thiolliere <gui.thiolliere@gmail.com> * revert UI tests changes because we can't use rust 1.70 * fix UI tests * fix another UI test * use simplified import style * switch back import since tests are written expecting it that way --------- Co-authored-by: Guillaume Yu Thiolliere <gui.thiolliere@gmail.com> --- substrate/Cargo.lock | 16 ++--- .../frame/examples/default-config/src/lib.rs | 8 +-- .../frame/examples/kitchensink/src/tests.rs | 4 +- substrate/frame/support/Cargo.toml | 2 +- substrate/frame/support/procedural/Cargo.toml | 2 +- substrate/frame/support/procedural/src/lib.rs | 71 ++++++++++++------- substrate/frame/support/src/lib.rs | 1 - .../attached_to_non_impl.stderr | 16 +---- .../bad_disambiguation_path.stderr | 17 +---- 9 files changed, 65 insertions(+), 72 deletions(-) diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 5584971260c..8564a4198d4 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -4845,9 +4845,9 @@ dependencies = [ [[package]] name = "macro_magic" -version = "0.3.5" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a2d6d7fe4741b5621cf7c8048e472933877c7ea870cbf1420da55ea9f3bb08c" +checksum = "614b1304ab7877b499925b4dcc5223ff480f2646ad4db1ee7065badb8d530439" dependencies = [ "macro_magic_core", "macro_magic_macros", @@ -4857,9 +4857,9 @@ dependencies = [ [[package]] name = "macro_magic_core" -version = "0.3.5" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3005604258419767cacc5989c2dd75263f8b33773dd680734f598eb88baf5370" +checksum = "a8d72c1b662d07b8e482c80d3a7fc4168e058b3bef4c573e94feb714b670f406" dependencies = [ "derive-syn-parse", "macro_magic_core_macros", @@ -4870,9 +4870,9 @@ dependencies = [ [[package]] name = "macro_magic_core_macros" -version = "0.3.5" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de6267819c9042df1a9e62ca279e5a34254ad5dfdcb13ff988f560d75576e8b4" +checksum = "93d7d9e6e234c040dafc745c7592738d56a03ad04b1fa04ab60821deb597466a" dependencies = [ "proc-macro2", "quote", @@ -4881,9 +4881,9 @@ dependencies = [ [[package]] name = "macro_magic_macros" -version = "0.3.5" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc7176ac15ab2ed7f335e2398f729b9562dae0c233705bc1e1e3acd8452d403d" +checksum = "ffd19f13cfd2bfbd83692adfef8c244fe5109b3eb822a1fb4e0a6253b406cd81" dependencies = [ "macro_magic_core", "quote", diff --git a/substrate/frame/examples/default-config/src/lib.rs b/substrate/frame/examples/default-config/src/lib.rs index adb2469e92f..5112f3d72d5 100644 --- a/substrate/frame/examples/default-config/src/lib.rs +++ b/substrate/frame/examples/default-config/src/lib.rs @@ -106,12 +106,6 @@ pub mod pallet { #[cfg(any(test, doc))] pub mod tests { use super::*; - - use frame_support::macro_magic::use_attr; - // Because `derive_impl` is a [macro_magic](https://crates.io/crates/macro_magic) attribute - // macro, [`#[use_attr]`](`frame_support::macro_magic::use_attr`) must be attached to any use - // statement that brings it into scope. - #[use_attr] use frame_support::derive_impl; use super::pallet as pallet_default_config_example; @@ -168,7 +162,7 @@ pub mod tests { } // Similarly, we use the defaults provided by own crate as well. - use pallet::config_preludes::TestDefaultConfig; + use pallet::config_preludes::*; #[derive_impl(TestDefaultConfig as pallet::DefaultConfig)] impl crate::pallet::Config for Test { // These two both cannot have defaults. diff --git a/substrate/frame/examples/kitchensink/src/tests.rs b/substrate/frame/examples/kitchensink/src/tests.rs index d6c0891df89..a8645bedcb7 100644 --- a/substrate/frame/examples/kitchensink/src/tests.rs +++ b/substrate/frame/examples/kitchensink/src/tests.rs @@ -18,9 +18,7 @@ //! Tests for pallet-example-kitchensink. use crate::*; -#[use_attr] -use frame_support::derive_impl; -use frame_support::{macro_magic::use_attr, parameter_types, traits::ConstU64}; +use frame_support::{derive_impl, parameter_types, traits::ConstU64}; use sp_runtime::BuildStorage; // Reexport crate as its pallet name for construct_runtime. use crate as pallet_example_kitchensink; diff --git a/substrate/frame/support/Cargo.toml b/substrate/frame/support/Cargo.toml index 030a52485b0..5e1eb9dc3fe 100644 --- a/substrate/frame/support/Cargo.toml +++ b/substrate/frame/support/Cargo.toml @@ -29,7 +29,7 @@ sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../pr sp-weights = { version = "20.0.0", default-features = false, path = "../../primitives/weights" } sp-debug-derive = { default-features = false, path = "../../primitives/debug-derive" } tt-call = "1.0.8" -macro_magic = "0.3.5" +macro_magic = "0.4.1" frame-support-procedural = { version = "4.0.0-dev", default-features = false, path = "./procedural" } paste = "1.0" once_cell = { version = "1", default-features = false, optional = true } diff --git a/substrate/frame/support/procedural/Cargo.toml b/substrate/frame/support/procedural/Cargo.toml index f8c6b9393f0..5614d8a58ae 100644 --- a/substrate/frame/support/procedural/Cargo.toml +++ b/substrate/frame/support/procedural/Cargo.toml @@ -24,7 +24,7 @@ quote = "1.0.28" syn = { version = "2.0.16", features = ["full"] } frame-support-procedural-tools = { version = "4.0.0-dev", path = "./tools" } proc-macro-warning = { version = "0.4.1", default-features = false } -macro_magic = { version = "0.3.5", features = ["proc_support"] } +macro_magic = { version = "0.4.1", features = ["proc_support"] } expander = "2.0.0" [features] diff --git a/substrate/frame/support/procedural/src/lib.rs b/substrate/frame/support/procedural/src/lib.rs index 2b790c6d4d0..bde5b55148b 100644 --- a/substrate/frame/support/procedural/src/lib.rs +++ b/substrate/frame/support/procedural/src/lib.rs @@ -37,6 +37,7 @@ mod storage_alias; mod transactional; mod tt_macro; +use frame_support_procedural_tools::generate_crate_access_2018; use macro_magic::import_tokens_attr; use proc_macro::TokenStream; use quote::{quote, ToTokens}; @@ -952,28 +953,6 @@ pub fn storage_alias(_: TokenStream, input: TokenStream) -> TokenStream { /// /// # Advanced Usage /// -/// ## Importing & Re-Exporting -/// -/// Since `#[derive_impl(..)]` is a -/// [`macro_magic`](https://docs.rs/macro_magic/latest/macro_magic/)-based attribute macro, special -/// care must be taken when importing and re-exporting it. Glob imports will work properly, such as -/// `use frame_support::*` to bring `derive_impl` into scope, however any other use statements -/// involving `derive_impl` should have -/// [`#[macro_magic::use_attr]`](https://docs.rs/macro_magic/latest/macro_magic/attr.use_attr.html) -/// attached or your use statement will fail to fully bring the macro into scope. -/// -/// This brings `derive_impl` into scope in the current context: -/// ```ignore -/// #[use_attr] -/// use frame_support::derive_impl; -/// ``` -/// -/// This brings `derive_impl` into scope and publicly re-exports it from the current context: -/// ```ignore -/// #[use_attr] -/// pub use frame_support::derive_impl; -/// ``` -/// /// ## Expansion /// /// The `#[derive_impl(default_impl_path as disambiguation_path)]` attribute will expand to the @@ -987,7 +966,18 @@ pub fn storage_alias(_: TokenStream, input: TokenStream) -> TokenStream { /// Items that lack a `syn::Ident` for whatever reason are first checked to see if they exist, /// verbatim, in the local/destination trait before they are copied over, so you should not need to /// worry about collisions between identical unnamed items. -#[import_tokens_attr(frame_support::macro_magic)] +#[import_tokens_attr { + format!( + "{}::macro_magic", + match generate_crate_access_2018("frame-support") { + Ok(path) => Ok(path), + Err(_) => generate_crate_access_2018("frame"), + } + .expect("Failed to find either `frame-support` or `frame` in `Cargo.toml` dependencies.") + .to_token_stream() + .to_string() + ) +}] #[with_custom_parsing(derive_impl::DeriveImplAttrArgs)] #[proc_macro_attribute] pub fn derive_impl(attrs: TokenStream, input: TokenStream) -> TokenStream { @@ -1034,8 +1024,41 @@ pub fn no_default(_: TokenStream, _: TokenStream) -> TokenStream { /// type MaxConsumers = frame_support::traits::ConstU32<16>; /// } /// ``` +/// +/// ## Advanced Usage +/// /// This macro acts as a thin wrapper around macro_magic's `#[export_tokens]`. See the docs -/// [here](https://docs.rs/macro_magic/latest/macro_magic/attr.export_tokens.html) for more info. +/// [here](https://docs.rs/macro_magic/latest/macro_magic/attr.export_tokens.html) for more +/// info. +/// +/// There are some caveats when applying a `use` statement to bring a +/// `#[register_default_impl]` item into scope. If you have a `#[register_default_impl]` +/// defined in `my_crate::submodule::MyItem`, it is currently not sufficient to do something +/// like: +/// +/// ```ignore +/// use my_crate::submodule::MyItem; +/// #[derive_impl(MyItem as Whatever)] +/// ``` +/// +/// This will fail with a mysterious message about `__export_tokens_tt_my_item` not being +/// defined. +/// +/// You can, however, do any of the following: +/// ```ignore +/// // partial path works +/// use my_crate::submodule; +/// #[derive_impl(submodule::MyItem as Whatever)] +/// ``` +/// ```ignore +/// // full path works +/// #[derive_impl(my_crate::submodule::MyItem as Whatever)] +/// ``` +/// ```ignore +/// // wild-cards work +/// use my_crate::submodule::*; +/// #[derive_impl(MyItem as Whatever)] +/// ``` #[proc_macro_attribute] pub fn register_default_impl(attrs: TokenStream, tokens: TokenStream) -> TokenStream { // ensure this is a impl statement diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index 8c2fd4fb62e..0f2b135eed3 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -210,7 +210,6 @@ impl TypeId for PalletId { /// ``` pub use frame_support_procedural::storage_alias; -#[macro_magic::use_attr] pub use frame_support_procedural::derive_impl; /// Create new implementations of the [`Get`](crate::traits::Get) trait. diff --git a/substrate/frame/support/test/tests/derive_impl_ui/attached_to_non_impl.stderr b/substrate/frame/support/test/tests/derive_impl_ui/attached_to_non_impl.stderr index 94aee244251..735fd7a628e 100644 --- a/substrate/frame/support/test/tests/derive_impl_ui/attached_to_non_impl.stderr +++ b/substrate/frame/support/test/tests/derive_impl_ui/attached_to_non_impl.stderr @@ -1,15 +1,5 @@ error: expected `impl` - --> tests/derive_impl_ui/attached_to_non_impl.rs:24:1 + --> tests/derive_impl_ui/attached_to_non_impl.rs:39:1 | -24 | / #[register_default_impl(FourLeggedAnimal)] -25 | | impl Animal for FourLeggedAnimal { -26 | | type Locomotion = RunsOnFourLegs; -27 | | type Diet = Omnivore; -... | -37 | | -38 | | #[derive_impl(FourLeggedAnimal as Animal)] - | |_-----------------------------------------^ - | | - | in this procedural macro expansion - | - = note: this error originates in the macro `__import_tokens_attr_derive_impl_inner` which comes from the expansion of the attribute macro `derive_impl` (in Nightly builds, run with -Z macro-backtrace for more info) +39 | struct Something {} + | ^^^^^^ diff --git a/substrate/frame/support/test/tests/derive_impl_ui/bad_disambiguation_path.stderr b/substrate/frame/support/test/tests/derive_impl_ui/bad_disambiguation_path.stderr index dd098d82c1e..76cf1646c48 100644 --- a/substrate/frame/support/test/tests/derive_impl_ui/bad_disambiguation_path.stderr +++ b/substrate/frame/support/test/tests/derive_impl_ui/bad_disambiguation_path.stderr @@ -1,16 +1,5 @@ error[E0433]: failed to resolve: use of undeclared type `Insect` - --> tests/derive_impl_ui/bad_disambiguation_path.rs:24:1 + --> tests/derive_impl_ui/bad_disambiguation_path.rs:38:35 | -24 | / #[register_default_impl(FourLeggedAnimal)] -25 | | impl Animal for FourLeggedAnimal { -26 | | type Locomotion = RunsOnFourLegs; -27 | | type Diet = Omnivore; -... | -37 | | -38 | | #[derive_impl(FourLeggedAnimal as Insect)] - | | -----------------------------------------^ - | |_|________________________________________| - | | use of undeclared type `Insect` - | in this procedural macro expansion - | - = note: this error originates in the macro `__import_tokens_attr_derive_impl_inner` which comes from the expansion of the attribute macro `derive_impl` (in Nightly builds, run with -Z macro-backtrace for more info) +38 | #[derive_impl(FourLeggedAnimal as Insect)] + | ^^^^^^ use of undeclared type `Insect` -- GitLab