-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove need for Copy on error enums (#1292)
### What Remove need for Copy/Clone/etc on error enums and int enums. ### Why Less compiler errors for folks just getting started. The generated code currently requires that error enums impl the Copy trait because they use a copy within. That copy is unnecessary and this change removes it, removing the need for Copy to be derived on error enums. I was confident that the copy had been put there as an optimisation, but when I looked into the code that was relying on it I found not optimisation was in use. When I tried to add the optimisation, I couldn't find a contract size difference. A while ago we in #1293 added better error messages for needing copy, which was a good fix at the time, I just think we can remove the need for this all together. Close #630
- Loading branch information
1 parent
8fc9f53
commit 627e164
Showing
8 changed files
with
464 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::{self as soroban_sdk}; | ||
use soroban_sdk::{contract, contracterror, contractimpl, contracttype, Env}; | ||
|
||
#[contract] | ||
pub struct Contract; | ||
|
||
#[contracttype] | ||
pub enum Flag { | ||
A = 0, | ||
B = 1, | ||
} | ||
|
||
#[contracterror] | ||
pub enum Error { | ||
AnError = 1, | ||
} | ||
|
||
#[contractimpl] | ||
impl Contract { | ||
pub fn f(flag: Flag) -> Result<(), Error> { | ||
match flag { | ||
Flag::A => Ok(()), | ||
Flag::B => Err(Error::AnError), | ||
} | ||
} | ||
} | ||
|
||
// The assertions in the following tests intentionally don't use assert_eq, and | ||
// unwraps the object using Rust's pattern matching, so that no derives like | ||
// Debug, Eq, PartialEq, etc are required on the error enum type because this | ||
// test serves to ensure that none of the generated code for the error type or | ||
// the client that uses it is dependent on derives that may or may not be | ||
// available. | ||
|
||
#[test] | ||
fn test_success() { | ||
let env = Env::default(); | ||
let contract_id = env.register(Contract, ()); | ||
let client = ContractClient::new(&env, &contract_id); | ||
|
||
// See comment above about why this assertion is a match. | ||
let Ok(Ok(())) = client.try_f(&Flag::A) else { | ||
panic!("unexpected value returned"); | ||
}; | ||
} | ||
|
||
#[test] | ||
fn test_error() { | ||
let env = Env::default(); | ||
let contract_id = env.register(Contract, ()); | ||
let client = ContractClient::new(&env, &contract_id); | ||
|
||
// See comment above about why this assertion is a match. | ||
let Err(Ok(Error::AnError)) = client.try_f(&Flag::B) else { | ||
panic!("unexpected value returned"); | ||
}; | ||
} |
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
216 changes: 216 additions & 0 deletions
216
soroban-sdk/test_snapshots/tests/contract_udt_enum_error/test_error.1.json
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,216 @@ | ||
{ | ||
"generators": { | ||
"address": 1, | ||
"nonce": 0 | ||
}, | ||
"auth": [ | ||
[], | ||
[] | ||
], | ||
"ledger": { | ||
"protocol_version": 22, | ||
"sequence_number": 0, | ||
"timestamp": 0, | ||
"network_id": "0000000000000000000000000000000000000000000000000000000000000000", | ||
"base_reserve": 0, | ||
"min_persistent_entry_ttl": 4096, | ||
"min_temp_entry_ttl": 16, | ||
"max_entry_ttl": 6312000, | ||
"ledger_entries": [ | ||
[ | ||
{ | ||
"contract_data": { | ||
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM", | ||
"key": "ledger_key_contract_instance", | ||
"durability": "persistent" | ||
} | ||
}, | ||
[ | ||
{ | ||
"last_modified_ledger_seq": 0, | ||
"data": { | ||
"contract_data": { | ||
"ext": "v0", | ||
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM", | ||
"key": "ledger_key_contract_instance", | ||
"durability": "persistent", | ||
"val": { | ||
"contract_instance": { | ||
"executable": { | ||
"wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" | ||
}, | ||
"storage": null | ||
} | ||
} | ||
} | ||
}, | ||
"ext": "v0" | ||
}, | ||
4095 | ||
] | ||
], | ||
[ | ||
{ | ||
"contract_code": { | ||
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" | ||
} | ||
}, | ||
[ | ||
{ | ||
"last_modified_ledger_seq": 0, | ||
"data": { | ||
"contract_code": { | ||
"ext": "v0", | ||
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | ||
"code": "" | ||
} | ||
}, | ||
"ext": "v0" | ||
}, | ||
4095 | ||
] | ||
] | ||
] | ||
}, | ||
"events": [ | ||
{ | ||
"event": { | ||
"ext": "v0", | ||
"contract_id": null, | ||
"type_": "diagnostic", | ||
"body": { | ||
"v0": { | ||
"topics": [ | ||
{ | ||
"symbol": "fn_call" | ||
}, | ||
{ | ||
"bytes": "0000000000000000000000000000000000000000000000000000000000000001" | ||
}, | ||
{ | ||
"symbol": "__constructor" | ||
} | ||
], | ||
"data": "void" | ||
} | ||
} | ||
}, | ||
"failed_call": false | ||
}, | ||
{ | ||
"event": { | ||
"ext": "v0", | ||
"contract_id": null, | ||
"type_": "diagnostic", | ||
"body": { | ||
"v0": { | ||
"topics": [ | ||
{ | ||
"symbol": "fn_call" | ||
}, | ||
{ | ||
"bytes": "0000000000000000000000000000000000000000000000000000000000000001" | ||
}, | ||
{ | ||
"symbol": "f" | ||
} | ||
], | ||
"data": { | ||
"u32": 1 | ||
} | ||
} | ||
} | ||
}, | ||
"failed_call": false | ||
}, | ||
{ | ||
"event": { | ||
"ext": "v0", | ||
"contract_id": "0000000000000000000000000000000000000000000000000000000000000001", | ||
"type_": "diagnostic", | ||
"body": { | ||
"v0": { | ||
"topics": [ | ||
{ | ||
"symbol": "fn_return" | ||
}, | ||
{ | ||
"symbol": "f" | ||
} | ||
], | ||
"data": { | ||
"error": { | ||
"contract": 1 | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"failed_call": true | ||
}, | ||
{ | ||
"event": { | ||
"ext": "v0", | ||
"contract_id": "0000000000000000000000000000000000000000000000000000000000000001", | ||
"type_": "diagnostic", | ||
"body": { | ||
"v0": { | ||
"topics": [ | ||
{ | ||
"symbol": "error" | ||
}, | ||
{ | ||
"error": { | ||
"contract": 1 | ||
} | ||
} | ||
], | ||
"data": { | ||
"string": "escalating Ok(ScErrorType::Contract) frame-exit to Err" | ||
} | ||
} | ||
} | ||
}, | ||
"failed_call": true | ||
}, | ||
{ | ||
"event": { | ||
"ext": "v0", | ||
"contract_id": null, | ||
"type_": "diagnostic", | ||
"body": { | ||
"v0": { | ||
"topics": [ | ||
{ | ||
"symbol": "error" | ||
}, | ||
{ | ||
"error": { | ||
"contract": 1 | ||
} | ||
} | ||
], | ||
"data": { | ||
"vec": [ | ||
{ | ||
"string": "contract try_call failed" | ||
}, | ||
{ | ||
"symbol": "f" | ||
}, | ||
{ | ||
"vec": [ | ||
{ | ||
"u32": 1 | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
"failed_call": false | ||
} | ||
] | ||
} |
Oops, something went wrong.