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

Improve errors #601

Closed
wants to merge 2 commits into from
Closed

Conversation

tcharding
Copy link
Member

Patch 1: Remove derives from error types

Now that we have `matches` because of the recent MSRV bump we can
ergonomically test error types without using `PartialEq`/`Eq`.

Remove all derives from error types except `Debug`.

Patch 2: Trivial refactor

Now that we have `matches` because of the recent MSRV bump we can
ergonomically test error types without using `PartialEq`/`Eq`.

Remove all derives from error types except `Debug`.
Do trivial refactor to reduce the clutter in the `source` method.

Refactor only, no logic changes.
assert_eq!(full.recover_ecdsa(&msg, &sigr), Ok(pk));
let vrfy_res = vrfy.recover_ecdsa(&msg, &sigr);
let full_res = full.recover_ecdsa(&msg, &sigr);
assert!(matches!(vrfy_res, full_res));
Copy link
Member

Choose a reason for hiding this comment

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

In 645238f:

This is not correct -- you can't use matches! on two variables like this; the result is to bind vrfy_res to full_res and then check that the binding succeeded (which it always will because there are no further restrictions). See https://stackoverflow.com/questions/72537643/apparent-unused-variable-in-match-statement

In general I'm a bit suspicious of these allow(unused_variables) lines because I think they're masking similar mistakes .... and in retrospect we should revisit what's going on in rust-bitcoin with these.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, I need to read the matches code then before using it willy-nilly. Just had a thought, perhaps we could do this on error types

#[derive(Debug)]
#[non_exhaustive]
#[allow(missing_copy_implementations)]
#[cfg_attr(test, derive(Copy, PartialEq, Eq, Clone))]

Then all the test changes in this PR are not required and matches! use goes away.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are we removing PartialEq in secp256k1? The biggest reason to remove it is that we may add std::io::Error inside one day but I don't expect this to happen in this crate. Same with Clone. I'd avoid Copy since adding something that allocates is quite plausible.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that's a good point -- I had half-forgotten that the reason for "error types don't impl any traits" is that io::Error tends to poison our types. But you're right that we're extremely unlikely to ever have I/O errors from this pure-math crate.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I thought that the "great error cleanup", across the org, meant that all errors should only derive Debug? Are you guys saying that for every error type I/we have to think about what traits to derive? That sounds like unnecessary work, can't we just decide on a minimum set and use that (which is what I thought we did and how we got to only deriving Debug)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just lazy typing by me, I should have written PartialEq/Eq :)

Copy link
Member Author

Choose a reason for hiding this comment

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

oh I didn't type it, lazy typing by @Kixunil (I assume) - double smiley face.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not necessarily. We may avoid promising Eq to avoid future breakage like what happened with LockTime. But I'm not entirely sure about this one, there's a good chance Eq is just fine.

Copy link
Member

Choose a reason for hiding this comment

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

The LockTime breakage was about Ord. Having a type that is PartialEq but not Eq is a pretty obscure use-case, and I'm doubtful that crypto errors will meet it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indeed, I'm very open to having Eq.

@tcharding
Copy link
Member Author

Could be closed, setting as draft so I don't loose the discussion and can re-visit what to do with errors in secp (if anything).

@tcharding tcharding marked this pull request as draft April 20, 2023 22:26
@tcharding
Copy link
Member Author

This will be redundant after #635

@tcharding tcharding closed this Aug 8, 2023
@tcharding tcharding deleted the 04-19-error branch October 9, 2023 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants