Skip to content

cmd/compile: bad error message when defining a variable for a parenthesized type #68589

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

Closed
iwahbe opened this issue Jul 25, 2024 · 4 comments
Closed
Assignees
Labels
BadErrorMessage Issues related compiler error messages that should be better. compiler/runtime Issues related to the Go compiler and/or runtime.
Milestone

Comments

@iwahbe
Copy link

iwahbe commented Jul 25, 2024

Go version

go version go1.22.4 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN='/Users/ianwahbe/go/bin'
GOCACHE='/Users/ianwahbe/Library/Caches/go-build'
GOENV='/Users/ianwahbe/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/ianwahbe/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/ianwahbe/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.22.4/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.4/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.4'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/fg/_1q36r4j6yx0rwz2fbhjd5y40000gn/T/go-build3467175502=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I tried to compile this code (link):

var key (type keyType struct{})

What did you see happen?

The compilation errors with:

./prog.go:3:10: syntax error: unexpected type, expected type

I wasn't sure if the above was valid Go code, but the compiler should give a better error message.

What did you expect to see?

An improved error message like:

./prog.go:3:16: syntax error: type declarations cannot be used as expressions

@ianlancetaylor ianlancetaylor changed the title Bad error message when defining a variable for a parenthesized type cmd/compile: bad error message when defining a variable for a parenthesized type Jul 25, 2024
@ianlancetaylor
Copy link
Contributor

CC @griesemer @golang/compiler

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 25, 2024
@cherrymui cherrymui added the BadErrorMessage Issues related compiler error messages that should be better. label Jul 25, 2024
@griesemer griesemer self-assigned this Jul 25, 2024
@griesemer griesemer added this to the Go1.24 milestone Jul 25, 2024
@griesemer
Copy link
Contributor

This is rather unusual code not typically encountered once a basic familiarity with Go has been established (at least that's the first time something like this has come to our attention in 15 years). It's also non-trivial to recognize that the parenthesized code is a type declaration in order to provide the desired error message; the effort doesn't seem justified given that this is just a rare situation.

It perhaps makes sense to report something like "unexpected keyword type, expected type expression" to distinguish the two kinds of types. That would only incur a cost in case of an error (rare).

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/623819 mentions this issue: cmd/compile: better error message when offending/missing token is a keyword

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.
Projects
Development

No branches or pull requests

6 participants