Skip to content

cmd/compile: unexpected error with curly-braced initializer before if-block condition #20777

@snargleplax

Description

@snargleplax

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.8.3

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/luke/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/kk/4m8p_4c155bdfbg8kb5cgvx40000gq/T/go-build187863764=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

https://play.golang.org/p/TVAiPb6Bj7

What did you expect to see?

I expected this program to compile without error. It seems to choke on the use of curly-braced initializers in the (optional) simple statement before the semicolon preceding the if block's condition expression. According to the spec, a SimpleStatement can be a ShortVarDecl, which by my reading ought to encompass such cases. I suspect that the parser is treating the open curly brace on the struct literal as if it were the open brace for the if's actual block.

What did you see instead?

tmp/sandbox703829250/main.go:10: syntax error: a := myStruct used as value
tmp/sandbox703829250/main.go:11: syntax error: unexpected semicolon or newline, expecting comma or }

Adding parens around the SimpleStatement works around the problem. The same problem occurs not just with struct types, but also literals of user-defined types with an underlying type whose kind is a slice.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions