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: switch expressions behave differently for non-comparable type parameters and other non-comparable types #67285

Closed
flimzy opened this issue May 10, 2024 · 2 comments

Comments

@flimzy
Copy link
Contributor

flimzy commented May 10, 2024

Go version

1.22.0

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/jonhall/.cache/go-build'
GOENV='/home/jonhall/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/jonhall/go/pkg/mod'
GONOPROXY='gitlab.com/FlashbackSRS/priv,gitlab.com/flimzy/hacker-portfolio'
GONOSUMDB='gitlab.com/FlashbackSRS/priv,gitlab.com/flimzy/hacker-portfolio'
GOOS='linux'
GOPATH='/home/jonhall/go'
GOPRIVATE='gitlab.com/FlashbackSRS/priv,gitlab.com/flimzy/hacker-portfolio'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='/usr/bin/gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/jonhall/src/test/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build4212822218=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I ran this program with go run .

package main

import "fmt"

func main() {
	var x [3]int
	switch x {
	default:
		fmt.Println("default")
	}
}

What did you see happen?

It compiled, and output the following text:

default

What did you expect to see?

I expected a compilation error.

The spec states that:

The switch expression type must be comparable.

Further it explains:

Slice, map, and function types are not comparable. However, as a special case, a slice, map, or function value may be compared to the predeclared identifier `.

Based on this, I would expect a switch expression on an array (for example) to not compile. Note that this following code indeed does not compile, which matches expectations.

func foo[T any](t T) {
	switch t {
	default:
		fmt.Println("default")
	}
}

But as the spec doesn't mention any difference between the "non-comparability" of type parameters, and other non-comparable types, I expected them to behave the same.

@ALTree
Copy link
Member

ALTree commented May 10, 2024

[3]int is an array type, not a slice, and array types are comparable. I think this is working as intended, so I'm closing, but feel free to comment if I've misunderstood the issue.

@ALTree ALTree closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
@flimzy
Copy link
Contributor Author

flimzy commented May 10, 2024

@ALTree: You're absolutely right. Thanks for pointing out the obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants