1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
use super::helper;
use frame_support_procedural_tools::get_doc_literals;
use quote::ToTokens;
use syn::spanned::Spanned;
mod keyword {
syn::custom_keyword!(Config);
syn::custom_keyword!(From);
syn::custom_keyword!(T);
syn::custom_keyword!(I);
syn::custom_keyword!(config);
syn::custom_keyword!(IsType);
syn::custom_keyword!(Event);
syn::custom_keyword!(constant);
syn::custom_keyword!(frame_system);
syn::custom_keyword!(disable_frame_system_supertrait_check);
}
pub struct ConfigDef {
pub index: usize,
pub has_instance: bool,
pub consts_metadata: Vec<ConstMetadataDef>,
pub has_event_type: bool,
pub where_clause: Option<syn::WhereClause>,
pub attr_span: proc_macro2::Span,
}
pub struct ConstMetadataDef {
pub ident: syn::Ident,
pub type_: syn::Type,
pub doc: Vec<syn::Lit>,
}
impl TryFrom<&syn::TraitItemType> for ConstMetadataDef {
type Error = syn::Error;
fn try_from(trait_ty: &syn::TraitItemType) -> Result<Self, Self::Error> {
let err = |span, msg| {
syn::Error::new(span, format!("Invalid usage of `#[pallet::constant]`: {}", msg))
};
let doc = get_doc_literals(&trait_ty.attrs);
let ident = trait_ty.ident.clone();
let bound = trait_ty
.bounds
.iter()
.find_map(|b| {
if let syn::TypeParamBound::Trait(tb) = b {
tb.path
.segments
.last()
.and_then(|s| if s.ident == "Get" { Some(s) } else { None })
} else {
None
}
})
.ok_or_else(|| err(trait_ty.span(), "`Get<T>` trait bound not found"))?;
let type_arg = if let syn::PathArguments::AngleBracketed(ref ab) = bound.arguments {
if ab.args.len() == 1 {
if let syn::GenericArgument::Type(ref ty) = ab.args[0] {
Ok(ty)
} else {
Err(err(ab.args[0].span(), "Expected a type argument"))
}
} else {
Err(err(bound.span(), "Expected a single type argument"))
}
} else {
Err(err(bound.span(), "Expected trait generic args"))
}?;
let type_ = syn::parse2::<syn::Type>(replace_self_by_t(type_arg.to_token_stream()))
.expect("Internal error: replacing `Self` by `T` should result in valid type");
Ok(Self { ident, type_, doc })
}
}
pub struct DisableFrameSystemSupertraitCheck;
impl syn::parse::Parse for DisableFrameSystemSupertraitCheck {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
input.parse::<syn::Token![#]>()?;
let content;
syn::bracketed!(content in input);
content.parse::<syn::Ident>()?;
content.parse::<syn::Token![::]>()?;
content.parse::<keyword::disable_frame_system_supertrait_check>()?;
Ok(Self)
}
}
pub struct TypeAttrConst(proc_macro2::Span);
impl Spanned for TypeAttrConst {
fn span(&self) -> proc_macro2::Span {
self.0
}
}
impl syn::parse::Parse for TypeAttrConst {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
input.parse::<syn::Token![#]>()?;
let content;
syn::bracketed!(content in input);
content.parse::<syn::Ident>()?;
content.parse::<syn::Token![::]>()?;
Ok(TypeAttrConst(content.parse::<keyword::constant>()?.span()))
}
}
pub struct ConfigBoundParse(syn::Ident);
impl syn::parse::Parse for ConfigBoundParse {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let ident = input.parse::<syn::Ident>()?;
input.parse::<syn::Token![::]>()?;
input.parse::<keyword::Config>()?;
if input.peek(syn::token::Lt) {
input.parse::<syn::AngleBracketedGenericArguments>()?;
}
Ok(Self(ident))
}
}
pub struct IsTypeBoundEventParse(syn::Ident);
impl syn::parse::Parse for IsTypeBoundEventParse {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
input.parse::<keyword::IsType>()?;
input.parse::<syn::Token![<]>()?;
input.parse::<syn::Token![<]>()?;
input.parse::<syn::Token![Self]>()?;
input.parse::<syn::Token![as]>()?;
let ident = input.parse::<syn::Ident>()?;
input.parse::<syn::Token![::]>()?;
input.parse::<keyword::Config>()?;
input.parse::<syn::Token![>]>()?;
input.parse::<syn::Token![::]>()?;
input.parse::<keyword::Event>()?;
input.parse::<syn::Token![>]>()?;
Ok(Self(ident))
}
}
pub struct FromEventParse {
is_generic: bool,
has_instance: bool,
}
impl syn::parse::Parse for FromEventParse {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut is_generic = false;
let mut has_instance = false;
input.parse::<keyword::From>()?;
input.parse::<syn::Token![<]>()?;
input.parse::<keyword::Event>()?;
if input.peek(syn::Token![<]) {
is_generic = true;
input.parse::<syn::Token![<]>()?;
input.parse::<syn::Token![Self]>()?;
if input.peek(syn::Token![,]) {
input.parse::<syn::Token![,]>()?;
input.parse::<keyword::I>()?;
has_instance = true;
}
input.parse::<syn::Token![>]>()?;
}
input.parse::<syn::Token![>]>()?;
Ok(Self { is_generic, has_instance })
}
}
fn check_event_type(
frame_system: &syn::Ident,
trait_item: &syn::TraitItem,
trait_has_instance: bool,
) -> syn::Result<bool> {
if let syn::TraitItem::Type(type_) = trait_item {
if type_.ident == "Event" {
if !type_.generics.params.is_empty() || type_.generics.where_clause.is_some() {
let msg = "Invalid `type Event`, associated type `Event` is reserved and must have\
no generics nor where_clause";
return Err(syn::Error::new(trait_item.span(), msg))
}
let has_is_type_bound = type_.bounds.iter().any(|s| {
syn::parse2::<IsTypeBoundEventParse>(s.to_token_stream())
.map_or(false, |b| b.0 == *frame_system)
});
if !has_is_type_bound {
let msg = format!(
"Invalid `type Event`, associated type `Event` is reserved and must \
bound: `IsType<<Self as {}::Config>::Event>`",
frame_system,
);
return Err(syn::Error::new(type_.span(), msg))
}
let from_event_bound = type_
.bounds
.iter()
.find_map(|s| syn::parse2::<FromEventParse>(s.to_token_stream()).ok());
let from_event_bound = if let Some(b) = from_event_bound {
b
} else {
let msg = "Invalid `type Event`, associated type `Event` is reserved and must \
bound: `From<Event>` or `From<Event<Self>>` or `From<Event<Self, I>>`";
return Err(syn::Error::new(type_.span(), msg))
};
if from_event_bound.is_generic && (from_event_bound.has_instance != trait_has_instance)
{
let msg = "Invalid `type Event`, associated type `Event` bounds inconsistent \
`From<Event..>`. Config and generic Event must be both with instance or \
without instance";
return Err(syn::Error::new(type_.span(), msg))
}
Ok(true)
} else {
Ok(false)
}
} else {
Ok(false)
}
}
pub fn replace_self_by_t(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
input
.into_iter()
.map(|token_tree| match token_tree {
proc_macro2::TokenTree::Group(group) =>
proc_macro2::Group::new(group.delimiter(), replace_self_by_t(group.stream())).into(),
proc_macro2::TokenTree::Ident(ident) if ident == "Self" =>
proc_macro2::Ident::new("T", ident.span()).into(),
other => other,
})
.collect()
}
impl ConfigDef {
pub fn try_from(
frame_system: &syn::Ident,
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
) -> syn::Result<Self> {
let item = if let syn::Item::Trait(item) = item {
item
} else {
let msg = "Invalid pallet::config, expected trait definition";
return Err(syn::Error::new(item.span(), msg))
};
if !matches!(item.vis, syn::Visibility::Public(_)) {
let msg = "Invalid pallet::config, trait must be public";
return Err(syn::Error::new(item.span(), msg))
}
syn::parse2::<keyword::Config>(item.ident.to_token_stream())?;
let where_clause = {
let stream = replace_self_by_t(item.generics.where_clause.to_token_stream());
syn::parse2::<Option<syn::WhereClause>>(stream).expect(
"Internal error: replacing `Self` by `T` should result in valid where
clause",
)
};
if item.generics.params.len() > 1 {
let msg = "Invalid pallet::config, expected no more than one generic";
return Err(syn::Error::new(item.generics.params[2].span(), msg))
}
let has_instance = if item.generics.params.first().is_some() {
helper::check_config_def_gen(&item.generics, item.ident.span())?;
true
} else {
false
};
let mut has_event_type = false;
let mut consts_metadata = vec![];
for trait_item in &mut item.items {
has_event_type =
has_event_type || check_event_type(frame_system, trait_item, has_instance)?;
let type_attrs_const: Vec<TypeAttrConst> = helper::take_item_pallet_attrs(trait_item)?;
if type_attrs_const.len() > 1 {
let msg = "Invalid attribute in pallet::config, only one attribute is expected";
return Err(syn::Error::new(type_attrs_const[1].span(), msg))
}
if type_attrs_const.len() == 1 {
match trait_item {
syn::TraitItem::Type(ref type_) => {
let constant = ConstMetadataDef::try_from(type_)?;
consts_metadata.push(constant);
},
_ => {
let msg =
"Invalid pallet::constant in pallet::config, expected type trait \
item";
return Err(syn::Error::new(trait_item.span(), msg))
},
}
}
}
let attr: Option<DisableFrameSystemSupertraitCheck> =
helper::take_first_item_pallet_attr(&mut item.attrs)?;
let disable_system_supertrait_check = attr.is_some();
let has_frame_system_supertrait = item.supertraits.iter().any(|s| {
syn::parse2::<ConfigBoundParse>(s.to_token_stream())
.map_or(false, |b| b.0 == *frame_system)
});
if !has_frame_system_supertrait && !disable_system_supertrait_check {
let found = if item.supertraits.is_empty() {
"none".to_string()
} else {
let mut found = item.supertraits.iter().fold(String::new(), |acc, s| {
format!("{}`{}`, ", acc, quote::quote!(#s).to_string())
});
found.pop();
found.pop();
found
};
let msg = format!(
"Invalid pallet::trait, expected explicit `{}::Config` as supertrait, \
found {}. \
(try `pub trait Config: frame_system::Config {{ ...` or \
`pub trait Config<I: 'static>: frame_system::Config {{ ...`). \
To disable this check, use `#[pallet::disable_frame_system_supertrait_check]`",
frame_system, found,
);
return Err(syn::Error::new(item.span(), msg))
}
Ok(Self { index, has_instance, consts_metadata, has_event_type, where_clause, attr_span })
}
}