Skip to content

spec: clarify allowed implementation restrictions for switch statements #15896

Closed
@josharian

Description

@josharian

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?

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions