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

Commit 7dc8022

Browse files
committed
working + clean up
1 parent 07d80f5 commit 7dc8022

File tree

5 files changed

+8
-175
lines changed

5 files changed

+8
-175
lines changed

frame/examples/basic/src/tests.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@
1919
2020
use crate::*;
2121
use frame_support::{
22-
assert_ok, derive_impl_inner,
22+
assert_ok,
2323
dispatch::{DispatchInfo, GetDispatchInfo},
2424
macro_magic::use_attr,
2525
traits::{ConstU64, OnInitialize},
2626
};
27-
use sp_core::H256;
27+
2828
// The testing primitives are very useful for avoiding having to work with signatures
2929
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
30-
use sp_runtime::{
31-
testing::Header,
32-
traits::{BlakeTwo256, IdentityLookup},
33-
BuildStorage,
34-
};
30+
use sp_runtime::{testing::Header, BuildStorage};
3531
// Reexport crate as its pallet name for construct_runtime.
3632
use crate as pallet_example_basic;
3733

frame/support/procedural/src/derive_impl.rs

+3-155
Original file line numberDiff line numberDiff line change
@@ -21,124 +21,7 @@ use macro_magic::core::pretty_print;
2121
use proc_macro2::TokenStream as TokenStream2;
2222
use quote::{quote, ToTokens};
2323
use std::collections::HashSet;
24-
use syn::{
25-
braced, bracketed,
26-
parse::{Parse, ParseStream},
27-
parse2, parse_quote,
28-
punctuated::Punctuated,
29-
token::{Brace, Bracket},
30-
Ident, ImplItem, ItemImpl, Path, Result, Token, TypePath,
31-
};
32-
33-
mod keywords {
34-
use syn::custom_keyword;
35-
36-
custom_keyword!(derive_impl);
37-
custom_keyword!(partial_impl_block);
38-
custom_keyword!(implementing_type);
39-
custom_keyword!(type_items);
40-
custom_keyword!(fn_items);
41-
custom_keyword!(const_items);
42-
}
43-
44-
pub struct DeriveImplDef {
45-
/// The partial impl block that the user provides. This should be interpreted as "override".
46-
partial_impl_block: ItemImpl,
47-
/// The full path to the type that can be used to receive defaults form.
48-
implementing_type: TypePath,
49-
/// All of the associated type items that we must eventually implement.
50-
type_items: Punctuated<Ident, Token![,]>,
51-
/// All of the function items that we must eventually implement.
52-
fn_items: Punctuated<Ident, Token![,]>,
53-
/// All of the constant items that we must eventually implement.
54-
const_items: Punctuated<Ident, Token![,]>,
55-
}
56-
57-
impl Parse for DeriveImplDef {
58-
fn parse(input: ParseStream) -> Result<Self> {
59-
// NOTE: unfortunately, the order the keywords here must match what the pallet macro
60-
// expands. We can probably used a shared set of keywords later.
61-
let mut partial_impl_block;
62-
let _ = input.parse::<keywords::partial_impl_block>()?;
63-
let _ = input.parse::<Token![=]>()?;
64-
let _replace_with_bracket: Bracket = bracketed!(partial_impl_block in input);
65-
let _replace_with_brace: Brace = braced!(partial_impl_block in partial_impl_block);
66-
let partial_impl_block = partial_impl_block.parse()?;
67-
68-
let mut implementing_type;
69-
let _ = input.parse::<keywords::implementing_type>()?;
70-
let _ = input.parse::<Token![=]>()?;
71-
let _replace_with_bracket: Bracket = bracketed!(implementing_type in input);
72-
let _replace_with_brace: Brace = braced!(implementing_type in implementing_type);
73-
let implementing_type = implementing_type.parse()?;
74-
75-
let mut type_items;
76-
let _ = input.parse::<keywords::type_items>()?;
77-
let _ = input.parse::<Token![=]>()?;
78-
let _replace_with_bracket: Bracket = bracketed!(type_items in input);
79-
let _replace_with_brace: Brace = braced!(type_items in type_items);
80-
let type_items = Punctuated::<Ident, Token![,]>::parse_terminated(&type_items)?;
81-
82-
let mut fn_items;
83-
let _ = input.parse::<keywords::fn_items>()?;
84-
let _ = input.parse::<Token![=]>()?;
85-
let _replace_with_bracket: Bracket = bracketed!(fn_items in input);
86-
let _replace_with_brace: Brace = braced!(fn_items in fn_items);
87-
let fn_items = Punctuated::<Ident, Token![,]>::parse_terminated(&fn_items)?;
88-
89-
let mut const_items;
90-
let _ = input.parse::<keywords::const_items>()?;
91-
let _ = input.parse::<Token![=]>()?;
92-
let _replace_with_bracket: Bracket = bracketed!(const_items in input);
93-
let _replace_with_brace: Brace = braced!(const_items in const_items);
94-
let const_items = Punctuated::<Ident, Token![,]>::parse_terminated(&const_items)?;
95-
96-
Ok(Self { partial_impl_block, type_items, fn_items, const_items, implementing_type })
97-
}
98-
}
99-
100-
pub(crate) fn derive_impl_inner(input: TokenStream2) -> Result<TokenStream2> {
101-
println!("input: {}", input);
102-
let DeriveImplDef { partial_impl_block, implementing_type, type_items, .. } = parse2(input)?;
103-
104-
let type_item_name = |i: &ImplItem| {
105-
if let ImplItem::Type(t) = i {
106-
t.ident.clone()
107-
} else {
108-
panic!("only support type items for now")
109-
}
110-
};
111-
112-
// might be able to mutate `partial_impl_block` along the way, but easier like this for now.
113-
let mut final_impl_block = partial_impl_block.clone();
114-
let source_crate_path = implementing_type.path.segments.first().unwrap().ident.clone();
115-
116-
// TODO: ensure type ident specified in `partial_impl_block` is beyond union(type_items,
117-
// const_items, fn_items).
118-
assert!(
119-
partial_impl_block
120-
.items
121-
.iter()
122-
.all(|i| { type_items.iter().find(|tt| tt == &&type_item_name(i)).is_some() }),
123-
"some item in the partial_impl_block is unexpected"
124-
);
125-
126-
// for each item that is in `type_items` but not present in `partial_impl_block`, fill it in.
127-
type_items.iter().for_each(|ident| {
128-
if partial_impl_block.items.iter().any(|i| &type_item_name(i) == ident) {
129-
// this is already present in the partial impl block -- noop
130-
} else {
131-
// add it
132-
let tokens = quote::quote!(type #ident = <#implementing_type as #source_crate_path::pallet::DefaultConfig>::#ident;);
133-
let parsed: ImplItem = parse2(tokens).expect("it is a valid type item");
134-
debug_assert!(matches!(parsed, ImplItem::Type(_)));
135-
136-
final_impl_block.items.push(parsed)
137-
}
138-
});
139-
140-
Ok(quote::quote!(#final_impl_block))
141-
}
24+
use syn::{parse2, parse_quote, Ident, ImplItem, ItemImpl, Path, Result};
14225

14326
fn impl_item_ident(impl_item: &ImplItem) -> Option<Ident> {
14427
match impl_item {
@@ -161,6 +44,8 @@ fn combine_impls(local_impl: ItemImpl, foreign_impl: ItemImpl, foreign_path: Pat
16144
.filter(|impl_item| impl_item_ident(impl_item).is_none())
16245
.cloned()
16346
.collect();
47+
// // TODO: may not be accurate.
48+
// // source_crate_path = frame_system
16449
let source_crate_path = foreign_path.segments.first().unwrap().ident.clone();
16550
let mut final_impl = local_impl;
16651
final_impl.items.extend(
@@ -220,41 +105,4 @@ pub fn derive_impl(
220105
pretty_print(&combined_impl.to_token_stream());
221106

222107
Ok(quote!(#combined_impl))
223-
// attr: frame_system::preludes::testing::Impl
224-
// tokens:
225-
// impl frame_system::Config for Test {
226-
// // These are all defined by system as mandatory.
227-
// type BaseCallFilter = frame_support::traits::Everything;
228-
// type RuntimeEvent = RuntimeEvent;
229-
// type RuntimeCall = RuntimeCall;
230-
// type RuntimeOrigin = RuntimeOrigin;
231-
// type OnSetCode = frame_system::DefaultSetCode<Self>;
232-
// type PalletInfo = PalletInfo;
233-
// type Header = Header;
234-
// // We decide to override this one.
235-
// type AccountData = pallet_balances::AccountData<u64>;
236-
// }
237-
// let implementing_type: TypePath = parse2(attrs.clone())?;
238-
// // ideas for sam:
239-
// // let other_path_tokens = magic_macro!(path_to_other_path_token);
240-
// // let foreign_trait_tokens = import_tokens_indirect!(frame_system::testing::DefaultConfig);
241-
// // println!("{}", foreign_trait_tokens.to_string());
242-
243-
// let frame_support = generate_crate_access_2018("frame-support")?;
244-
// // TODO: may not be accurate.
245-
// let source_crate_path = implementing_type.path.segments.first().unwrap().ident.clone();
246-
// // source_crate_path = frame_system
247-
248-
// //let tokens = import_tokens_indirect!(::pallet_example_basic::pallet::Config);
249-
250-
// Ok(quote::quote!(
251-
// #frame_support::tt_call! {
252-
// macro = [{ #source_crate_path::tt_config_items }] // frame_system::tt_config_items
253-
// frame_support = [{ #frame_support }] // ::frame_support
254-
// ~~> #frame_support::derive_impl_inner! {
255-
// partial_impl_block = [{ #input }]
256-
// implementing_type = [{ #attrs }]
257-
// }
258-
// }
259-
// ))
260108
}

frame/support/procedural/src/lib.rs

-7
Original file line numberDiff line numberDiff line change
@@ -786,13 +786,6 @@ pub fn derive_impl(attrs: TokenStream, input: TokenStream) -> TokenStream {
786786
.into()
787787
}
788788

789-
#[proc_macro]
790-
pub fn derive_impl_inner(input: TokenStream) -> TokenStream {
791-
derive_impl::derive_impl_inner(input.into())
792-
.unwrap_or_else(|r| r.into_compile_error())
793-
.into()
794-
}
795-
796789
/// Used internally to decorate pallet attribute macro stubs when they are erroneously used
797790
/// outside of a pallet module
798791
fn pallet_macro_stub() -> TokenStream {

frame/support/src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,9 @@ impl TypeId for PalletId {
212212
/// ```
213213
pub use frame_support_procedural::storage_alias;
214214

215-
/// Test
216215
#[use_attr]
217216
pub use frame_support_procedural::derive_impl;
218217

219-
#[doc(hidden)]
220-
pub use frame_support_procedural::derive_impl_inner;
221-
222218
/// Create new implementations of the [`Get`](crate::traits::Get) trait.
223219
///
224220
/// The so-called parameter type can be created in four different ways:

frame/system/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ pub mod pallet {
209209
use macro_magic::export_tokens;
210210
use sp_runtime::traits::IdentityLookup;
211211

212-
pub struct Impl {}
212+
pub struct TestDefaultConfig {}
213213

214214
#[export_tokens(TestDefaultConfig)]
215-
impl DefaultConfig for Impl {
215+
impl DefaultConfig for TestDefaultConfig {
216216
type Version = ();
217217
type BlockWeights = ();
218218
type BlockLength = ();

0 commit comments

Comments
 (0)