Skip to content

Nil check for custom errors in some situations are incorrect #31873

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

Closed
vishen opened this issue May 7, 2019 · 2 comments
Closed

Nil check for custom errors in some situations are incorrect #31873

vishen opened this issue May 7, 2019 · 2 comments

Comments

@vishen
Copy link

vishen commented May 7, 2019

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

$ go version
go version go1.12.1 linux/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/pentecostjonathan/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pentecostjonathan/go"
GOPROXY=""
GORACE=""
GOROOT="/home/pentecostjonathan/src/go"
GOTMPDIR=""
GOTOOLDIR="/home/pentecostjonathan/src/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/pentecostjonathan/src/coral-health/coralcloud/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 -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build298044920=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I have a custom error (implementing the Error interface). I have a function that calls two functions; the first function returns a nil error and and the second returns nil for my custom error. If I call the first function and assign it to err and then I call the second and accidentally assign it to the same err variable; a nil check always seems to return true.

type S struct{}

func (s *S) Error() string {
        return "error for S"
}

func structNil() *S {
        return nil
}

func errorNil() error {
        return nil
}

func main() {
        err := errorNil()
        fmt.Println(err != nil)
        err = structNil()
        fmt.Println(err != nil)
	err = errorNil()
	fmt.Println(err != nil)
}
false
true
false

https://play.golang.org/p/4edPo-lMy6G

I am unsure if this is expected or not, but it took me quite a while to track down exactly why my code was behaving weirdly. I realise it was a mistake on my behalf for assigning my custom error to an error type variable, but since the compiler seemed happy I didn't realise I was doing anything wrong.

What did you expect to see?

That all nil checks return the correct result.

What did you see instead?

A nil check on a custom error type always returns true.

@Bo0mer
Copy link

Bo0mer commented May 7, 2019

This is working as expected. See https://golang.org/doc/faq#nil_error.

@vishen
Copy link
Author

vishen commented May 7, 2019

Ah completely missed that, thanks!

@vishen vishen closed this as completed May 7, 2019
@golang golang locked and limited conversation to collaborators May 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants