]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.21] go/types, types2: use exact unification when comparing interf...
authorRobert Griesemer <gri@golang.org>
Mon, 14 Aug 2023 21:07:56 +0000 (14:07 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 17 Aug 2023 21:05:01 +0000 (21:05 +0000)
Irrespective of whether unification is exact or inexact, method
signatures of interfaces must always match exactly: a type never
satisfies/implements an interface if relevant method signatures
are different (i.e., not identical, possibly after substitution).

Fixes #61959.

Change-Id: I20c0aa28ac86e2edec615b40f2269938e4a96938
Reviewed-on: https://go-review.googlesource.com/c/go/+/519435
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/519416
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>

src/cmd/compile/internal/types2/unify.go
src/go/types/unify.go
src/internal/types/testdata/fixedbugs/issue61879.go [new file with mode: 0644]

index 94830e94f78ecb8cfc9e02c30e0afa4da9f5a8e6..eddd808cb77b56771555e5c9a2cb08fe2d68163c 100644 (file)
@@ -505,7 +505,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                        }
                        // All xmethods must exist in ymethods and corresponding signatures must unify.
                        for _, xm := range xmethods {
-                               if ym := ymap[xm.Id()]; ym == nil || !u.nify(xm.typ, ym.typ, emode, p) {
+                               if ym := ymap[xm.Id()]; ym == nil || !u.nify(xm.typ, ym.typ, exact, p) {
                                        return false
                                }
                        }
@@ -526,7 +526,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                        xmethods := xi.typeSet().methods
                        for _, xm := range xmethods {
                                obj, _, _ := LookupFieldOrMethod(y, false, xm.pkg, xm.name)
-                               if ym, _ := obj.(*Func); ym == nil || !u.nify(xm.typ, ym.typ, emode, p) {
+                               if ym, _ := obj.(*Func); ym == nil || !u.nify(xm.typ, ym.typ, exact, p) {
                                        return false
                                }
                        }
@@ -685,7 +685,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                                }
                                for i, f := range a {
                                        g := b[i]
-                                       if f.Id() != g.Id() || !u.nify(f.typ, g.typ, emode, q) {
+                                       if f.Id() != g.Id() || !u.nify(f.typ, g.typ, exact, q) {
                                                return false
                                        }
                                }
index bef851f4238e8ee4ad6e23fe53cde00975f774fe..56b258ae35a74abdae72bad3462c72131c6b4ca5 100644 (file)
@@ -507,7 +507,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                        }
                        // All xmethods must exist in ymethods and corresponding signatures must unify.
                        for _, xm := range xmethods {
-                               if ym := ymap[xm.Id()]; ym == nil || !u.nify(xm.typ, ym.typ, emode, p) {
+                               if ym := ymap[xm.Id()]; ym == nil || !u.nify(xm.typ, ym.typ, exact, p) {
                                        return false
                                }
                        }
@@ -528,7 +528,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                        xmethods := xi.typeSet().methods
                        for _, xm := range xmethods {
                                obj, _, _ := LookupFieldOrMethod(y, false, xm.pkg, xm.name)
-                               if ym, _ := obj.(*Func); ym == nil || !u.nify(xm.typ, ym.typ, emode, p) {
+                               if ym, _ := obj.(*Func); ym == nil || !u.nify(xm.typ, ym.typ, exact, p) {
                                        return false
                                }
                        }
@@ -687,7 +687,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                                }
                                for i, f := range a {
                                        g := b[i]
-                                       if f.Id() != g.Id() || !u.nify(f.typ, g.typ, emode, q) {
+                                       if f.Id() != g.Id() || !u.nify(f.typ, g.typ, exact, q) {
                                                return false
                                        }
                                }
diff --git a/src/internal/types/testdata/fixedbugs/issue61879.go b/src/internal/types/testdata/fixedbugs/issue61879.go
new file mode 100644 (file)
index 0000000..542bc2d
--- /dev/null
@@ -0,0 +1,57 @@
+// Copyright 2023 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
+
+import "fmt"
+
+type Interface[T any] interface {
+       m(Interface[T])
+}
+
+func f[S []Interface[T], T any](S) {}
+
+func _() {
+       var s []Interface[int]
+       f(s) // panic here
+}
+
+// Larger example from issue
+
+type InterfaceA[T comparable] interface {
+       setData(string) InterfaceA[T]
+}
+
+type ImplA[T comparable] struct {
+       data string
+       args []any
+}
+
+func NewInterfaceA[T comparable](args ...any) InterfaceA[T] {
+       return &ImplA[T]{
+               data: fmt.Sprintf("%v", args...),
+               args: args,
+       }
+}
+
+func (k *ImplA[T]) setData(data string) InterfaceA[T] {
+       k.data = data
+       return k
+}
+
+func Foo[M ~map[InterfaceA[T]]V, T comparable, V any](m M) {
+       // DO SOMETHING HERE
+       return
+}
+
+func Bar() {
+       keys := make([]InterfaceA[int], 0, 10)
+       m := make(map[InterfaceA[int]]int)
+       for i := 0; i < 10; i++ {
+               keys = append(keys, NewInterfaceA[int](i))
+               m[keys[i]] = i
+       }
+
+       Foo(m) // panic here
+}