aboutsummaryrefslogtreecommitdiff
path: root/test/typeparam
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2022-02-24 22:11:40 -0800
committerRobert Griesemer <gri@golang.org>2022-03-01 23:48:58 +0000
commit0807986fe6bf6040bafa9c415ab72e4cc8e519a4 (patch)
treee6bb077c1d040f916649c8847a1bcd9b9f291187 /test/typeparam
parentaaa3d39f270d7b9957f34f3d2a68decba63beffe (diff)
downloadgo-0807986fe6bf6040bafa9c415ab72e4cc8e519a4.tar.xz
go/types, types2: correctly consider ~ (tilde) in constraint type inference
When doing constraint type inference, we must consider whether the constraint's core type is precise (no tilde) or imprecise (tilde, or not a single specific type). In the latter case, we cannot infer an unknown type argument from the (imprecise) core type because there are infinitely many possible types. For instance, given [E ~byte] if we don't know E, we cannot infer that E must be byte (it could be myByte, etc.). On the other hand, if we do know the type argument, say for S in this example: [S ~[]E, E any] we must consider the underlying type of S when matching against ~[]E because we have a tilde. Because constraint type inference may infer type arguments that were not eligible initially (because they were unknown and the core type is imprecise), we must iterate the process until nothing changes any- more. For instance, given [S ~[]E, M ~map[string]S, E any] where we initially only know the type argument for M, we must ignore S (and E) at first. After one iteration of constraint type inference, S is known at which point we can infer E as well. The change is large-ish but the actual functional changes are small: - There's a new method "unknowns" to determine the number of as of yet unknown type arguments. - The adjCoreType function has been adjusted to also return tilde and single-type information. This is now conveniently returned as (*term, bool), and the function has been renamed to coreTerm. - The original constraint type inference loop has been adjusted to consider tilde information. - This adjusted original constraint type inference loop has been nested in another loop for iteration, together with some minimal logic to control termination. The remaining changes are modifications to tests: - There's a substantial new test for this issue. - Several existing test cases were adjusted to accomodate the fact that they inferred incorrect types: tildes have been removed throughout. Most of these tests are for pathological cases. - A couple of tests were adjusted where there was a difference between the go/types and types2 version. Fixes #51229. Change-Id: If0bf5fb70ec22913b5a2da89adbf8a27fbc921d9 Reviewed-on: https://go-review.googlesource.com/c/go/+/387977 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'test/typeparam')
-rw-r--r--test/typeparam/issue48424.go10
-rw-r--r--test/typeparam/settable.go2
-rw-r--r--test/typeparam/typelist.go8
3 files changed, 10 insertions, 10 deletions
diff --git a/test/typeparam/issue48424.go b/test/typeparam/issue48424.go
index b1238df697..c5e5d4b105 100644
--- a/test/typeparam/issue48424.go
+++ b/test/typeparam/issue48424.go
@@ -13,14 +13,14 @@ func identity[T int](x T) T {
return x
}
-func min[T int|string](x, y T) T {
+func min[T int | string](x, y T) T {
if x < y {
return x
}
return y
}
-func max[T ~float64](x, y T) T {
+func max[T ~int | ~float64](x, y T) T {
if x > y {
return x
}
@@ -48,7 +48,7 @@ func main() {
// Some random type parameter lists with elided interfaces.
type (
- _ [T struct{}] struct{}
- _ [M map[K]V, K comparable, V any] struct{}
- _ [_ interface{}|int] struct{}
+ _[T struct{}] struct{}
+ _[M map[K]V, K comparable, V any] struct{}
+ _[_ interface{} | int] struct{}
)
diff --git a/test/typeparam/settable.go b/test/typeparam/settable.go
index 1f7eba3558..56cf36745b 100644
--- a/test/typeparam/settable.go
+++ b/test/typeparam/settable.go
@@ -15,7 +15,7 @@ import (
type Setter[B any] interface {
Set(string)
- ~*B
+ *B
}
// Takes two type parameters where PT = *T
diff --git a/test/typeparam/typelist.go b/test/typeparam/typelist.go
index 27e1b60ab0..7c713212b0 100644
--- a/test/typeparam/typelist.go
+++ b/test/typeparam/typelist.go
@@ -89,7 +89,7 @@ func f1x() {
}
*/
-func f2[A any, B interface{ ~[]A }](_ A, _ B) {}
+func f2[A any, B interface{ []A }](_ A, _ B) {}
func f2x() {
f := f2[byte]
f(byte(0), []byte{})
@@ -109,7 +109,7 @@ func f3x() {
}
*/
-func f4[A any, B interface{ ~[]C }, C interface{ ~*A }](_ A, _ B, c C) {}
+func f4[A any, B interface{ []C }, C interface{ *A }](_ A, _ B, c C) {}
func f4x() {
f := f4[int]
var x int
@@ -118,11 +118,11 @@ func f4x() {
}
func f5[A interface {
- ~struct {
+ struct {
b B
c C
}
-}, B any, C interface{ ~*B }](x B) A {
+}, B any, C interface{ *B }](x B) A {
panic(0)
}
func f5x() {