-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Result<Self, Error>
return type for constructors (#1446)
* constructors return Err abort transaction + started example * return type parser * add output type to dispatchable constructor * codegen adds Result to return type * add type cheking * fix CALLABLE type * add comments for refactoring * ugly solution for failable contracts * avoid unnecessary error check for constructors * avoids encoding storage, adds demo example * allows differents syntax format of result return type * cargo fmt * add changes to CHANGELOG.md * add tests to the example * add UI tests * return comment to dns example * fix formatting in UI examples * fix tests and docs * apply formatting suggestions * change example and UI tests for more clarity * add imports for UI test * refactor execution.rs * remove syntactical check for constructors return type * UI test for type aliases * LLVM optimisation and refactoring * updated docs * refactoring and moving feature demo to mother contract * remove cargo-contract install in docker
- Loading branch information
German
authored
Oct 27, 2022
1 parent
c083c8c
commit 3713037
Showing
18 changed files
with
338 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
crates/ink/tests/ui/contract/fail/constructor-return-result-invalid.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#[ink::contract] | ||
mod contract { | ||
#[ink(storage)] | ||
pub struct Contract {} | ||
|
||
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)] | ||
#[cfg_attr(feature = "std", derive(::scale_info::TypeInfo))] | ||
pub enum Error { | ||
Foo, | ||
} | ||
|
||
impl Contract { | ||
#[ink(constructor)] | ||
pub fn constructor() -> Result<u8, Error> { | ||
Ok(5_u8) | ||
} | ||
|
||
#[ink(message)] | ||
pub fn message(&self) {} | ||
} | ||
} | ||
|
||
fn main() {} |
14 changes: 14 additions & 0 deletions
14
crates/ink/tests/ui/contract/fail/constructor-return-result-invalid.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0277]: the trait bound `codegen::dispatch::execution::private::Seal<Result<u8, contract::Error>>: codegen::dispatch::execution::ConstructorReturnType<Contract>` is not satisfied | ||
--> tests/ui/contract/fail/constructor-return-result-invalid.rs:14:9 | ||
| | ||
14 | pub fn constructor() -> Result<u8, Error> { | ||
| ^^^ the trait `codegen::dispatch::execution::ConstructorReturnType<Contract>` is not implemented for `codegen::dispatch::execution::private::Seal<Result<u8, contract::Error>>` | ||
| | ||
= help: the following other types implement trait `codegen::dispatch::execution::ConstructorReturnType<C>`: | ||
codegen::dispatch::execution::private::Seal<C> | ||
codegen::dispatch::execution::private::Seal<Result<C, E>> | ||
note: required by a bound in `execute_constructor` | ||
--> src/codegen/dispatch/execution.rs | ||
| | ||
| private::Seal<R>: ConstructorReturnType<Contract>, | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `execute_constructor` |
22 changes: 22 additions & 0 deletions
22
crates/ink/tests/ui/contract/fail/constructor-return-result-non-codec-error.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#[ink::contract] | ||
mod contract { | ||
#[ink(storage)] | ||
pub struct Contract {} | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum Error { | ||
Foo, | ||
} | ||
|
||
impl Contract { | ||
#[ink(constructor)] | ||
pub fn constructor() -> Result<Self, Error> { | ||
Ok(Self {}) | ||
} | ||
|
||
#[ink(message)] | ||
pub fn message(&self) {} | ||
} | ||
} | ||
|
||
fn main() {} |
22 changes: 22 additions & 0 deletions
22
crates/ink/tests/ui/contract/fail/constructor-return-result-non-codec-error.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error[E0277]: the trait bound `contract::Error: WrapperTypeEncode` is not satisfied | ||
--> tests/ui/contract/fail/constructor-return-result-non-codec-error.rs:13:9 | ||
| | ||
13 | pub fn constructor() -> Result<Self, Error> { | ||
| ^^^ the trait `WrapperTypeEncode` is not implemented for `contract::Error` | ||
| | ||
= help: the following other types implement trait `WrapperTypeEncode`: | ||
&T | ||
&mut T | ||
Arc<T> | ||
Box<T> | ||
Cow<'a, T> | ||
Rc<T> | ||
String | ||
Vec<T> | ||
parity_scale_codec::Ref<'a, T, U> | ||
= note: required because of the requirements on the impl of `Encode` for `contract::Error` | ||
note: required by a bound in `execute_constructor` | ||
--> src/codegen/dispatch/execution.rs | ||
| | ||
| <private::Seal<R> as ConstructorReturnType<Contract>>::Error: Encode, | ||
| ^^^^^^ required by this bound in `execute_constructor` |
Oops, something went wrong.