Skip to content
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

errors: As should allow target to be a nil pointer to a concrete type #31541

Open
bcmills opened this issue Apr 18, 2019 · 1 comment

Comments

Projects
None yet
1 participant
@bcmills
Copy link
Member

commented Apr 18, 2019

What did you do?

example.com$ cat >./main.go <<EOF
package main

import (
        "errors"
        "fmt"
        "net"
)

func main() {
        const addr = "••••••••"
        conn, err := net.Dial("tcp", addr)
        if err != nil {
                if errors.As(err, (*net.InvalidAddrError)(nil)) {
                        fmt.Println("invalid address: %s")
                }
        }
        defer conn.Close()
}
EOF

example.com$ cat >./go.mod <<EOF
module example.com

go 1.12
EOF

example.com$ go version
go version devel +33fdc10a30 Wed Apr 17 13:37:58 2019 -0400 linux/amd64

example.com$ go run main.go

What did you expect to see?

errors.As being useful for type-checks even if I don't want to allocate space to store the result.

For this specific example:

invalid address: ••••••••

What did you see instead?

panic: errors: target must be a non-nil pointer

goroutine 1 [running]:
errors.As(0x54de60, 0xc000098000, 0x507020, 0x0, 0x0)
        /usr/local/google/home/bcmills/go/src/errors/wrap.go:86 +0x5eb
main.main()
        /tmp/tmp.8hp5hhWBDQ/example.com/main.go:13 +0xd5
exit status 2

This is the documented behavior of errors.As, but it seems strictly less useful than allowing a nil pointer of a concrete type.

@bcmills bcmills added this to the Go1.13 milestone Apr 18, 2019

@bcmills

This comment has been minimized.

Copy link
Member Author

commented Apr 18, 2019

(See previously #30970, which relaxed a somewhat related constraint on err.)

CC @neild @jba @mpvl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.