Go version

go version go1.24.4 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/jmymay/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/jmymay/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build251118401=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/jmymay/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/jmymay/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go-1.24'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/jmymay/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go-1.24/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.4'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Attempted to use errors.Is on nested error struct which is not a comparable type:

package main

import (
    "errors"
    "fmt"
)

type NestedError struct {
    Err error
}

func (e NestedError) Error() string {
    return e.Err.Error()
}

type ErrWithSlice struct {
    Slice []string
}

func (e ErrWithSlice) Error() string {
    return "ErrWithSlice"
}

func main() {
    if errors.Is(
        NestedError{Err: ErrWithSlice{[]string{"a", "b"}}},
        NestedError{Err: ErrWithSlice{[]string{"a", "b"}}},
    ) {
        fmt.Println("a is b")
    } else {
        fmt.Println("a is not b")
    }
}

What did you see happen?

$ go run error_is_example.go 
panic: runtime error: comparing uncomparable type main.ErrWithSlice

What did you expect to see?

a is not b should be printed.

Note this still behaves correctly if ErrWithSlice is passed directly to errors.Is, as opposed to nested.

Comment From: jmymay

Possibly similar past issue: https://github.com/golang/go/issues/54709

Comment From: gabyhelp

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

Comment From: neild

Note that errors.Is can only panic if the target error is not comparable.

That is, if err == target panics, then errors.Is(err, target) also panics, as does errors.Is(errWrappingTarget, target). If err == err does not panic, then errors.Is(x, err) does not panic. (Perhaps I'm missing something and there are cases where this isn't true; if so, I'd appreciate it if someone could point them out.)

Since errors.Is(err, target) is essentially err == target but across the entire tree of err, I think this behavior is fairly reasonable. If we had a cheap way to avoid the panic, we might use it, but we don't.

So perhaps errors.Is should just better document that it can panic if handed a target that is not comparable.

Comment From: gopherbot

Change https://go.dev/cl/686175 mentions this issue: errors: document that Is can panic on noncomparable errors