Skip to content

Commit 14abe8a

Browse files
committed
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 <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 1ebc983 commit 14abe8a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/cmd/compile/internal/noder/stencil.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,9 @@ func (subst *subster) node(n ir.Node) ir.Node {
12141214
if m.Tag != nil && m.Tag.Op() == ir.OTYPESW {
12151215
break // Nothing to do here for type switches.
12161216
}
1217+
if m.Tag != nil && !types.IsComparable(m.Tag.Type()) {
1218+
break // Nothing to do here for un-comparable types.
1219+
}
12171220
if m.Tag != nil && !m.Tag.Type().IsEmptyInterface() && m.Tag.Type().HasShape() {
12181221
// To implement a switch on a value that is or has a type parameter, we first convert
12191222
// that thing we're switching on to an interface{}.

test/fixedbugs/issue53635.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// run
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
func main() {
10+
f[int]()
11+
}
12+
13+
func f[T any]() {
14+
switch []T(nil) {
15+
case nil:
16+
default:
17+
panic("FAIL")
18+
}
19+
20+
switch (func() T)(nil) {
21+
case nil:
22+
default:
23+
panic("FAIL")
24+
}
25+
26+
switch (map[int]T)(nil) {
27+
case nil:
28+
default:
29+
panic("FAIL")
30+
}
31+
}

0 commit comments

Comments
 (0)