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: unnecessary error reports #22619
Comments
Is this a compiler or vet issue? It's not clear from your description. Please describe exactly what you did. (Your "What did you do"? report just shows some code). FWIW, I get
as the only errors with cmd/compile or go vet. Please clarify. |
I can recreate the problem with the Go 1.9 cmd/vet. But it's fixed on tip. Thanks for the report, but since it's been fixed I'm going to close this. I don't think this is important enough to backport to 1.9. |
@griesemer @ianlancetaylor |
@ianlancetaylor The
|
@dotaheor Please describe your exact steps (compiler, go vet invocation) and errors you see. |
'~/go/src/github.com/golang/go/bin/go build main.go`:
|
However, vet is fixed on tip. |
The error is correct according to the language spec, though the reason is a little complicated. A label must always appear before a statement. However, a statement may be a single semicolon, representing the empty statement. And that semicolon may be omitted before a |
But why must there be at least one statement following a label? On the other hand, from the implementation view, |
@dotaheor Well, now you are talking about a language change. Perhaps it would be a straightforward change, I don't know. Someone would have to work through all the possible issues and make a language change proposal. For your purposes, you could simply write |
OK, a quite good workaround. |
@ianlancetaylor |
@dotaheor A labeled statement is simply a statement. A labeled statement may have an empty statement following the ":", and an empty statement is literally nothing. But, all statements must be followed by a semicolon (*) because statements appear in statement lists. It would be extremely irregular in the grammar to have some statements be followed by semicolons and not others. (*) Finally, independently, semicolons may be elided before a closing "}". Altogether, this is what leads to the current behavior. You can be assured that this is not coincidental and has been carefully thought through. The rules are like they are to make a) the syntax regular (all statements must be followed by semicolons), and b) the case where a statement is immediately followed by a closing "}" of a block not overly onerous . These rules are in the syntax of the spec, and in the prose. |
PS: Most of the time a semicolon does not need to be written because of the semicolon insertion rule ( https://tip.golang.org/ref/spec#Semicolons ). This works out for pretty much all statements, but because empty statements have no tokens (they are empty), the token immediately before determines if a semicolon is inserted or not at the end of a line. In a labelled empty statement, that last token is ":", and ":" does not automatically cause a semicolon to be inserted at the end of a line. If there was any change to be made, one could perhaps propose that a semicolon can be omitted before a "case" or "default" keyword as well, and then this would work out without error. But I am not recommending such a change; it doesn't seem worthwhile the additional complexity for a corner case that is rare. |
Ah, it is quite complicated and subtle. I may make a proposal that inserting a semicolon right after each label declaration., Thanks for the detailed explanations. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go version go1.9.2 linux/amd64
Does this issue reproduce with the latest release?
yes
What did you do?
What did you expect to see?
No errors are reported.
What did you see instead?
The text was updated successfully, but these errors were encountered: