Skip to content

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

Closed
@bcmills

Description

@bcmills

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions