]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: fix parameter order dependence in type inference
authorRobert Griesemer <gri@golang.org>
Tue, 21 Jun 2022 22:56:16 +0000 (15:56 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 22 Jun 2022 00:14:46 +0000 (00:14 +0000)
If we have more than two function arguments to a generic function,
we may have arguments with named and unnamed types. If that is the
case, permutate params and args such that the arguments with named
types are first in the list. This way, independent of parameter
ordering, the type inference will produce the same result.

This extra step is not explicitly outlined in the spec yet but we
all agree that (parameter) order independence is an invariant that
we should uphold for type inference. As we move towards less
operational and more descriptive rules for type inference, we will
incorporate this property as well.

The actual fix for this bug existed before 1.18 but was not enabled.
This CL merely enables the fix (switches a flag) and adjusts some
tests.

Fixes #43056.

Change-Id: Ie4e40cf8438dfd82fa94b78068e4f6f6f53f83e6
Reviewed-on: https://go-review.googlesource.com/c/go/+/413459
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/infer.go
src/cmd/compile/internal/types2/testdata/examples/functions.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue43056.go
src/go/types/infer.go
src/go/types/testdata/examples/functions.go
src/go/types/testdata/fixedbugs/issue43056.go

index b0c6a4fceac275c08e5c26fb26854183c4d9a8b2..8425cd603409d9333502024d9b53ab9dbaaaae22 100644 (file)
@@ -128,11 +128,8 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
        // named and unnamed types are passed to parameters with identical type, different types
        // (named vs underlying) may be inferred depending on the order of the arguments.
        // By ensuring that named types are seen first, order dependence is avoided and unification
-       // succeeds where it can.
-       //
-       // This code is disabled for now pending decision whether we want to address cases like
-       // these and make the spec on type inference more complicated (see issue #43056).
-       const enableArgSorting = false
+       // succeeds where it can (issue #43056).
+       const enableArgSorting = true
        if m := len(args); m >= 2 && enableArgSorting {
                // Determine indices of arguments with named and unnamed types.
                var named, unnamed []int
index ef8953cb43b92ff8c3b13d5d661533508077897a..d50f79d11f82f78f5849c5287a56be5493f82264 100644 (file)
@@ -182,7 +182,7 @@ func _() {
        type myString string
        var s1 string
        g3(nil, "1", myString("2"), "3")
-       g3(&s1, "1", myString /* ERROR does not match */ ("2"), "3")
+       g3(& /* ERROR does not match */ s1, "1", myString("2"), "3")
        _ = s1
 
        type myStruct struct{x int}
index 35c7ef592d2729b38445c9420ce62a1a504f8834..8ff4e7f9b4d8f763d25edbb024ab6dbeeb1e0336 100644 (file)
@@ -14,7 +14,7 @@ func _() {
        var j func(F)
 
        f(i, j)
-       // f(j, i) // disabled for now
+       f(j, i)
 }
 
 // example from issue
@@ -27,5 +27,5 @@ func _() {
        var j interface{ Equal(I) bool }
 
        g(i, j)
-       // g(j, i) // disabled for now
+       g(j, i)
 }
index 1aa26126387ad3fc21e55bd21695846001827c02..768efbf73ba5446a37062f00c82b1fe55a290804 100644 (file)
@@ -128,11 +128,8 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
        // named and unnamed types are passed to parameters with identical type, different types
        // (named vs underlying) may be inferred depending on the order of the arguments.
        // By ensuring that named types are seen first, order dependence is avoided and unification
-       // succeeds where it can.
-       //
-       // This code is disabled for now pending decision whether we want to address cases like
-       // these and make the spec on type inference more complicated (see issue #43056).
-       const enableArgSorting = false
+       // succeeds where it can (issue #43056).
+       const enableArgSorting = true
        if m := len(args); m >= 2 && enableArgSorting {
                // Determine indices of arguments with named and unnamed types.
                var named, unnamed []int
index 0af77267c5f56f9db551ac9fc838f5958dbe6fcb..1d30075c7c72dfdc876ae0d296897226a5fedfca 100644 (file)
@@ -182,7 +182,7 @@ func _() {
        type myString string
        var s1 string
        g3(nil, "1", myString("2"), "3")
-       g3(&s1, "1", myString /* ERROR does not match */ ("2"), "3")
+       g3(& /* ERROR does not match */ s1, "1", myString("2"), "3")
 
        type myStruct struct{x int}
        var s2 myStruct
index 35c7ef592d2729b38445c9420ce62a1a504f8834..8ff4e7f9b4d8f763d25edbb024ab6dbeeb1e0336 100644 (file)
@@ -14,7 +14,7 @@ func _() {
        var j func(F)
 
        f(i, j)
-       // f(j, i) // disabled for now
+       f(j, i)
 }
 
 // example from issue
@@ -27,5 +27,5 @@ func _() {
        var j interface{ Equal(I) bool }
 
        g(i, j)
-       // g(j, i) // disabled for now
+       g(j, i)
 }