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

Allow error type conversions in ensure! and ensure_eq! #474

Merged
merged 2 commits into from
Oct 7, 2021
Merged
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
41 changes: 37 additions & 4 deletions packages/cw0/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
macro_rules! ensure {
($cond:expr, $e:expr) => {
if !($cond) {
return Err($e);
return Err(std::convert::From::from($e));
}
};
}
Expand Down Expand Up @@ -43,9 +43,7 @@ macro_rules! fail_if {
#[macro_export]
macro_rules! ensure_eq {
($a:expr, $b:expr, $e:expr) => {
if $a != $b {
return Err($e);
}
ensure!($a == $b, $e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better reuse of code 👍

};
}

Expand All @@ -55,6 +53,19 @@ mod test {

#[test]
fn ensure_works() {
fn check(a: usize, b: usize) -> Result<(), StdError> {
ensure!(a == b, StdError::generic_err("foobar"));
Ok(())
}

let err = check(5, 6).unwrap_err();
assert!(matches!(err, StdError::GenericErr { .. }));

check(5, 5).unwrap();
}

#[test]
fn ensure_can_infer_error_type() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to understand why this works. How does Rust infer the error type in that case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woah, this compiles???

I guess it uses the matches! segment to determine it is expected to compare with StdError, thus setting the Result<_, StdError>. But this compiler is starting to get a bit too smart....

let check = |a, b| {
ensure!(a == b, StdError::generic_err("foobar"));
Ok(())
Expand All @@ -66,6 +77,28 @@ mod test {
check(5, 5).unwrap();
}

#[test]
fn ensure_can_convert_into() {
#[derive(Debug)]
struct ContractError;

impl From<StdError> for ContractError {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice minimal case 👍

fn from(_original: StdError) -> Self {
ContractError
}
}

fn check(a: usize, b: usize) -> Result<(), ContractError> {
Copy link
Member

@ethanfrey ethanfrey Oct 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, here is the real test of the compiler's intelligence:

If you remove -> Result<(), ContractError>, will the compiler infer it from line 97?

Copy link
Member Author

@webmaster128 webmaster128 Oct 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An fn always needs a specific return type. Unset means (). So this is more realistic that the closure test case.

But yes, if you turn it into a closure and remove the return type, compiler can infer it.

ensure!(a == b, StdError::generic_err("foobar"));
Ok(())
}

let err = check(5, 6).unwrap_err();
assert!(matches!(err, ContractError));

check(5, 5).unwrap();
}

#[test]
fn fail_if_works() {
let check = |a, b| {
Expand Down