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: regression in type switch deduplication #17517

Closed
dsnet opened this issue Oct 19, 2016 · 6 comments
Closed

cmd/compile: regression in type switch deduplication #17517

dsnet opened this issue Oct 19, 2016 · 6 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Oct 19, 2016

8c85e23 caused a regression where duplicate cases fail to be detected in a switch.

package main

func main() {
    var a int
    switch a {
    case 0,1:
    case 0:
    }
}

On Go1.7, the above program fails:

./main.go:13: duplicate case 0 in switch
    previous case at ./main.go:12

On Go1.8dev, the above program, unexpectedly succeeds.

I understand that this is not a language guarantee:

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.

However, we should be consistent with how it is done. The following snippet is rejected on both Go1.7 and Go1.8dev:

var a int
switch a {
case 0:
case 0:
}

/cc @josharian @mdempsky

@dsnet dsnet added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 19, 2016
@dsnet dsnet added this to the Go1.8 milestone Oct 19, 2016
@rsc
Copy link
Contributor

rsc commented Oct 21, 2016

Caused by 8c85e23. Must be fixed for Go 1.8.

/cc @josharian @mdempsky

@odeke-em
Copy link
Member

Interestingly type switches catch the duplicates but the expression switches
do not catch them like @dsnet reported

package main

func main() {
    var a int
    switch a {
    case 1, 0:
    case 0:
    }

    var s interface{} = "hello"
    switch s.(type) {
    case string:
    case int, string:
    }
}
$ go run main.go
./main.go:13: duplicate case string in type switch
    previous case at ./main.go:12
$ go version
go version devel +930ab0a Fri Oct 21 02:14:57 2016 +0000 darwin/amd64

so I think that can narrow the bug to the code for *exprSwitch

@odeke-em
Copy link
Member

I've mailed https://go-review.googlesource.com/31671

@gopherbot
Copy link

CL https://golang.org/cl/31671 mentions this issue.

@gopherbot
Copy link

CL https://golang.org/cl/31716 mentions this issue.

@mdempsky
Copy link
Member

@odeke-em Thanks again for your help in root causing the issue!

@golang golang locked and limited conversation to collaborators Oct 21, 2017
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