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/go: multiline comment after switch expression causes build errors #33657

Closed
willfaught opened this issue Aug 14, 2019 · 3 comments
Closed

cmd/go: multiline comment after switch expression causes build errors #33657

willfaught opened this issue Aug 14, 2019 · 3 comments

Comments

@willfaught
Copy link
Contributor

@willfaught willfaught commented Aug 14, 2019

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

$ go version
go version go1.12.7 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/Will/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/Will/Developer/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.7/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.7/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/bx/qk0phsxd265fqj512dnnpg080000gn/T/go-build062382774=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

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

func main() {
	switch 1 /*
	 */{

	case 1:
		fmt.Println("Hello, playground")
	}
}

What did you expect to see?

No errors.

What did you see instead?

Errors:

./prog.go:8:9: 1 evaluated but not used
./prog.go:11:2: invalid case 1 in switch (mismatched types int and bool)

Some thoughts:

  • It seems to be interpreting the switch as having a simple statement "preamble" (the 1 expression) and missing a "switch value", which defaults to true, which is why it's complaining about int and bool not matching. Note that if you press the "Format" button in the Playground, it inserts a semicolon after the 1 in the "switch value" position.
  • It complains that 1 is evaluated but not used. This seems to be referring to the switch simple statement preamble. Isn't this also wrong? I read the language spec section for the switch statement, and there's nothing in there referring to a requirement about the statement being used. A SimpleStmt can indeed be an expression statement, according to the grammar there, if I understood correctly, so I'm confused where this is coming from. I thought maybe this is coming from the same place in the spec that forbids unused local variables, but the only place in the spec I could find that refers to that is "Implementation restriction: A compiler may make it illegal to declare a variable inside a function body if the variable is never used" in the Variable declarations section, which doesn't seem to apply to expression statements. Maybe I've missed or forgotten the relevant part of the spec?
@robpike

This comment has been minimized.

Copy link
Contributor

@robpike robpike commented Aug 15, 2019

This is mostly working as intended, or at least working as specified. As the spec says, a multiline comment is replaced by a newline. Thus your code, as seen by the compiler, has a newline after the 1, which by the rules of semicolon insertion terminates the switch statement at that point.

The error messages are a little odd, but the program is not valid.

@robpike

This comment has been minimized.

Copy link
Contributor

@robpike robpike commented Aug 15, 2019

The errors are the same if we replace the /*\n*/ by a newline: https://play.golang.org/p/jb7dUoBhAf-

@willfaught

This comment has been minimized.

Copy link
Contributor Author

@willfaught willfaught commented Aug 15, 2019

Gotcha. Interesting, didn't know that. Thanks!

@willfaught willfaught closed this Aug 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.