]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: correct type identity comparison with "any"
authorCherry Mui <cherryyz@google.com>
Wed, 15 Dec 2021 17:34:34 +0000 (12:34 -0500)
committerCherry Mui <cherryyz@google.com>
Wed, 15 Dec 2021 20:26:03 +0000 (20:26 +0000)
The builtin "any" type should only be identical to an unnamed empty
interface type, not a defined empty interface type.

Fixes #50169.

Change-Id: Ie5bb88868497cb795de1fd0276133ba9812edfe4
Reviewed-on: https://go-review.googlesource.com/c/go/+/372217
Trust: Cherry Mui <cherryyz@google.com>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/compile/internal/types/identity.go
test/fixedbugs/issue50169.go [new file with mode: 0644]

index a164b84da929b3259396cc0b7410c405cc7c9406..60a0f2e7c581a9bbc35165d1b19166dca1690a3f 100644 (file)
@@ -59,12 +59,13 @@ 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:
-                       // Make sure named any type matches any empty interface
+                       // Make sure named any type matches any unnamed empty interface
                        // (but not a shape type, if identStrict).
+                       isUnnamedEface := func(t *Type) bool { return t.IsEmptyInterface() && t.Sym() == nil }
                        if flags&identStrict != 0 {
-                               return t1 == AnyType && t2.IsEmptyInterface() && !t2.HasShape() || t2 == AnyType && t1.IsEmptyInterface() && !t1.HasShape()
+                               return t1 == AnyType && isUnnamedEface(t2) && !t2.HasShape() || t2 == AnyType && isUnnamedEface(t1) && !t1.HasShape()
                        }
-                       return t1 == AnyType && t2.IsEmptyInterface() || t2 == AnyType && t1.IsEmptyInterface()
+                       return t1 == AnyType && isUnnamedEface(t2) || t2 == AnyType && isUnnamedEface(t1)
                default:
                        return false
                }
diff --git a/test/fixedbugs/issue50169.go b/test/fixedbugs/issue50169.go
new file mode 100644 (file)
index 0000000..30d2713
--- /dev/null
@@ -0,0 +1,24 @@
+// compile
+
+// 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 main
+
+func main() {
+       var x Value
+       NewScanner().Scan(x)
+}
+
+type Value any
+
+type Scanner interface{ Scan(any) error }
+
+func NewScanner() Scanner {
+       return &t{}
+}
+
+type t struct{}
+
+func (*t) Scan(interface{}) error { return nil }