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: "cannot use _ as value" compile error for blank identifier variable declaration with non-empty composite literal #20484

Closed
dmitshur opened this issue May 24, 2017 · 2 comments

Comments

@dmitshur
Copy link
Contributor

dmitshur commented May 24, 2017

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

$ go version
go version go1.8.3 darwin/amd64

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

$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/Dmitri/Dropbox/Work/2013/GoLanding:/Users/Dmitri/Dropbox/Work/2013/GoLand"
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/tw/kgz4v2kn4n7d7ryg5k_z3dk40000gn/T/go-build536199591=/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?

I was writing some new Go code, and wanted to assign an unused struct to the blank identifier to prevent a compiler error.

package main

func main() {
	_ = int(5)               // ok
	_ = struct{ int }{}      // ok
	_ = struct{ int }{5}     // ok
	var _ = int(5)           // ok
	var _ = struct{ int }{}  // ok
	var _ = struct{ int }{5} // cannot use _ as value
}

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

What did you expect to see?

No compile errors. (Probably? I'm not sure if this is intended behavior or not, but it doesn't seem to be.)

What did you see instead?

I got "cannot use _ as value" compile error.

Additional Notes

I tried gc compiler from Go 1.4.3, and got the same error.

go/types doesn't seem to give an error for that code.

@odeke-em
Copy link
Member

Looks like a duplicate of #15481 and in particular this permutation #15481 (comment).

@dmitshur
Copy link
Contributor Author

Thanks, that does look the same. Closing as duplicate.

I'll copy my snippet to that issue, since I don't see a case of struct literals mentioned there.

@golang golang locked and limited conversation to collaborators May 24, 2018
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