Skip to content

everything: fail to check err == nil when assigning explicit type to error #56930

@liran-funaro

Description

@liran-funaro

What version of Go are you using (go version)?

This has been validated on other machines and Go version, as well as on https://go.dev/play.

$ go version
go version go1.19.3 darwin/arm64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/liranfunaro/Library/Caches/go-build"
GOENV="/Users/liranfunaro/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/liranfunaro/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/liranfunaro/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.16.9"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/liranfunaro/workspace/go-testing/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/by/rgkh7tcn3yn0wdgxl63bynfc0000gn/T/go-build1699839087=/tmp/go-build -gno-record-gcc-switches -fno-common"
~/w/go-testing $ go1.19.3 env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/liranfunaro/Library/Caches/go-build"
GOENV="/Users/liranfunaro/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/liranfunaro/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/liranfunaro/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/liranfunaro/sdk/go1.19.3"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/liranfunaro/sdk/go1.19.3/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.19.3"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/liranfunaro/workspace/go-testing/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/by/rgkh7tcn3yn0wdgxl63bynfc0000gn/T/go-build3375246314=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Here is a minimalist example that reproduces this error (play):

package main

import "fmt"

type MyError struct {
	Text string
}

func (e *MyError) Error() string {
	return fmt.Sprintf("msg: %s", e.Text)
}

func foo() *MyError {
	return nil
}

func main() {
	var typedErr *MyError
	typedErr = foo()
	fmt.Printf("       typedErr: %v\n", typedErr)
	fmt.Printf("typedErr == nil: %v\n\n", typedErr == nil)

	var err error
	err = foo()
	fmt.Printf("            err: %v\n", err)
	fmt.Printf("     err == nil: %v\n\n", err == nil)
}

What did you expect to see?

I expect that err == nil in both cases as foo() always returns nil.

       typedErr: <nil>
typedErr == nil: true

            err: <nil>
     err == nil: true

What did you see instead?

But when I assigned foo() to a generic error variable, then the check err == nil return false even though it is clearly visible that err is nil as printed right above it.

       typedErr: <nil>
typedErr == nil: true

            err: <nil>
     err == nil: false

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions