Skip to content

cmd/compile: better error message for ambiguous parse #70828

Open
@smu-ggl

Description

@smu-ggl

Go version

go version 1.23

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/smu/.cache/go-build'
GOENV='/home/smu/.config/go/env'
GOEXE=''
GOEXPERIMENT='fieldtrack,boringcrypto'
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/smu/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/smu/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/google-golang'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/google-golang/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23 [redacted] X:fieldtrack,boringcrypto'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/smu/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1689118444=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I expected

if mErr := &MyError{}; errors.As(err1, &mErr) {
...
}

to be parse correctly. See https://go.dev/play/p/XHkampOkowJ for a reproduction case.

Actual result of that playground snippet was:

./prog.go:23:10: syntax error: cannot use mErr := &MyError as value
./prog.go:26:1: syntax error: non-declaration statement outside function body

Changing the line

if mErr := &MyError{}; errors.As(err1, &mErr) {

into

if mErr := &(MyError{}); errors.As(err1, &mErr) {

or into

mErr := &MyError{}
if errors.As(err1, &mErr) {

makes the code work, but I would expect the first variant to work just as well.

What did you see happen?

./prog.go:23:10: syntax error: cannot use mErr := &MyError as value
./prog.go:26:1: syntax error: non-declaration statement outside function body

What did you expect to see?

A compiled program

Metadata

Metadata

Assignees

Labels

BadErrorMessageIssues related compiler error messages that should be better.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

Todo

Relationships

None yet

Development

No branches or pull requests

Issue actions