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

Commit f406f49

Browse files
bkchrgui1117
andcommitted
Make transactional attribute less scope dependent (#7112)
* Make `transactional` attribute less scope dependent The old implementation expected that `frame-support` wasn't imported under a different name. Besides that the pr removes some whitespaces. * Update frame/support/procedural/src/lib.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
1 parent 111a110 commit f406f49

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

frame/support/procedural/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
// tag::description[]
1918
//! Proc macro of Support code for the runtime.
20-
// end::description[]
2119
2220
#![recursion_limit="512"]
2321

@@ -296,7 +294,7 @@ pub fn construct_runtime(input: TokenStream) -> TokenStream {
296294
/// The return type of the annotated function must be `Result`. All changes to storage performed
297295
/// by the annotated function are discarded if it returns `Err`, or committed if `Ok`.
298296
///
299-
/// #Example
297+
/// # Example
300298
///
301299
/// ```nocompile
302300
/// #[transactional]
@@ -313,5 +311,5 @@ pub fn construct_runtime(input: TokenStream) -> TokenStream {
313311
/// ```
314312
#[proc_macro_attribute]
315313
pub fn transactional(attr: TokenStream, input: TokenStream) -> TokenStream {
316-
transactional::transactional(attr, input)
314+
transactional::transactional(attr, input).unwrap_or_else(|e| e.to_compile_error().into())
317315
}

frame/support/procedural/src/transactional.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717

1818
use proc_macro::TokenStream;
1919
use quote::quote;
20-
use syn::{parse_macro_input, ItemFn};
20+
use syn::{ItemFn, Result};
21+
use frame_support_procedural_tools::generate_crate_access_2018;
2122

22-
pub fn transactional(_attr: TokenStream, input: TokenStream) -> TokenStream {
23-
let ItemFn { attrs, vis, sig, block } = parse_macro_input!(input as ItemFn);
23+
pub fn transactional(_attr: TokenStream, input: TokenStream) -> Result<TokenStream> {
24+
let ItemFn { attrs, vis, sig, block } = syn::parse(input)?;
2425

26+
let crate_ = generate_crate_access_2018()?;
2527
let output = quote! {
2628
#(#attrs)*
27-
#vis #sig {
28-
use frame_support::storage::{with_transaction, TransactionOutcome};
29+
#vis #sig {
30+
use #crate_::storage::{with_transaction, TransactionOutcome};
2931
with_transaction(|| {
3032
let r = #block;
3133
if r.is_ok() {
@@ -34,7 +36,8 @@ pub fn transactional(_attr: TokenStream, input: TokenStream) -> TokenStream {
3436
TransactionOutcome::Rollback(r)
3537
}
3638
})
37-
}
38-
};
39-
output.into()
39+
}
40+
};
41+
42+
Ok(output.into())
4043
}

frame/support/procedural/tools/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ pub fn generate_crate_access(unique_id: &str, def_crate: &str) -> TokenStream {
4646
}
4747
}
4848

49+
/// Generate the crate access for the `frame-support` crate using 2018 syntax.
50+
///
51+
/// Output will for example be `frame_support`.
52+
pub fn generate_crate_access_2018() -> Result<TokenStream, Error> {
53+
if std::env::var("CARGO_PKG_NAME").unwrap() == "frame-support" {
54+
Ok(quote::quote!( frame_support ))
55+
} else {
56+
match crate_name("frame-support") {
57+
Ok(name) => {
58+
let name = Ident::new(&name, Span::call_site());
59+
Ok(quote!( #name ))
60+
},
61+
Err(e) => {
62+
Err(Error::new(Span::call_site(), &e))
63+
}
64+
}
65+
}
66+
}
67+
4968
/// Generates the hidden includes that are required to make the macro independent from its scope.
5069
pub fn generate_hidden_includes(unique_id: &str, def_crate: &str) -> TokenStream {
5170
if std::env::var("CARGO_PKG_NAME").unwrap() == def_crate {

0 commit comments

Comments
 (0)