-
Notifications
You must be signed in to change notification settings - Fork 18k
spec: clarify allowed implementation restrictions for switch statements #15896
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
Comments
I guess the last one would break backwards compatibility. I have another implementation approach for such dead cases (including duplicate nil cases in type switches). We might want to add a vet check for detectably duplicate cases, if the compiler isn't going to reject them, since they're almost certainly bugs. I'll file a separate issue for that. |
For type switches, the spec says ( Cases then match actual types T against the dynamic type of the expression x. As with type assertions, x must be of interface type, and each non-interface type T listed in a case must implement the type of x. That is, the spec already disallows using Furthermore, given that the semantics of the type switch is essentially an if-else-if sequence, a duplicate entry is never reached. But all, gc, gccgo, and go/types complain about duplicate entries, so we should probably document that by disallowing them in the spec. Finally, I've tried to disallow certain duplicate entries for expression switches before, several years back, and it met with some resistance. We most likely cannot change something here if the compiler allowed it until now otherwise we are violating the Go 1.0 guarantee. |
Since the compiler's don't permit duplicate types in type switches, we should probably disallow duplicate nils in type switches as well, per discussion with @randall77 and @ianlancetaylor . The nil case in type switches was added later, and it's arguably an oversight (bug) that compilers permitted them. go/types doesn't permit it. |
Thanks, Robert. I filed #15906 for a possible vet check. So it sounds like the only outstanding todo here is to document that compilers may disallow duplicate entries in type switches. |
CL https://golang.org/cl/23584 mentions this issue. |
The spec (version January 5, 2016) says in the
Expression switches
section:Three questions about this:
This generates the error
duplicate case int in type switch
. But this implementation restriction is not currently allowed by the spec. Should it be? Does it matter?This generates the error
impossible type switch case: e (type error) cannot have dynamic type int (missing Error method)
. But this implementation restriction is not currently allowed by the spec. Should it be? Does it matter?Currently, this fails to compile, but it is a bug (#15895). One could argue that such composite literals "evaluate to the same constant", but it's not a constant in the sense usually used in the spec.
The text was updated successfully, but these errors were encountered: