From 14abe8aa7324bdf0e09e1dfebfb3519cc30f4918 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Fri, 1 Jul 2022 16:33:54 +0700 Subject: [PATCH] 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 Run-TryBot: Cuong Manh Le Reviewed-by: Keith Randall Reviewed-by: Keith Randall Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/noder/stencil.go | 3 +++ test/fixedbugs/issue53635.go | 31 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 test/fixedbugs/issue53635.go diff --git a/src/cmd/compile/internal/noder/stencil.go b/src/cmd/compile/internal/noder/stencil.go index 796a740528..1534a1fa49 100644 --- a/src/cmd/compile/internal/noder/stencil.go +++ b/src/cmd/compile/internal/noder/stencil.go @@ -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{}. diff --git a/test/fixedbugs/issue53635.go b/test/fixedbugs/issue53635.go new file mode 100644 index 0000000000..bea5493805 --- /dev/null +++ b/test/fixedbugs/issue53635.go @@ -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") + } +} -- 2.48.1