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: unreachable "break" after return in switch statement causes erroneous compiler static analysis #37364
Comments
A reproducing code sample that's even more aggressively minimal: https://play.golang.org/p/MGido5G7mc7 |
This may be a philosophical argument about what constitutes a "terminating statement" in the context of
(https://golang.org/ref/spec#Terminating_statements) In the code examples above, the In other words, the strict wording of the spec does not support the behavior in this issue being a "bug" per se. |
cc @griesemer |
I think the most important point in this area is that we keep the rules for terminating statements simple, so that all compilers can simply and easily agree as to which statements terminate and which do not. I think that is much more important than handling cases like Also, So my preference here would be to do nothing. |
Also, vet should catch this. |
Vet has the same confusion about the missing return that build does. Is that what you meant by vet "catching" it, or could vet give a more helpful error like "line 11 is unreachable"? |
Oh right, because vet needs the code to typecheck before it runs. |
I agree with @ianlancetaylor here: Making the rules more complicated just to catch a few extra, possibly pathological, cases does not seem worth it. It is also a slippery slope: What about func f() int {
if true {
return 0
}
} (https://play.golang.org/p/_bZ7FxnpNbt) It's obvious that this function returns, yet it is not covered by the current rules and the compiler will complain. It's not obvious where to stop, once we make the existing rules even just slightly more complicated. That all said, the current behavior is correct according to the spec and I am closing this as "working as intended". If you disagree, I encourage you to open a new issue with a concrete proposal regarding the rule change you're envisioning. Thanks. |
What version of Go are you using (
go version
)?(from the Playground)
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?The Go playground at https://play.golang.org
What did you do?
https://play.golang.org/p/K_qpq7GmBhF
The
break
after areturn
in aswitch/case
causes the compiler to think that the wrapping function is missing areturn
, when in fact it is impossible for the function to break out of theswitch
.What did you expect to see?
1
(or
2
or3
depending on the variable)What did you see instead?
The text was updated successfully, but these errors were encountered: