Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive forward attribute #314

Merged
merged 7 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion concordium-std/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

- Support adding attribute `#[concordium(repr(u))]` for enum types, where `u` is either `u8` or `u16`. Setting this changes the integer serialization used for the variant tags in derive macros such as `Serial`, `Deserial`, `DeserialWithState` and `SchemaType`.
- Support adding attribute `#[concordium(tag = n)]` for enum variants, where `n` is some unsigned integer literal. Setting this attribute on a variant overrides the tag used in derive macros such as `Serial`, `Deserial`, `DeserialWithState` and `SchemaType`. Note that setting `#[concordium(repr(u*))]` is required when using this attribute.

- Support adding `#[concordium(forward = n)]`, for enum variants, where `n` is either an unsigned integer literal, `cis2_events`, `cis3_events`, `cis4_events` or an array of the same options.
Setting this attribute on a variant overrides the (de)serialization to flatten with the (de)serialization of the inner field when using derive macros such as `Serial`, `Deserial`, `DeserialWithState` and `SchemaType`.
Note that setting `#[concordium(repr(u*))]` is required when using this attribute.

## concordium-std 7.0.0 (2023-06-16)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Ensure `derive(DeserialWithState)` fails when 'forward' attributes are
//! colliding.
use concordium_std::*;

#[derive(DeserialWithState)]
#[concordium(state_parameter = "S", repr(u8))]
enum Count<S> {
One {
field: StateSet<u32, S>,
},
#[concordium(forward = [5, 6])]
Two(Inner),

#[concordium(forward = 5)]
Three(Inner),
}

#[derive(Deserial)]
#[concordium(repr(u8))]
enum Inner {
#[concordium(tag = 5)]
Alpha {
balance: u32,
},
#[concordium(tag = 6)]
Beta(u16),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: The tag of 'Three' collides with the tag of 'Two'.
--> tests/derive-deserial-with-state/fail-colliding-forward.rs:14:28
|
14 | #[concordium(forward = 5)]
| ^

error: The tag of 'Two' collides with the tag of 'Three'.
--> tests/derive-deserial-with-state/fail-colliding-forward.rs:11:29
|
11 | #[concordium(forward = [5, 6])]
| ^
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Ensure `derive(DeserialWithState)` fails when 'forward' and 'tag' attributes
//! are colliding.
use concordium_std::*;

#[derive(DeserialWithState)]
#[concordium(state_parameter = "S", repr(u8))]
enum Count<S> {
One {
field: StateSet<u32, S>,
},
#[concordium(forward = [5, 6])]
Two(Inner),

#[concordium(tag = 5)]
Three(Inner),
}

#[derive(Deserial)]
#[concordium(repr(u8))]
enum Inner {
#[concordium(tag = 5)]
Alpha {
balance: u32,
},
#[concordium(tag = 6)]
Beta(u16),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: The tag of 'Three' collides with the tag of 'Two'.
--> tests/derive-deserial-with-state/fail-colliding-tag-forward.rs:14:18
|
14 | #[concordium(tag = 5)]
| ^^^

error: The tag of 'Two' collides with the tag of 'Three'.
--> tests/derive-deserial-with-state/fail-colliding-tag-forward.rs:11:29
|
11 | #[concordium(forward = [5, 6])]
| ^
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! Ensure `derive(DeserialWithState)` fails when a 'forward' attribute collides
//! with the default tag for another variant.
use concordium_std::*;

#[derive(DeserialWithState)]
#[concordium(state_parameter = "S", repr(u8))]
enum Count<S> {
One {
field: StateSet<u32, S>,
},
#[concordium(forward = [0, 6])]
Two(Inner),
}

#[derive(Deserial)]
#[concordium(repr(u8))]
enum Inner {
Alpha {
balance: u32,
},
#[concordium(tag = 6)]
Beta(u16),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: The tag of 'Two' collides with the tag of 'One'.
--> tests/derive-deserial-with-state/fail-forward-collide-with-inferred.rs:11:29
|
11 | #[concordium(forward = [0, 6])]
| ^

error: The tag of 'One' collides with the tag of 'Two'.
--> tests/derive-deserial-with-state/fail-forward-collide-with-inferred.rs:8:5
|
8 | One {
| ^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Ensure `derive(DeserialWithState)` fails when a 'forward' attribute is
//! greater than what 'repr(u8)' can represent.
use concordium_std::*;

#[derive(DeserialWithState)]
#[concordium(state_parameter = "S", repr(u8))]
enum Count<S> {
One {
field: StateSet<u32, S>,
},
#[concordium(forward = [500, 6])]
Two(Inner),
}

#[derive(Deserial)]
#[concordium(repr(u16))]
enum Inner {
#[concordium(tag = 500)]
Alpha {
balance: u32,
},
#[concordium(tag = 6)]
Beta(u16),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: 'forward' attribute tag value of 500 cannot be represented by the type u8 set in 'repr(u8)'
--> tests/derive-deserial-with-state/fail-forward-gt-repr.rs:11:29
|
11 | #[concordium(forward = [500, 6])]
| ^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Ensure `derive(DeserialWithState)` fails when using 'forward' attribute
//! without a 'repr' attribute.
use concordium_std::*;

#[derive(DeserialWithState)]
#[concordium(state_parameter = "S")]
enum Count<S> {
One {
field: StateSet<u32, S>,
},
#[concordium(forward = [5, 6])]
Two(Inner),
}

#[derive(Deserial)]
#[concordium(repr(u8))]
enum Inner {
#[concordium(tag = 5)]
Alpha {
balance: u32,
},
#[concordium(tag = 6)]
Beta(u16),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: 'repr(..)' attribute must be set on the type when using 'forward' attribute
--> tests/derive-deserial-with-state/fail-forward-without-repr.rs:5:10
|
5 | #[derive(DeserialWithState)]
| ^^^^^^^^^^^^^^^^^
|
= note: this error originates in the derive macro `DeserialWithState` (in Nightly builds, run with -Z macro-backtrace for more info)
28 changes: 28 additions & 0 deletions concordium-std/tests/derive-deserial/fail-colliding-forward.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Ensure `derive(Deserial)` fails when 'forward' attributes are colliding.
use concordium_std::*;

#[derive(Deserial)]
#[concordium(repr(u8))]
enum Count {
One {
field: u32,
},
#[concordium(forward = [5, 6])]
Two(Inner),

#[concordium(forward = 5)]
Three(Inner),
}

#[derive(Deserial)]
#[concordium(repr(u8))]
enum Inner {
#[concordium(tag = 5)]
Alpha {
balance: u32,
},
#[concordium(tag = 6)]
Beta(u16),
}

fn main() {}
11 changes: 11 additions & 0 deletions concordium-std/tests/derive-deserial/fail-colliding-forward.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: The tag of 'Three' collides with the tag of 'Two'.
--> tests/derive-deserial/fail-colliding-forward.rs:13:28
|
13 | #[concordium(forward = 5)]
| ^

error: The tag of 'Two' collides with the tag of 'Three'.
--> tests/derive-deserial/fail-colliding-forward.rs:10:29
|
10 | #[concordium(forward = [5, 6])]
| ^
29 changes: 29 additions & 0 deletions concordium-std/tests/derive-deserial/fail-colliding-tag-forward.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Ensure `derive(Deserial)` fails when 'forward' and 'tag' attributes are
//! colliding.
use concordium_std::*;

#[derive(Deserial)]
#[concordium(repr(u8))]
enum Count {
One {
field: u32,
},
#[concordium(forward = [5, 6])]
Two(Inner),

#[concordium(tag = 5)]
Three(Inner),
}

#[derive(Deserial)]
#[concordium(repr(u8))]
enum Inner {
#[concordium(tag = 5)]
Alpha {
balance: u32,
},
#[concordium(tag = 6)]
Beta(u16),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: The tag of 'Three' collides with the tag of 'Two'.
--> tests/derive-deserial/fail-colliding-tag-forward.rs:14:18
|
14 | #[concordium(tag = 5)]
| ^^^

error: The tag of 'Two' collides with the tag of 'Three'.
--> tests/derive-deserial/fail-colliding-tag-forward.rs:11:29
|
11 | #[concordium(forward = [5, 6])]
| ^
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! Ensure `derive(Deserial)` fails when a 'forward' attribute collides with the
//! default tag for another variant.
use concordium_std::*;

#[derive(Deserial)]
#[concordium(repr(u8))]
enum Count {
One {
field: u32,
},
#[concordium(forward = [0, 6])]
Two(Inner),
}

#[derive(Deserial)]
#[concordium(repr(u8))]
enum Inner {
Alpha {
balance: u32,
},
#[concordium(tag = 6)]
Beta(u16),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: The tag of 'Two' collides with the tag of 'One'.
--> tests/derive-deserial/fail-forward-collide-with-inferred.rs:11:29
|
11 | #[concordium(forward = [0, 6])]
| ^

error: The tag of 'One' collides with the tag of 'Two'.
--> tests/derive-deserial/fail-forward-collide-with-inferred.rs:8:5
|
8 | One {
| ^^^
26 changes: 26 additions & 0 deletions concordium-std/tests/derive-deserial/fail-forward-gt-repr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Ensure `derive(Deserial)` fails when a 'forward' attribute is greater than
//! what 'repr(u8)' can represent.
use concordium_std::*;

#[derive(Deserial)]
#[concordium(repr(u8))]
enum Count {
One {
field: u32,
},
#[concordium(forward = [500, 6])]
Two(Inner),
}

#[derive(Deserial)]
#[concordium(repr(u16))]
enum Inner {
#[concordium(tag = 500)]
Alpha {
balance: u32,
},
#[concordium(tag = 6)]
Beta(u16),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: 'forward' attribute tag value of 500 cannot be represented by the type u8 set in 'repr(u8)'
--> tests/derive-deserial/fail-forward-gt-repr.rs:11:29
|
11 | #[concordium(forward = [500, 6])]
| ^^^
25 changes: 25 additions & 0 deletions concordium-std/tests/derive-deserial/fail-forward-without-repr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! Ensure `derive(Deserial)` fails when using 'forward' attribute without a
//! 'repr' attribute.
use concordium_std::*;

#[derive(Deserial)]
enum Count {
One {
field: u32,
},
#[concordium(forward = [5, 6])]
Two(Inner),
}

#[derive(Deserial)]
#[concordium(repr(u8))]
enum Inner {
#[concordium(tag = 5)]
Alpha {
balance: u32,
},
#[concordium(tag = 6)]
Beta(u16),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: 'repr(..)' attribute must be set on the type when using 'forward' attribute
--> tests/derive-deserial/fail-forward-without-repr.rs:5:10
|
5 | #[derive(Deserial)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Deserial` (in Nightly builds, run with -Z macro-backtrace for more info)
Loading
Loading