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

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

Open
smu-ggl opened this issue Dec 13, 2024 · 5 comments
Open

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

smu-ggl opened this issue Dec 13, 2024 · 5 comments
Assignees
Labels
BadErrorMessage Issues related compiler error messages that should be better. compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@smu-ggl
Copy link

smu-ggl commented Dec 13, 2024

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

@schachmat
Copy link

Probably the same as #37043 (comment) ?

@smu-ggl
Copy link
Author

smu-ggl commented Dec 13, 2024

This is well known; see https://golang.org/ref/spec#Composite_literals:

A parsing ambiguity arises when a composite literal using the TypeName form of the LiteralType appears as an operand between the keyword and the opening brace of the block of an "if", "for", or "switch" statement, and the composite literal is not enclosed in parentheses, square brackets, or curly braces. In this rare case, the opening brace of the literal is erroneously parsed as the one introducing the block of statements. To resolve the ambiguity, the composite literal must appear within parentheses.

@smu-ggl
Copy link
Author

smu-ggl commented Dec 13, 2024

closing this

@smu-ggl smu-ggl closed this as completed Dec 13, 2024
@ianlancetaylor
Copy link
Member

Reopening because I think we can produce a better error message.

package p

type S struct {
	F int
}

func F() {
	if s := S{}; s.F != 0 {
	}
}

The gc compiler says:

foo.go:8:7: syntax error: cannot use s := S as value
foo.go:10:1: syntax error: non-declaration statement outside function body

The gccgo compiler says

foo.go:8:17: error: parentheses required around this composite literal to avoid parsing ambiguity
    8 |         if s := S{}; s.F != 0 {
      |                 ^

CC @griesemer

@ianlancetaylor ianlancetaylor changed the title parser: Issue parsing assignment of newly created pointer value in if clause cmd/compile: better error message for ambiguous parse Dec 14, 2024
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 14, 2024
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Dec 14, 2024
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Dec 14, 2024
@griesemer griesemer self-assigned this Dec 16, 2024
@mknyszek mknyszek added the BadErrorMessage Issues related compiler error messages that should be better. label Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BadErrorMessage Issues related compiler error messages that should be better. compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Development

No branches or pull requests

7 participants