Skip to content

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

Closed
@carnott-snap

Description

@carnott-snap

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{})

Activity

added this to the Proposal milestone on Jul 22, 2020
carnott-snap

carnott-snap commented on Aug 6, 2020

@carnott-snap
Author

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

ianlancetaylor

ianlancetaylor commented on Aug 6, 2020

@ianlancetaylor
Contributor
urandom2

urandom2 commented on Apr 14, 2021

@urandom2
Contributor

did this get lost in the incoming stack?

ianlancetaylor

ianlancetaylor commented on Apr 14, 2021

@ianlancetaylor
Contributor

The incoming stack has more than 200 issues in it, so it's going to take a while to get through them all.

See https://github.com/golang/go/projects/1 .

rsc

rsc commented on Apr 21, 2021

@rsc
Contributor

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

neild

neild commented on Apr 21, 2021

@neild
Contributor

I don't object to cmd/vet checking the signature of As/Is/Unwrap methods for types that implement the error interface; it seems unlikely to have significant false positives. However, the best way to ensure an error implements these interfaces correctly is to write a test for the error, because a test will validate not only the method signature but the implementation.

gopherbot

gopherbot commented on Apr 22, 2021

@gopherbot
Contributor

Change https://golang.org/cl/312829 mentions this issue: passes/stdmethods: warn when an error has an unexpected Is, As, or Unwrap method.

rsc

rsc commented on Apr 28, 2021

@rsc
Contributor

Based on the discussion above, this proposal seems like a likely accept.
— rsc for the proposal review group

timothy-king

timothy-king commented on Apr 29, 2021

@timothy-king
Contributor

Tested golang.org/cl/312829 against a large Go codebase that uses Is, As and Unwrap. No findings so no false positives.

2 remaining items

modified the milestones: Proposal, Backlog on May 5, 2021
gopherbot

gopherbot commented on May 20, 2021

@gopherbot
Contributor

Change https://golang.org/cl/321389 mentions this issue: cmd: go get golang.org/x/tools/analysis@49064d23 && go mod vendor

modified the milestones: Backlog, Go1.17 on May 20, 2021
locked and limited conversation to collaborators on May 20, 2022
moved this to Accepted in Proposalson Aug 10, 2022
removed this from Proposalson Oct 19, 2022
added a commit that references this issue on Apr 29, 2024
baa934d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @neild@rsc@timothy-king@dmitshur@urandom2

        Issue actions

          cmd/vet: ensure errors implement Is, As, and Unwrap anonymous interfaces · Issue #40356 · golang/go