Repository: go
Issue: x/tools/go/ssa: addNamedLocal panic: runtime error: invalid memory address or nil pointer dereference
Labels: NeedsInvestigation,Tools

<!--
Please answer these questions before submitting your issue. Thanks!
-->

### What version of Go are you using (`go version`)?
<pre>
$ go version
go version go1.18.3 linux/amd64
</pre>

### Does this issue reproduce with the latest release?
Yes


### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/[username]/.cache/go-build"
GOENV="/home/[username]/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/data00/home/[username]/gopath/pkg/mod"
GONOPROXY="*.byted.org,*.everphoto.cn,git.smartisan.com"
GONOSUMDB="*.byted.org,*.everphoto.cn,git.smartisan.com"
GOOS="linux"
GOPATH="/data00/home/[username]/gopath"
GOPRIVATE="*.byted.org,*.everphoto.cn,git.smartisan.com"
GOPROXY="https://go-mod-proxy.byted.org,https://goproxy.cn,https://proxy.golang.org,direct"
GOROOT="/data00/home/[username]/go1.16"
GOSUMDB="sum.golang.google.cn"
GOTMPDIR=""
GOTOOLDIR="/data00/home/[username]/go1.16/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/data00/home/[username]/repos/bytest_ut_api/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-build3893359244=/tmp/go-build -gno-record-gcc-switches"
</pre></details>

### What did you do?

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on go.dev/play is best.
-->
Create SSA program for a set of packages 
```golang
initial, err := packages.Load(cfg, fileDir)
if err != nil {
return nil, fmt.Errorf("The error belongs to %w\n, the details is: %v", logextractor.ParseProgramError, err.Error())
}

// Create SSA packages for all well-typed packages.
prog, pkgs := ssautil.AllPackages(initial, 0)
prog.Build()
```


### What did you expect to see?
Packages loaded without panic.


### What did you see instead?
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x50 pc=0x104773d28]
goroutine 13469 [running]:
golang.org/x/tools/go/ssa.(*Function).addNamedLocal(0x1400aa32780, {0x0, 0x0})
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/func.go:392 +0x28
golang.org/x/tools/go/ssa.(*Function).addLocalForIdent(...)
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/func.go:399
golang.org/x/tools/go/ssa.(*builder).localValueSpec(0x7708?, 0x1400aa32780, 0x140003b7540)
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/builder.go:1155 +0x2c0
golang.org/x/tools/go/ssa.(*builder).stmt(0x140000a7901?, 0x1400aa32780, {0x104a42a30?, 0x14003be1bf0?})
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/builder.go:2147 +0x1514
golang.org/x/tools/go/ssa.(*builder).stmtList(0x1049d0e80?, 0x104a453b8?, {0x14005cb0a80?, 0x6, 0x140000a7948?})
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/builder.go:946 +0x6c
golang.org/x/tools/go/ssa.(*builder).stmt(0x1400aa32780?, 0x1400aa32780, {0x104a428e0?, 0x14005093ec0?})
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/builder.go:2277 +0x850
golang.org/x/tools/go/ssa.(*builder).buildFunctionBody(0x1400aa32180?, 0x1400aa32780)
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/builder.go:2391 +0x3c8
golang.org/x/tools/go/ssa.(*builder).buildFunction(0x104773b30?, 0x1400aa32780)
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/builder.go:2326 +0x34
golang.org/x/tools/go/ssa.(*builder).buildCreated(0x140000a7dd0)
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/builder.go:2413 +0x2c
golang.org/x/tools/go/ssa.(*Package).build(0x1400a8b1c00)
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/builder.go:2606 +0xaf8
sync.(*Once).doSlow(0x140035c37a8?, 0x10474cf24?)
        /usr/local/go/src/sync/once.go:68 +0x10c
sync.(*Once).Do(...)
        /usr/local/go/src/sync/once.go:59
golang.org/x/tools/go/ssa.(*Package).Build(...)
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/builder.go:2477
golang.org/x/tools/go/ssa.(*Program).Build.func1(0x0?)
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/builder.go:2462 +0x58
created by golang.org/x/tools/go/ssa.(*Program).Build
        /Users/[username]/go/pkg/mod/golang.org/x/tools@v0.6.0/go/ssa/builder.go:2461 +0x180
```

### Potentially problematic code
https://cs.opensource.google/go/x/tools/+/refs/tags/v0.6.0:go/ssa/func.go;l=399
`f.info.Defs[id]` cloud be nil in some case. Should check if `Defs[id] == nil` at first