diff options
author | Cuong Manh Le <cuong.manhle.vn@gmail.com> | 2022-06-29 00:07:37 +0700 |
---|---|---|
committer | Heschi Kreinick <heschi@google.com> | 2022-07-06 19:33:49 +0000 |
commit | f86c6b9a32daacef5169bba0d94cbd9da6813bbe (patch) | |
tree | ced1b912a3c42c797dc2ca4c34326ec6f3d86d7d | |
parent | eeef5ebd84d0d16d49e48640001b8367607350ba (diff) | |
download | go-f86c6b9a32daacef5169bba0d94cbd9da6813bbe.tar.gz go-f86c6b9a32daacef5169bba0d94cbd9da6813bbe.zip |
[release-branch.go1.18] cmd/compile: fix generic inter-inter comparisons from value switch statements
If value is a non-empty interface and has shape, we still need to
convert it to an interface{} first.
Fixes #53587
Change-Id: I516063ba4429a6cc24c483758387ec13815fc63e
Reviewed-on: https://go-review.googlesource.com/c/go/+/414834
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/414835
-rw-r--r-- | src/cmd/compile/internal/noder/stencil.go | 4 | ||||
-rw-r--r-- | test/typeparam/issue53477.go | 34 |
2 files changed, 36 insertions, 2 deletions
diff --git a/src/cmd/compile/internal/noder/stencil.go b/src/cmd/compile/internal/noder/stencil.go index 7cfd70ec546..4c3dd37d94f 100644 --- a/src/cmd/compile/internal/noder/stencil.go +++ b/src/cmd/compile/internal/noder/stencil.go @@ -1186,7 +1186,7 @@ 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 && !m.Tag.Type().IsInterface() && m.Tag.Type().HasShape() { + 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{}. m.Tag = assignconvfn(m.Tag, types.Types[types.TINTER]) @@ -1195,7 +1195,7 @@ func (subst *subster) node(n ir.Node) ir.Node { for i, x := range c.List { // If we have a case that is or has a type parameter, convert that case // to an interface{}. - if !x.Type().IsInterface() && x.Type().HasShape() { + if !x.Type().IsEmptyInterface() && x.Type().HasShape() { c.List[i] = assignconvfn(x, types.Types[types.TINTER]) } } diff --git a/test/typeparam/issue53477.go b/test/typeparam/issue53477.go new file mode 100644 index 00000000000..314171a7586 --- /dev/null +++ b/test/typeparam/issue53477.go @@ -0,0 +1,34 @@ +// run -gcflags=-G=3 + +// 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. + +// Test that generic interface-interface comparisons resulting from +// value switch statements are handled correctly. + +package main + +func main() { + f[X](0) +} + +type Mer[T any] interface{ M(T) } +type MNer[T any] interface { + Mer[T] + N() +} + +type X int + +func (X) M(X) {} +func (X) N() {} + +func f[T MNer[T]](t T) { + switch Mer[T](t) { + case MNer[T](t): + // ok + default: + panic("FAIL") + } +} |