-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
spec: switch with no value uses bool rather than untyped bool #6398
Labels
Milestone
Comments
I am not convinced this is a bug. Where do you draw the line? http://play.golang.org/p/uymdQ7c1U8 |
Yes, the compelling reason not to do this is that a switch is defined to evaluate the switch expression just once, presumably store it somewhere, and then use that stored result repeatedly for each == test. If you switch f() it does not turn into == f() at each use. The storing it somewhere implies a specific type. I think it's pretty magical to make untyped constants not follow this rule. Russ |
The spec says: "In an expression switch, the switch expression is evaluated and the case expressions, which need not be constants, are evaluated left-to-right and top-to-bottom; the first one that equals the switch expression triggers execution of the statements of the associated case; the other cases are skipped." As for the evaluated expression, I don't see where the spec says that an expression must have a type. In fact, as per the grammar "2 * 3" and "true || false" are both expressions, but they are untyped. |
This is related to issue #5769 which proposes that "only field selectors should auto-dereference pointers to structs". If we accept 5769, this example would indeed expose a bug in gc (and gccgo for that matter). |
I remain unconvinced that this is a bug in the compilers. Perhaps the spec needs to be revised. I don't see a connection to issue #5769; there are no pointers in this program. Perhaps you meant to add comment #11 to some other issue, gri? Labels changed: removed release-go1.3. |
I agree that comment #11 is misplaced and probably should have gone elsewhere. Will delete. I think one could follow the spec to the word (switches are equivalent to if-else-if sequences) and then this would be a compiler bug. But such a direct interpretation would not easily explain that a switch tag that is a function call is only executed once. So perhaps we indeed do need to update the spec. |
Labels changed: added documentation. Owner changed to @griesemer. |
CL https://golang.org/cl/12711 mentions this issue. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The text was updated successfully, but these errors were encountered: