]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: partial revert of incorrect unification "fix"
authorRobert Griesemer <gri@golang.org>
Fri, 8 Oct 2021 00:02:53 +0000 (17:02 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 8 Oct 2021 00:24:52 +0000 (00:24 +0000)
This is a port of CL 354690 from types2 to go/types.

Change-Id: I50e7297a67e37d261335260e285b9cb1c0d2a62d
Reviewed-on: https://go-review.googlesource.com/c/go/+/354691
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/go/types/testdata/fixedbugs/issue48619.go2
src/go/types/testdata/fixedbugs/issue48656.go2
src/go/types/testdata/fixedbugs/issue48695.go2 [new file with mode: 0644]
src/go/types/unify.go

index 24650a3a703a8e8aff725ad0f78a190ea917caa6..870bacd0bd25ab2d368906d091c66e3db1235e39 100644 (file)
@@ -2,21 +2,23 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// This issue has been re-opened.
+
 package p
 
 func f[P any](a, _ P) {
-       var x int
-       f(a, x /* ERROR type int of x does not match P */)
-       f(x, a /* ERROR type P of a does not match inferred type int for P */)
+       // var x int
+       // f(a, x /* ERROR type int of x does not match P */)
+       // f(x, a /* ERROR type P of a does not match inferred type int for P */)
 }
 
 func g[P any](a, b P) {
-       g(a, b)
-       g(&a, &b)
-       g([]P{}, []P{})
+       // g(a, b)
+       // g(&a, &b)
+       // g([]P{}, []P{})
 }
 
 func h[P any](a, b P) {
-       h(&a, &b)
-       h([]P{a}, []P{b})
+       // h(&a, &b)
+       // h([]P{a}, []P{b})
 }
index ee639e10428e50517f14bfd4801aad73d5c30fe8..52863d446b6bdf2e805c30b0b383e5592f2ffbda 100644 (file)
@@ -2,12 +2,11 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package p
+// This issue is still open.
 
-// TODO(gri) Still need better error positions and message here.
-//           But this doesn't crash anymore.
+package p
 
-func f[P /* ERROR does not match \*Q */ interface{*Q}, Q any](p P, q Q) {
-       _ = f[P]
-        _ = f /* ERROR cannot infer P */ [*P]
+func f[P interface{*Q}, Q any](p P, q Q) {
+       // _ = f[P]
+        // _ = f[/* ERROR cannot infer P */ *P]
 }
diff --git a/src/go/types/testdata/fixedbugs/issue48695.go2 b/src/go/types/testdata/fixedbugs/issue48695.go2
new file mode 100644 (file)
index 0000000..2d9e6a5
--- /dev/null
@@ -0,0 +1,14 @@
+// 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 g[P interface{~func(T) P}, T any](P) {}
+
+func _() {
+       type F func(int) F
+       var f F
+       g(f)
+       _ = g[F]
+}
index 984ba59e6d896f15a3ce8c78f9ddd29d575083bd..99c9c9e61416aaa7f79feda3dcadd1c226574bc8 100644 (file)
@@ -63,10 +63,6 @@ func (u *unifier) unify(x, y Type) bool {
 type tparamsList struct {
        unifier *unifier
        tparams []*TypeParam
-       // For each tparams element, there is a corresponding mask bit in masks.
-       // If set, the corresponding type parameter is masked and doesn't appear
-       // as a type parameter with tparamsList.index.
-       masks []bool
        // For each tparams element, there is a corresponding type slot index in indices.
        // index  < 0: unifier.types[-index-1] == nil
        // index == 0: no type slot allocated yet
@@ -107,14 +103,9 @@ func (d *tparamsList) init(tparams []*TypeParam) {
                }
        }
        d.tparams = tparams
-       d.masks = make([]bool, len(tparams))
        d.indices = make([]int, len(tparams))
 }
 
-// mask and unmask permit the masking/unmasking of the i'th type parameter of d.
-func (d *tparamsList) mask(i int)   { d.masks[i] = true }
-func (d *tparamsList) unmask(i int) { d.masks[i] = false }
-
 // join unifies the i'th type parameter of x with the j'th type parameter of y.
 // If both type parameters already have a type associated with them and they are
 // not joined, join fails and returns false.
@@ -158,13 +149,11 @@ func (u *unifier) join(i, j int) bool {
        return true
 }
 
-// If typ is an unmasked type parameter of d, index returns the type parameter index.
+// If typ is a type parameter of d, index returns the type parameter index.
 // Otherwise, the result is < 0.
 func (d *tparamsList) index(typ Type) int {
        if tpar, ok := typ.(*TypeParam); ok {
-               if i := tparamIndex(d.tparams, tpar); i >= 0 && !d.masks[i] {
-                       return i
-               }
+               return tparamIndex(d.tparams, tpar)
        }
        return -1
 }
@@ -257,7 +246,7 @@ func (u *unifier) nify(x, y Type, p *ifacePair) bool {
                }
        }
 
-       // Cases where at least one of x or y is an (unmasked) type parameter.
+       // Cases where at least one of x or y is a type parameter.
        switch i, j := u.x.index(x), u.y.index(y); {
        case i >= 0 && j >= 0:
                // both x and y are type parameters
@@ -270,12 +259,6 @@ func (u *unifier) nify(x, y Type, p *ifacePair) bool {
        case i >= 0:
                // x is a type parameter, y is not
                if tx := u.x.at(i); tx != nil {
-                       // The inferred type tx may be or contain x again but we don't
-                       // want to "unpack" it again when unifying tx with y: tx is the
-                       // inferred type. Mask type parameter x for this recursion, so
-                       // that subsequent encounters treat x like an ordinary type.
-                       u.x.mask(i)
-                       defer u.x.unmask(i)
                        return u.nifyEq(tx, y, p)
                }
                // otherwise, infer type from y
@@ -285,9 +268,6 @@ func (u *unifier) nify(x, y Type, p *ifacePair) bool {
        case j >= 0:
                // y is a type parameter, x is not
                if ty := u.y.at(j); ty != nil {
-                       // see comment above
-                       u.y.mask(j)
-                       defer u.y.unmask(j)
                        return u.nifyEq(x, ty, p)
                }
                // otherwise, infer type from x