diff options
author | Robert Griesemer <gri@golang.org> | 2023-05-03 17:47:29 -0700 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2023-05-04 17:01:49 +0000 |
commit | 8337ca094beb07001cba12a1f88926dff78c1bcd (patch) | |
tree | 8511b6b67376de4412a70e20025522413274a9a3 /src/internal | |
parent | 8dea63547035e2246e09ba96d0338ad9ba8b39ad (diff) | |
download | go-git-8337ca094beb07001cba12a1f88926dff78c1bcd.tar.gz |
go/types, types2: rename generic function arguments
For correct inference, if the same generic function is provided
more than once as an argument to another function, the argument
function's type parameters must be unique for each argument so
that the type parameters can be correctly inferred.
Example:
func f(func(int), func(string)) {}
func g[P any](P) {}
func _() {
f(g, g)
}
Here the type parameter P for the first g argument resolves to int
and the type parameter P for the second g argument resolves to string.
Fixes #59956.
For #59338.
Change-Id: I10ce0ea08c2033722dd7c7c976b2a5448b2ee2d8
Reviewed-on: https://go-review.googlesource.com/c/go/+/492516
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Diffstat (limited to 'src/internal')
-rw-r--r-- | src/internal/types/testdata/examples/inference2.go | 4 | ||||
-rw-r--r-- | src/internal/types/testdata/fixedbugs/issue59956.go | 49 |
2 files changed, 50 insertions, 3 deletions
diff --git a/src/internal/types/testdata/examples/inference2.go b/src/internal/types/testdata/examples/inference2.go index 80acc828dd..7b86266d5e 100644 --- a/src/internal/types/testdata/examples/inference2.go +++ b/src/internal/types/testdata/examples/inference2.go @@ -88,7 +88,5 @@ func _() { g2(f4) g4(f6) g5(f6, f7) - - // TODO(gri) this should work (requires type parameter renaming for f1) - g6(f1, f1 /* ERROR "type func[P any](P) of f1 does not match func(string)" */) + g6(f1, f1) } diff --git a/src/internal/types/testdata/fixedbugs/issue59956.go b/src/internal/types/testdata/fixedbugs/issue59956.go new file mode 100644 index 0000000000..33b05d72c1 --- /dev/null +++ b/src/internal/types/testdata/fixedbugs/issue59956.go @@ -0,0 +1,49 @@ +// -reverseTypeInference + +// 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 + +func f1(func(int)) +func f2(func(int), func(string)) +func f3(func(int), func(string), func(float32)) + +func g1[P any](P) {} + +func _() { + f1(g1) + f2(g1, g1) + f3(g1, g1, g1) +} + +// More complex examples + +func g2[P any](P, P) {} +func h3[P any](func(P), func(P), func() P) {} +func h4[P, Q any](func(P), func(P, Q), func() Q, func(P, Q)) {} + +func r1() int { return 0 } + +func _() { + h3(g1, g1, r1) + h4(g1, g2, r1, g2) +} + +// Variadic cases + +func f(func(int)) +func g[P any](P) {} + +func d[P any](...func(P)) {} + +func _() { + d /* ERROR "cannot infer P" */ () + d(f) + d(f, g) + d(f, g, g) + d /* ERROR "cannot infer P" */ (g, g, g) + d(g, g, f) + d(g, f, g, f) +} |