Description
The spec (version January 5, 2016) says in the Expression switches
section:
Implementation restriction: A compiler may disallow multiple case expressions evaluating to the same constant. For instance, the current compilers disallow duplicate integer, floating point, or string constants in case expressions.
Three questions about this:
- cmd/compile also disallows duplicate concrete type cases in type switches:
func f(e interface{}) {
switch e.(type) {
case int:
case int:
}
}
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?
- cmd/compile also disallows unreachable type cases in type switches:
func f(e error) {
switch e.(type) {
case int:
}
}
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?
- I would like to make cmd/compile disallow duplicate composite literal cases in expression switches, to enable binary search over them (cmd/compile: suboptimal compilation of struct-valued switch statements #15164). For example:
type A [1]int
func f(x A) {
switch x {
case A([1]int{1}):
case A([1]int{1}):
}
}
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.