]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix identical to recognize any and interface{}
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Tue, 30 Nov 2021 09:58:36 +0000 (16:58 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 1 Dec 2021 10:19:34 +0000 (10:19 +0000)
Currently, identical handles any and interface{} by checking against
Types[TINTER]. This is not always true, since when two generated
interface{} types may not use the same *Type instance.

Instead, we must check whether Type is empty interface or not.

Fixes #49875

Change-Id: I28fe4fc0100041a01bb03da795cfe8232b515fc4
Reviewed-on: https://go-review.googlesource.com/c/go/+/367754
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/types/identity.go
src/cmd/compile/internal/types/type.go
test/typeparam/issue49875.go [new file with mode: 0644]

index 89343b8419c894fa1217d9d88adf0dfcff87145b..f99e50a1c3851a21c241e8fe1ed929f1ffe80bb1 100644 (file)
@@ -59,7 +59,8 @@ func identical(t1, t2 *Type, flags int, assumedEqual map[typePair]struct{}) bool
                case TINT32:
                        return (t1 == Types[TINT32] || t1 == RuneType) && (t2 == Types[TINT32] || t2 == RuneType)
                case TINTER:
-                       return (t1 == Types[TINTER] || t1 == AnyType) && (t2 == Types[TINTER] || t2 == AnyType)
+                       // Make sure named any type matches any empty interface.
+                       return t1 == AnyType && t2.IsEmptyInterface() || t2 == AnyType && t1.IsEmptyInterface()
                default:
                        return false
                }
index b1194fa1962b5c21c2c6e67145ee456535d5ccf2..7d22e2da234e6fd8b3d075ff956fe198888cb22e 100644 (file)
@@ -1211,7 +1211,8 @@ func (t *Type) cmp(x *Type) Cmp {
                        }
 
                case TINTER:
-                       if (t == Types[AnyType.kind] || t == AnyType) && (x == Types[AnyType.kind] || x == AnyType) {
+                       // Make sure named any type matches any empty interface.
+                       if t == AnyType && x.IsEmptyInterface() || x == AnyType && t.IsEmptyInterface() {
                                return CMPeq
                        }
                }
diff --git a/test/typeparam/issue49875.go b/test/typeparam/issue49875.go
new file mode 100644 (file)
index 0000000..aece7de
--- /dev/null
@@ -0,0 +1,14 @@
+// compile -G=3
+
+// Copyright 2021 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 p
+
+func f(args ...interface{}) {}
+
+func g() {
+       var args []any
+       f(args...)
+}