]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.18] cmd/compile: fix generic inter-inter comparisons from value...
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Tue, 28 Jun 2022 17:07:37 +0000 (00:07 +0700)
committerHeschi Kreinick <heschi@google.com>
Wed, 6 Jul 2022 19:33:49 +0000 (19:33 +0000)
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

src/cmd/compile/internal/noder/stencil.go
test/typeparam/issue53477.go [new file with mode: 0644]

index 7cfd70ec546cb272939db2e67dedc26d99c1572f..4c3dd37d94f0c4c7cc0f90dd448a03cad41d064e 100644 (file)
@@ -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 (file)
index 0000000..314171a
--- /dev/null
@@ -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")
+       }
+}