Skip to content

spec: clarify requirements for duplicate constants in interface-typed map literal keys and switches #28085

Open
@mdempsky

Description

@mdempsky

The Go spec disallows duplicate constants in map literal keys, and allows compilers to reject duplicate constants in switch cases.

However, the Go spec does not formally allow interface-typed constants, and doesn't mention how to handle constants that are implicitly or explicitly converted to interface type. The existing compilers handle these situations in differing ways:

package p

// #1
var _ = map[interface{}]int{
	0: 0,
	0: 0,
}

// #2
var _ = map[interface{}]int{
	interface{}(0): 0,
	interface{}(0): 0,
}

func _() {
	// #3
	switch interface{}(0) {
	case 0:
	case 0:
	}

	// #4
	switch interface{}(0) {
	case interface{}(0):
	case interface{}(0):
	}
}

cmd/compile rejects 1, 2, and 3.

go/types rejects 1 and 3.

gccgo (8.0) rejects none.

/cc @griesemer @ianlancetaylor

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions