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

Allow pallet error enum variants to contain fields #10242

Merged
merged 83 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
e569450
Allow pallet errors to contain at most one field
KiChjang Nov 11, 2021
c4f7b51
Update docs on pallet::error
KiChjang Nov 11, 2021
788ea42
Reword documentation
KiChjang Nov 11, 2021
0b70512
cargo fmt
KiChjang Nov 11, 2021
d3e5c7c
Introduce CompactPalletError trait and require #[pallet::error] field…
KiChjang Nov 18, 2021
e8f6714
cargo fmt
KiChjang Nov 18, 2021
815a616
Do not assume tuple variants
KiChjang Nov 19, 2021
78ff9af
Add CompactPalletError derive macro
KiChjang Nov 19, 2021
e86c2d9
Check for error type compactness in construct_runtime
KiChjang Nov 21, 2021
2ba5c5b
cargo fmt
KiChjang Nov 21, 2021
217e1a4
Derive CompactPalletError instead of implementing it directly during …
KiChjang Nov 23, 2021
3a399b6
Implement CompactPalletError on OptionBool instead of Option<bool>
KiChjang Nov 23, 2021
a583025
Check for type idents instead of variant ident
KiChjang Nov 24, 2021
e68ba71
Add doc comments for ErrorCompactnessTest
KiChjang Nov 24, 2021
ed643ff
Add an trait implementation of ErrorCompactnessTest for ()
KiChjang Dec 1, 2021
dcddc79
Merge remote-tracking branch 'origin/master' into kckyeung/nested-pal…
KiChjang Dec 4, 2021
3913e85
Convert the error field of DispatchError to a 4-element byte array
KiChjang Dec 4, 2021
26b0adf
Add static check for pallet error size
KiChjang Dec 7, 2021
82c29a9
Rename to MAX_PALLET_ERROR_ENCODED_SIZE
KiChjang Dec 8, 2021
a1a0c32
Remove ErrorCompactnessTest trait
KiChjang Dec 8, 2021
0fb5860
Remove check_compactness
KiChjang Dec 8, 2021
69753e9
Return only the most significant byte when constructing a custom Inva…
KiChjang Dec 8, 2021
c57ccb6
Rename CompactPalletError to PalletError
KiChjang Dec 8, 2021
f6f8be5
Use counter to generate unique idents for assert macros
KiChjang Dec 8, 2021
9b92379
Make declarative pallet macros compile with pallet error size checks
KiChjang Dec 14, 2021
b984433
Remove unused doc comment
KiChjang Dec 14, 2021
2fb09fb
Try and fix build errors
KiChjang Dec 14, 2021
adceb34
Fix build errors
KiChjang Dec 21, 2021
b018d42
Add macro_use for some test modules
KiChjang Dec 21, 2021
56ff09d
Test fix
KiChjang Dec 21, 2021
29841d9
Fix compilation errors
KiChjang Dec 21, 2021
1ca9680
Remove unneeded #[macro_use]
KiChjang Dec 21, 2021
cf1cbd3
Merge remote-tracking branch 'origin/master' into kckyeung/nested-pal…
KiChjang Dec 21, 2021
b7c2b9f
Resolve import ambiguity
KiChjang Dec 21, 2021
8783da5
Make path to pallet Error enum more specific
KiChjang Dec 21, 2021
1f991ff
Fix test expectation
KiChjang Dec 21, 2021
d7e3597
Disambiguate imports
KiChjang Dec 21, 2021
aea23e0
Fix test expectations
KiChjang Dec 21, 2021
1a4bcdf
Revert appending pallet module name to path
KiChjang Dec 21, 2021
d2e42ef
Rename bags_list::list::Error to BagError
KiChjang Dec 21, 2021
9ab1b56
Fixes
KiChjang Dec 21, 2021
1be3227
Fixes
KiChjang Dec 21, 2021
0342859
Fixes
KiChjang Dec 21, 2021
066ec5f
Fix test expectations
KiChjang Dec 21, 2021
1f3ab67
Fix test expectation
KiChjang Dec 21, 2021
b45ca96
Add more implementations for PalletError
KiChjang Dec 21, 2021
981a29f
Lift the 1-field requirement for nested pallet errors
KiChjang Dec 22, 2021
0ef2812
Fix UI test expectation
KiChjang Dec 22, 2021
fb1b32b
Remove PalletError impl for OptionBool
KiChjang Dec 22, 2021
fb73b33
Use saturating operations
KiChjang Dec 23, 2021
8e8a27a
cargo fmt
KiChjang Dec 23, 2021
2a54736
Delete obsolete test
KiChjang Dec 23, 2021
a250788
Fix test expectation
KiChjang Dec 24, 2021
7d80dae
Merge remote-tracking branch 'origin/master' into kckyeung/nested-pal…
KiChjang Jan 15, 2022
c44e6e4
Try and use assert macro in const context
KiChjang Jan 19, 2022
cc06206
Pull out the pallet error size check macro
KiChjang Feb 4, 2022
c037c6d
Merge remote-tracking branch 'origin/master' into kckyeung/nested-pal…
KiChjang Feb 4, 2022
e4c3f47
Fix UI test for const assertion
KiChjang Feb 4, 2022
fa0e155
cargo fmt
KiChjang Feb 4, 2022
29e3df1
Apply clippy suggestion
KiChjang Feb 4, 2022
a8b6214
Fix doc comment
KiChjang Feb 4, 2022
cca5ed6
Docs for create_tt_return_macro
KiChjang Feb 4, 2022
52a0a73
Ensure TryInto is imported in earlier Rust editions
KiChjang Feb 7, 2022
ece305e
Apply suggestions from code review
KiChjang Feb 11, 2022
29a131a
Fix up comments and names
KiChjang Feb 12, 2022
9cdf1aa
Implement PalletError for Never
KiChjang Feb 12, 2022
5070335
cargo fmt
KiChjang Feb 12, 2022
ac1f198
Don't compile example code
KiChjang Feb 12, 2022
811e2f2
Bump API version for block builder
KiChjang Feb 16, 2022
7595123
Factor in codec attributes while derving PalletError
KiChjang Feb 17, 2022
89abef7
Rename module and fix unit test
KiChjang Feb 17, 2022
161fb88
Add missing attribute
KiChjang Feb 18, 2022
babebfc
Check API version and convert ApplyExtrinsicResult accordingly
KiChjang Feb 18, 2022
80ed571
Merge remote-tracking branch 'origin/master' into kckyeung/nested-pal…
KiChjang Feb 18, 2022
7fed725
Rename BagError to ListError
KiChjang Mar 17, 2022
8d25a2f
Merge remote-tracking branch 'origin/master' into kckyeung/nested-pal…
KiChjang Mar 20, 2022
9fef2e1
Use codec crate re-exported from frame support
KiChjang Mar 20, 2022
ddc2cce
Add links to types mentioned in doc comments
KiChjang Mar 20, 2022
23608e7
cargo fmt
KiChjang Mar 20, 2022
9d0dfa7
cargo fmt
KiChjang Mar 20, 2022
966b177
Merge branch 'master' into kckyeung/nested-pallet-error-enum
KiChjang Mar 21, 2022
2b3b730
Re-add attribute for hidden docs
KiChjang Mar 21, 2022
f4157f4
Merge remote-tracking branch 'origin/master' into kckyeung/nested-pal…
KiChjang Mar 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions frame/support/procedural/src/pallet/parse/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use super::helper;
use frame_support_procedural_tools::get_doc_literals;
use quote::ToTokens;
use syn::spanned::Spanned;
use syn::{spanned::Spanned, Fields};

/// List of additional token to be used for parsing.
mod keyword {
Expand Down Expand Up @@ -70,12 +70,18 @@ impl ErrorDef {
.variants
.iter()
.map(|variant| {
if !matches!(variant.fields, syn::Fields::Unit) {
let msg = "Invalid pallet::error, unexpected fields, must be `Unit`";
return Err(syn::Error::new(variant.fields.span(), msg))
match &variant.fields {
Fields::Unit => {},
Fields::Named(f) if f.named.len() < 2 => {},
Fields::Unnamed(u) if u.unnamed.len() < 2 => {},
_ => {
let msg = "Invalid pallet::error, unexpected fields, must be `Unit` or \
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
contain only 1 field";
return Err(syn::Error::new(variant.fields.span(), msg))
},
}
if variant.discriminant.is_some() {
let msg = "Invalid pallet::error, unexpected discriminant, discriminant \
let msg = "Invalid pallet::error, unexpected discriminant, discriminants \
are not supported";
let span = variant.discriminant.as_ref().unwrap().0.span();
return Err(syn::Error::new(span, msg))
Expand Down
8 changes: 7 additions & 1 deletion frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1614,10 +1614,16 @@ pub mod pallet_prelude {
/// pub enum Error<T> {
/// /// $some_optional_doc
/// $SomeFieldLessVariant,
/// /// $some_more_optional_doc
/// $SomeSingleFieldVariant(FieldType),
/// ...
/// }
/// ```
/// I.e. a regular rust enum named `Error`, with generic `T` and fieldless variants.
/// I.e. a regular rust enum named `Error`, with generic `T` and fieldless or single-field
/// variants.
/// Any field in the enum variants must implement `scale_info::TypeInfo` in order to be
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
/// properly used in the metadata, and its encoded size should be as small as possible,
/// preferably 1 byte in size.
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
/// The generic `T` mustn't bound anything and where clause is not allowed. But bounds and
/// where clause shouldn't be needed for any usecase.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod pallet {

#[pallet::error]
pub enum Error<T> {
Tuple(u8, u8),
U8(u8),
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Invalid pallet::error, unexpected fields, must be `Unit` or contain only 1 field
--> $DIR/error_more_than_1_field.rs:20:8
|
20 | Tuple(u8, u8),
| ^^^^^^^^
5 changes: 0 additions & 5 deletions frame/support/test/tests/pallet_ui/error_no_fieldless.stderr

This file was deleted.