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

cmd/compile: non-compliant duplicate constant checking #28078

Closed
mdempsky opened this issue Oct 8, 2018 · 4 comments

Comments

Projects
None yet
5 participants
@mdempsky
Copy link
Member

commented Oct 8, 2018

The Go spec disallows duplicate map key constants and allows implementations to restrict duplicate switch case constants. However, in both cases, these must be Go language constants (as the Go spec doesn't have any other notion of "constant".)

This code should be valid, but cmd/compile currently rejects it for having duplicate keys and cases:

package p

import "unsafe"

func f() {
	_ = map[uintptr]int {
		0: 0,
		uintptr(unsafe.Pointer(nil)): 0,
	}

	switch uintptr(0) {
	case 0:
	case uintptr(unsafe.Pointer(nil)):
	}

	switch interface{}(nil) {
	case nil:
	case nil:
	}

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

gotype accepts it.

(This is yet another bug relating to gc's overly eager constant folding in the frontend.)

@odeke-em

This comment has been minimized.

Copy link
Member

commented Oct 16, 2018

@mdempsky

This comment has been minimized.

Copy link
Member Author

commented Oct 16, 2018

FYI, I think this is relatively straight forward to fix: we just need to check isGoConstant() instead of Op==OLITERAL when checking for duplicate constants in composite literals and switch cases. (Moreover, we should probably de-duplicate that logic.)

Also related: #28085.

@bcmills bcmills added the NeedsFix label Oct 23, 2018

@bcmills bcmills added this to the Unplanned milestone Oct 23, 2018

@randall77

This comment has been minimized.

Copy link
Contributor

commented Nov 26, 2018

The fix in #24760 fixed the map cases, but not the switch cases.

@gopherbot

This comment has been minimized.

Copy link

commented Nov 27, 2018

Change https://golang.org/cl/151320 mentions this issue: cmd/compile: don't convert non-Go-constants to OLITERALs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.