Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 6bee6fc

Browse files
sam0x17gui1117
andauthored
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 <[email protected]> * 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 <[email protected]>
1 parent 16083b3 commit 6bee6fc

File tree

9 files changed

+65
-72
lines changed

9 files changed

+65
-72
lines changed

Cargo.lock

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frame/examples/default-config/src/lib.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,6 @@ pub mod pallet {
106106
#[cfg(any(test, doc))]
107107
pub mod tests {
108108
use super::*;
109-
110-
use frame_support::macro_magic::use_attr;
111-
// Because `derive_impl` is a [macro_magic](https://crates.io/crates/macro_magic) attribute
112-
// macro, [`#[use_attr]`](`frame_support::macro_magic::use_attr`) must be attached to any use
113-
// statement that brings it into scope.
114-
#[use_attr]
115109
use frame_support::derive_impl;
116110

117111
use super::pallet as pallet_default_config_example;
@@ -168,7 +162,7 @@ pub mod tests {
168162
}
169163

170164
// Similarly, we use the defaults provided by own crate as well.
171-
use pallet::config_preludes::TestDefaultConfig;
165+
use pallet::config_preludes::*;
172166
#[derive_impl(TestDefaultConfig as pallet::DefaultConfig)]
173167
impl crate::pallet::Config for Test {
174168
// These two both cannot have defaults.

frame/examples/kitchensink/src/tests.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
//! Tests for pallet-example-kitchensink.
1919
2020
use crate::*;
21-
#[use_attr]
22-
use frame_support::derive_impl;
23-
use frame_support::{macro_magic::use_attr, parameter_types, traits::ConstU64};
21+
use frame_support::{derive_impl, parameter_types, traits::ConstU64};
2422
use sp_runtime::BuildStorage;
2523
// Reexport crate as its pallet name for construct_runtime.
2624
use crate as pallet_example_kitchensink;

frame/support/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../pr
2929
sp-weights = { version = "20.0.0", default-features = false, path = "../../primitives/weights" }
3030
sp-debug-derive = { default-features = false, path = "../../primitives/debug-derive" }
3131
tt-call = "1.0.8"
32-
macro_magic = "0.3.5"
32+
macro_magic = "0.4.1"
3333
frame-support-procedural = { version = "4.0.0-dev", default-features = false, path = "./procedural" }
3434
paste = "1.0"
3535
once_cell = { version = "1", default-features = false, optional = true }

frame/support/procedural/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ quote = "1.0.28"
2424
syn = { version = "2.0.16", features = ["full"] }
2525
frame-support-procedural-tools = { version = "4.0.0-dev", path = "./tools" }
2626
proc-macro-warning = { version = "0.4.1", default-features = false }
27-
macro_magic = { version = "0.3.5", features = ["proc_support"] }
27+
macro_magic = { version = "0.4.1", features = ["proc_support"] }
2828
expander = "2.0.0"
2929

3030
[features]

frame/support/procedural/src/lib.rs

+47-24
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ mod storage_alias;
3737
mod transactional;
3838
mod tt_macro;
3939

40+
use frame_support_procedural_tools::generate_crate_access_2018;
4041
use macro_magic::import_tokens_attr;
4142
use proc_macro::TokenStream;
4243
use quote::{quote, ToTokens};
@@ -952,28 +953,6 @@ pub fn storage_alias(_: TokenStream, input: TokenStream) -> TokenStream {
952953
///
953954
/// # Advanced Usage
954955
///
955-
/// ## Importing & Re-Exporting
956-
///
957-
/// Since `#[derive_impl(..)]` is a
958-
/// [`macro_magic`](https://docs.rs/macro_magic/latest/macro_magic/)-based attribute macro, special
959-
/// care must be taken when importing and re-exporting it. Glob imports will work properly, such as
960-
/// `use frame_support::*` to bring `derive_impl` into scope, however any other use statements
961-
/// involving `derive_impl` should have
962-
/// [`#[macro_magic::use_attr]`](https://docs.rs/macro_magic/latest/macro_magic/attr.use_attr.html)
963-
/// attached or your use statement will fail to fully bring the macro into scope.
964-
///
965-
/// This brings `derive_impl` into scope in the current context:
966-
/// ```ignore
967-
/// #[use_attr]
968-
/// use frame_support::derive_impl;
969-
/// ```
970-
///
971-
/// This brings `derive_impl` into scope and publicly re-exports it from the current context:
972-
/// ```ignore
973-
/// #[use_attr]
974-
/// pub use frame_support::derive_impl;
975-
/// ```
976-
///
977956
/// ## Expansion
978957
///
979958
/// 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 {
987966
/// Items that lack a `syn::Ident` for whatever reason are first checked to see if they exist,
988967
/// verbatim, in the local/destination trait before they are copied over, so you should not need to
989968
/// worry about collisions between identical unnamed items.
990-
#[import_tokens_attr(frame_support::macro_magic)]
969+
#[import_tokens_attr {
970+
format!(
971+
"{}::macro_magic",
972+
match generate_crate_access_2018("frame-support") {
973+
Ok(path) => Ok(path),
974+
Err(_) => generate_crate_access_2018("frame"),
975+
}
976+
.expect("Failed to find either `frame-support` or `frame` in `Cargo.toml` dependencies.")
977+
.to_token_stream()
978+
.to_string()
979+
)
980+
}]
991981
#[with_custom_parsing(derive_impl::DeriveImplAttrArgs)]
992982
#[proc_macro_attribute]
993983
pub fn derive_impl(attrs: TokenStream, input: TokenStream) -> TokenStream {
@@ -1034,8 +1024,41 @@ pub fn no_default(_: TokenStream, _: TokenStream) -> TokenStream {
10341024
/// type MaxConsumers = frame_support::traits::ConstU32<16>;
10351025
/// }
10361026
/// ```
1027+
///
1028+
/// ## Advanced Usage
1029+
///
10371030
/// This macro acts as a thin wrapper around macro_magic's `#[export_tokens]`. See the docs
1038-
/// [here](https://docs.rs/macro_magic/latest/macro_magic/attr.export_tokens.html) for more info.
1031+
/// [here](https://docs.rs/macro_magic/latest/macro_magic/attr.export_tokens.html) for more
1032+
/// info.
1033+
///
1034+
/// There are some caveats when applying a `use` statement to bring a
1035+
/// `#[register_default_impl]` item into scope. If you have a `#[register_default_impl]`
1036+
/// defined in `my_crate::submodule::MyItem`, it is currently not sufficient to do something
1037+
/// like:
1038+
///
1039+
/// ```ignore
1040+
/// use my_crate::submodule::MyItem;
1041+
/// #[derive_impl(MyItem as Whatever)]
1042+
/// ```
1043+
///
1044+
/// This will fail with a mysterious message about `__export_tokens_tt_my_item` not being
1045+
/// defined.
1046+
///
1047+
/// You can, however, do any of the following:
1048+
/// ```ignore
1049+
/// // partial path works
1050+
/// use my_crate::submodule;
1051+
/// #[derive_impl(submodule::MyItem as Whatever)]
1052+
/// ```
1053+
/// ```ignore
1054+
/// // full path works
1055+
/// #[derive_impl(my_crate::submodule::MyItem as Whatever)]
1056+
/// ```
1057+
/// ```ignore
1058+
/// // wild-cards work
1059+
/// use my_crate::submodule::*;
1060+
/// #[derive_impl(MyItem as Whatever)]
1061+
/// ```
10391062
#[proc_macro_attribute]
10401063
pub fn register_default_impl(attrs: TokenStream, tokens: TokenStream) -> TokenStream {
10411064
// ensure this is a impl statement

frame/support/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ impl TypeId for PalletId {
210210
/// ```
211211
pub use frame_support_procedural::storage_alias;
212212

213-
#[macro_magic::use_attr]
214213
pub use frame_support_procedural::derive_impl;
215214

216215
/// Create new implementations of the [`Get`](crate::traits::Get) trait.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
error: expected `impl`
2-
--> tests/derive_impl_ui/attached_to_non_impl.rs:24:1
2+
--> tests/derive_impl_ui/attached_to_non_impl.rs:39:1
33
|
4-
24 | / #[register_default_impl(FourLeggedAnimal)]
5-
25 | | impl Animal for FourLeggedAnimal {
6-
26 | | type Locomotion = RunsOnFourLegs;
7-
27 | | type Diet = Omnivore;
8-
... |
9-
37 | |
10-
38 | | #[derive_impl(FourLeggedAnimal as Animal)]
11-
| |_-----------------------------------------^
12-
| |
13-
| in this procedural macro expansion
14-
|
15-
= 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)
4+
39 | struct Something {}
5+
| ^^^^^^
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
error[E0433]: failed to resolve: use of undeclared type `Insect`
2-
--> tests/derive_impl_ui/bad_disambiguation_path.rs:24:1
2+
--> tests/derive_impl_ui/bad_disambiguation_path.rs:38:35
33
|
4-
24 | / #[register_default_impl(FourLeggedAnimal)]
5-
25 | | impl Animal for FourLeggedAnimal {
6-
26 | | type Locomotion = RunsOnFourLegs;
7-
27 | | type Diet = Omnivore;
8-
... |
9-
37 | |
10-
38 | | #[derive_impl(FourLeggedAnimal as Insect)]
11-
| | -----------------------------------------^
12-
| |_|________________________________________|
13-
| | use of undeclared type `Insect`
14-
| in this procedural macro expansion
15-
|
16-
= 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)
4+
38 | #[derive_impl(FourLeggedAnimal as Insect)]
5+
| ^^^^^^ use of undeclared type `Insect`

0 commit comments

Comments
 (0)