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

proposal: cmd/vet: ensure errors implement Is, As, and Unwrap anonymous interfaces #40356

carnott-snap opened this issue Jul 22, 2020 · 2 comments


Copy link

@carnott-snap carnott-snap commented Jul 22, 2020

The interfaces for errors.Is and errors.As seem stable and complete. Unfortunately, it requires reading through the godoc to ensure that your custom error type correctly implements interface{ As(interface{}) bool }, interface{ Is(error) bool }, or interface{ Unwrap() error }. This can lead to implementation bugs that fail pathologically.

Instead of exposing these symbols, as was proposed in #39539, we can add a vet check to ensure that errors with Is, As, or Unwrap methods implement the correct interfaces. This is not a requirement that all errors implement Is, As, and Unwrap, but simply that if the symbol exists, the syntax matches.

Technically, this is not required, as you can use an anonymous type to perform this check at compile time. But this is ungainly, not automatic, and fails to the same typographical issues due to its distributed implementation:

var _, _, _,_ = (interface{ As(interface{}) bool })(myError{}), (interface{ Is(error) bool })(myError{}), (interface{ Unwrap() error })(myError{}), error(myError{})
Copy link

@carnott-snap carnott-snap commented Aug 6, 2020

@ianlancetaylor: can you add this to the proposals project?

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals Aug 6, 2020
Copy link

@ianlancetaylor ianlancetaylor commented Aug 6, 2020

@carnott-snap Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants