Skip to content
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: clarify requirements for duplicate constants in interface-typed map literal keys and switches #28085

Open
mdempsky opened this issue Oct 8, 2018 · 2 comments
Assignees
Labels
Milestone

Comments

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Oct 8, 2018

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

@griesemer griesemer self-assigned this Oct 9, 2018
@griesemer griesemer added this to the Go1.12 milestone Oct 9, 2018
@mdempsky

This comment has been minimized.

Copy link
Member Author

@mdempsky mdempsky commented Oct 9, 2018

Caveat to gccgo not warning about 1 or 2: gccgo 8.0 apparently doesn't even warn about:

_ = map[int]int{0: 0, 0: 0}

Filed as #28104.

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Dec 4, 2018

Change https://golang.org/cl/152544 mentions this issue: cmd/compile: unify duplicate const detection logic

@griesemer griesemer modified the milestones: Go1.12, Go1.13 Dec 11, 2018
gopherbot pushed a commit that referenced this issue Feb 27, 2019
Consistent logic for handling both duplicate map keys and case values,
and eliminates ad hoc value hashing code.

Also makes cmd/compile consistent with go/types's handling of
duplicate constants (see #28085), which is at least an improvement
over the status quo even if we settle on something different for the
spec.

As a side effect, this also suppresses cmd/compile's warnings about
duplicate nils in (non-interface expression) switch statements, which
was technically never allowed by the spec anyway.

Updates #28085.
Updates #28378.

Change-Id: I176a251e770c3c5bc11c2bf8d1d862db8f252a17
Reviewed-on: https://go-review.googlesource.com/c/152544
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.