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/cgo: newline replacement in composite literals generates invalid code #67517

Closed
flga opened this issue May 19, 2024 · 1 comment
Closed
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.

Comments

@flga
Copy link

flga commented May 19, 2024

Go version

go1.22.1 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/fl/.cache/go-build'
GOENV='/home/fl/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/fl/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/fl/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/fl/workspace/tools/go/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/fl/workspace/tools/go/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.1'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='zig cc'
CXX='g++'
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-build3518409302=/tmp/go-build -gno-record-gcc-switches'

What did you do?

reproduction in godbolt since the playground does not allow cgo

What did you see happen?

./example.go:8:23: syntax error: unexpected semicolon, expected expression
./example.go:13:26: syntax error: unexpected semicolon, expected expression

What did you expect to see?

Parsing fails because semicolons are incorrectly inserted in the generated code:

func() { _cgoBase0 := /*line :8:10*/&_Ctype_Foo /*line :8:16*/{	a:	1,;	ptr:	nil,}; _cgo0 := _cgoBase0; _cgoCheckPointer(_cgoBase0, 0 == 0); /*line :12:4*/_Cfunc_repro(_cgo0); }()
func() { _cgoBase0 := /*line :13:10*/&_Ctype_Foo /*line :13:16*/{	a:	1,;	ptr:	nil,}; _cgo0 := _cgoBase0; _cgoCheckPointer(_cgoBase0, 0 == 0); /*line :17:4*/_Cfunc_repro(_cgo0); }()

This is because gofmtLineReplacer does not account for consecutive newlines.

While the reproduction is clearly non-problematic, I found the issue in a much more involved construction (VkGraphicsPipelineCreateInfo) in which the freedom to insert newlines and comments is incredibly beneficial.

Furthermore, it appears that perhaps the replacing is too heavy handed in general since it also applies to string literals.

Related: #29748, #31017, #29781

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label May 19, 2024
@gopherbot
Copy link

Change https://go.dev/cl/586676 mentions this issue: cmd/cgo: accept multiple newlines when avoiding semicolons

@cagedmantis cagedmantis added the NeedsFix The path to resolution is known, but the work has not been done. label May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants