-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: multiple equal integer constant switch case values should be illegal #4524
Labels
Milestone
Comments
Aborted attempt at http://golang.org/cl/7311067. Leaving for gri. Status changed to Accepted. |
There are two orthogonal issues here: 1) definition of the switch semantics 2) restrictions on top of 1 Regarding 1: The idea of the expression switch statement was (from day one) that it should behave exactly like the corresponding if-else-if sequence (with the extra of fallthrough, but that is not an inherent complication). By defining a switch in terms of an if-else-if sequence we get an immediate and accurate definition of its behavior, it explains why an if-else-if sequence can always be rewritten into a respective switch statement and vice versa, and it also informs an straight-forward compiler how to implement it (e.g., by rewriting the AST to an if-else-if sequence). It also explains corner-case situations such as: switch 1<<100 { case 1<<100: } var x int switch 1.0 { case x: } etc., which are currently not accepted by 6g or gccgo for no good reasons. Going away from the switch if-else-if equivalence will make the spec and programming in Go more complicated. 2) It is possible to add orthogonal restrictions on top of the semantics proposed in 1). The typical restriction is to disallow multiple equal constant case values as in: var x int switch x { case 1: case 1: } At the moment, gc looks at duplicate integer, floating point, and string values (but ignores duplicate complex or boolean values); gccgo ignores duplicate boolean, string, and complex values. The situation is more complex when the tag expression is of interface type: var x interface{} switch x { case 1: case 1.0: case 1e0: } In this case 1 will be typed as int, while 1.0 and 1e0 will be typed as float64 and thus 1.0 and 1e0 would be the same constant, but not so 1. There are also good reasons for not having a restriction at all: For instance, any switch statement with more than 2 constant boolean case values will have duplicates, but those constants may be defined globally and enable/disable functionality that might be platform-specific. In such a scenario, we would not want a compiler error depending on configuration. The same applies to string constants, and to some extent integer constants. It may be reasonable to say that the restriction should apply to numeric values only, and perhaps even to integers only because that's a common scenario (ioata-defined integers of an "enum" type). After discussing this w/ r, I am leaning towards not having any restrictions enforced by the compiler at all. r suggested that go vet might be better suited for this job. |
Owner changed to @griesemer. |
griesemer
added
accepted
Documentation
Issues describing a change to documentation.
labels
Sep 10, 2014
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: