cmd/compile: don't convert to interface{} for un-comparable types in generic switch

Fixes #53635

Change-Id: I41f383be8870432fc0d29fa83687911ddd8217f1
Reviewed-on: https://go-review.googlesource.com/c/go/+/415634
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Cuong Manh Le 2022-07-01 16:33:54 +07:00
parent 1ebc983000
commit 14abe8aa73
2 changed files with 34 additions and 0 deletions

View file

@ -1214,6 +1214,9 @@ func (subst *subster) node(n ir.Node) ir.Node {
if m.Tag != nil && m.Tag.Op() == ir.OTYPESW {
break // Nothing to do here for type switches.
}
if m.Tag != nil && !types.IsComparable(m.Tag.Type()) {
break // Nothing to do here for un-comparable types.
}
if m.Tag != nil && !m.Tag.Type().IsEmptyInterface() && m.Tag.Type().HasShape() {
// To implement a switch on a value that is or has a type parameter, we first convert
// that thing we're switching on to an interface{}.

View file

@ -0,0 +1,31 @@
// run
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func main() {
f[int]()
}
func f[T any]() {
switch []T(nil) {
case nil:
default:
panic("FAIL")
}
switch (func() T)(nil) {
case nil:
default:
panic("FAIL")
}
switch (map[int]T)(nil) {
case nil:
default:
panic("FAIL")
}
}