Skip to content

Commit b004c73

Browse files
committed
go/types, types2: fix parameter order dependence in type inference
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 <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent f2c7e78 commit b004c73

File tree

6 files changed

+10
-16
lines changed

6 files changed

+10
-16
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,8 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
128128
// named and unnamed types are passed to parameters with identical type, different types
129129
// (named vs underlying) may be inferred depending on the order of the arguments.
130130
// By ensuring that named types are seen first, order dependence is avoided and unification
131-
// succeeds where it can.
132-
//
133-
// This code is disabled for now pending decision whether we want to address cases like
134-
// these and make the spec on type inference more complicated (see issue #43056).
135-
const enableArgSorting = false
131+
// succeeds where it can (issue #43056).
132+
const enableArgSorting = true
136133
if m := len(args); m >= 2 && enableArgSorting {
137134
// Determine indices of arguments with named and unnamed types.
138135
var named, unnamed []int

src/cmd/compile/internal/types2/testdata/examples/functions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func _() {
182182
type myString string
183183
var s1 string
184184
g3(nil, "1", myString("2"), "3")
185-
g3(&s1, "1", myString /* ERROR does not match */ ("2"), "3")
185+
g3(& /* ERROR does not match */ s1, "1", myString("2"), "3")
186186
_ = s1
187187

188188
type myStruct struct{x int}

src/cmd/compile/internal/types2/testdata/fixedbugs/issue43056.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func _() {
1414
var j func(F)
1515

1616
f(i, j)
17-
// f(j, i) // disabled for now
17+
f(j, i)
1818
}
1919

2020
// example from issue
@@ -27,5 +27,5 @@ func _() {
2727
var j interface{ Equal(I) bool }
2828

2929
g(i, j)
30-
// g(j, i) // disabled for now
30+
g(j, i)
3131
}

src/go/types/infer.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,8 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
128128
// named and unnamed types are passed to parameters with identical type, different types
129129
// (named vs underlying) may be inferred depending on the order of the arguments.
130130
// By ensuring that named types are seen first, order dependence is avoided and unification
131-
// succeeds where it can.
132-
//
133-
// This code is disabled for now pending decision whether we want to address cases like
134-
// these and make the spec on type inference more complicated (see issue #43056).
135-
const enableArgSorting = false
131+
// succeeds where it can (issue #43056).
132+
const enableArgSorting = true
136133
if m := len(args); m >= 2 && enableArgSorting {
137134
// Determine indices of arguments with named and unnamed types.
138135
var named, unnamed []int

src/go/types/testdata/examples/functions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func _() {
182182
type myString string
183183
var s1 string
184184
g3(nil, "1", myString("2"), "3")
185-
g3(&s1, "1", myString /* ERROR does not match */ ("2"), "3")
185+
g3(& /* ERROR does not match */ s1, "1", myString("2"), "3")
186186

187187
type myStruct struct{x int}
188188
var s2 myStruct

src/go/types/testdata/fixedbugs/issue43056.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func _() {
1414
var j func(F)
1515

1616
f(i, j)
17-
// f(j, i) // disabled for now
17+
f(j, i)
1818
}
1919

2020
// example from issue
@@ -27,5 +27,5 @@ func _() {
2727
var j interface{ Equal(I) bool }
2828

2929
g(i, j)
30-
// g(j, i) // disabled for now
30+
g(j, i)
3131
}

0 commit comments

Comments
 (0)