Skip to content

errors: Is should not panic on nested uncomparable types #74488

Open
@jmymay

Description

@jmymay

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions