Skip to content

Commit 5780be4

Browse files
committed
cmd/compile/internal/types2: partial revert of incorrect unification "fix"
The "fix" (CL 352832) for #48619 was incorrect and broke the unification algorithm in some cases (e.g., #48695). This CL reverts the changes made by CL 352832 to unify.go, and comments out code in corresponding tests. As a result, #48695 will be fixed, and we will re-open #48619. Fixes #48695. For #48619. For #48656. Change-Id: I91bc492062dbcc8dae7626f6b33f6dfabf48bcb8 Reviewed-on: https://go-review.googlesource.com/c/go/+/354690 Trust: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 7cef831 commit 5780be4

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

src/cmd/compile/internal/types2/testdata/fixedbugs/issue48619.go2

+10-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// This issue has been re-opened.
6+
57
package p
68

79
func f[P any](a, _ P) {
8-
var x int
9-
f(a, x /* ERROR type int of x does not match P */)
10-
f(x, a /* ERROR type P of a does not match inferred type int for P */)
10+
// var x int
11+
// f(a, x /* ERROR type int of x does not match P */)
12+
// f(x, a /* ERROR type P of a does not match inferred type int for P */)
1113
}
1214

1315
func g[P any](a, b P) {
14-
g(a, b)
15-
g(&a, &b)
16-
g([]P{}, []P{})
16+
// g(a, b)
17+
// g(&a, &b)
18+
// g([]P{}, []P{})
1719
}
1820

1921
func h[P any](a, b P) {
20-
h(&a, &b)
21-
h([]P{a}, []P{b})
22+
// h(&a, &b)
23+
// h([]P{a}, []P{b})
2224
}

src/cmd/compile/internal/types2/testdata/fixedbugs/issue48656.go2

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package p
5+
// This issue is still open.
66

7-
// TODO(gri) Still need better error positions and message here.
8-
// But this doesn't crash anymore.
7+
package p
98

10-
func f[P /* ERROR does not match \*Q */ interface{*Q}, Q any](p P, q Q) {
11-
_ = f[P]
12-
_ = f[/* ERROR cannot infer P */ *P]
9+
func f[P *Q, Q any](p P, q Q) {
10+
// _ = f[P]
11+
// _ = f[/* ERROR cannot infer P */ *P]
1312
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
func g[P ~func(T) P, T any](P) {}
8+
9+
func _() {
10+
type F func(int) F
11+
var f F
12+
g(f)
13+
_ = g[F]
14+
}

src/cmd/compile/internal/types2/unify.go

+3-23
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ func (u *unifier) unify(x, y Type) bool {
6363
type tparamsList struct {
6464
unifier *unifier
6565
tparams []*TypeParam
66-
// For each tparams element, there is a corresponding mask bit in masks.
67-
// If set, the corresponding type parameter is masked and doesn't appear
68-
// as a type parameter with tparamsList.index.
69-
masks []bool
7066
// For each tparams element, there is a corresponding type slot index in indices.
7167
// index < 0: unifier.types[-index-1] == nil
7268
// index == 0: no type slot allocated yet
@@ -107,14 +103,9 @@ func (d *tparamsList) init(tparams []*TypeParam) {
107103
}
108104
}
109105
d.tparams = tparams
110-
d.masks = make([]bool, len(tparams))
111106
d.indices = make([]int, len(tparams))
112107
}
113108

114-
// mask and unmask permit the masking/unmasking of the i'th type parameter of d.
115-
func (d *tparamsList) mask(i int) { d.masks[i] = true }
116-
func (d *tparamsList) unmask(i int) { d.masks[i] = false }
117-
118109
// join unifies the i'th type parameter of x with the j'th type parameter of y.
119110
// If both type parameters already have a type associated with them and they are
120111
// not joined, join fails and returns false.
@@ -158,13 +149,11 @@ func (u *unifier) join(i, j int) bool {
158149
return true
159150
}
160151

161-
// If typ is an unmasked type parameter of d, index returns the type parameter index.
152+
// If typ is a type parameter of d, index returns the type parameter index.
162153
// Otherwise, the result is < 0.
163154
func (d *tparamsList) index(typ Type) int {
164155
if tpar, ok := typ.(*TypeParam); ok {
165-
if i := tparamIndex(d.tparams, tpar); i >= 0 && !d.masks[i] {
166-
return i
167-
}
156+
return tparamIndex(d.tparams, tpar)
168157
}
169158
return -1
170159
}
@@ -257,7 +246,7 @@ func (u *unifier) nify(x, y Type, p *ifacePair) bool {
257246
}
258247
}
259248

260-
// Cases where at least one of x or y is an (unmasked) type parameter.
249+
// Cases where at least one of x or y is a type parameter.
261250
switch i, j := u.x.index(x), u.y.index(y); {
262251
case i >= 0 && j >= 0:
263252
// both x and y are type parameters
@@ -270,12 +259,6 @@ func (u *unifier) nify(x, y Type, p *ifacePair) bool {
270259
case i >= 0:
271260
// x is a type parameter, y is not
272261
if tx := u.x.at(i); tx != nil {
273-
// The inferred type tx may be or contain x again but we don't
274-
// want to "unpack" it again when unifying tx with y: tx is the
275-
// inferred type. Mask type parameter x for this recursion, so
276-
// that subsequent encounters treat x like an ordinary type.
277-
u.x.mask(i)
278-
defer u.x.unmask(i)
279262
return u.nifyEq(tx, y, p)
280263
}
281264
// otherwise, infer type from y
@@ -285,9 +268,6 @@ func (u *unifier) nify(x, y Type, p *ifacePair) bool {
285268
case j >= 0:
286269
// y is a type parameter, x is not
287270
if ty := u.y.at(j); ty != nil {
288-
// see comment above
289-
u.y.mask(j)
290-
defer u.y.unmask(j)
291271
return u.nifyEq(x, ty, p)
292272
}
293273
// otherwise, infer type from x

0 commit comments

Comments
 (0)