Skip to content

Can not compile this: if err := T{}.F(); err != nil{} #37043

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
DylanMeeus opened this issue Feb 5, 2020 · 2 comments
Closed

Can not compile this: if err := T{}.F(); err != nil{} #37043

DylanMeeus opened this issue Feb 5, 2020 · 2 comments

Comments

@DylanMeeus
Copy link

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

$ go version 13

Does this issue reproduce with the latest release?

yes

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

Whatever is running on the playground - could reproduce under Debian with this env

"
GOCACHE="/home/dylan/.cache/go-build"
GOENV="/home/dylan/.config/go/env"
GOEXE=""
GOFLAGS=" -mod="
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/dylan/Development/Code/Go/"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/dylan/Development/Code/Dploy/dploy-main/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build157748824=/tmp/go-build -gno-record-gcc-switches"

What did you do?

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

core code snippet:

if e := T{}.F(); e != nil { 
     // do something with e
}

What did you expect to see?

No compilation error, I expect this type if e := T{}.F(); e != nil{} to correctly resolve e to the result of F

What did you see instead?

Compilation error:

./prog.go:18:11: syntax error: e := T used as value

PS: I had a hard time naming this issue, feel free to rename

@mvdan
Copy link
Member

mvdan commented Feb 5, 2020

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.

@DylanMeeus
Copy link
Author

Ah I see, thanks :) Yeah I figured it could be resolved by adding the parenthesis (as on the playground). Guess this is not an issue then ;-)

@golang golang locked and limited conversation to collaborators Feb 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants