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
Closed

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

mdempsky opened this issue Oct 8, 2018 · 4 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@mdempsky
Copy link
Member

mdempsky 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
Copy link
Member

/cc @josharian

@mdempsky
Copy link
Member Author

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 The path to resolution is known, but the work has not been done. label Oct 23, 2018
@bcmills bcmills added this to the Unplanned milestone Oct 23, 2018
@randall77
Copy link
Contributor

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

@gopherbot
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants