Skip to content

Commit

Permalink
go/types, types2: add additional tests using core types during unific…
Browse files Browse the repository at this point in the history
…ation

This change adds tests that use a type parameter's core type during
function argument type inference, not just during constraint type
inference.

Also, fix a typo in a comment.

For #50755.

Change-Id: I0c3196bdce5338341e0b6dfd7c63efb2e43ace25
Reviewed-on: https://go-review.googlesource.com/c/go/+/385376
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
griesemer committed Feb 13, 2022
1 parent f03ab0e commit 5bd7348
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
24 changes: 22 additions & 2 deletions src/cmd/compile/internal/types2/testdata/fixedbugs/issue50755.go2
Expand Up @@ -4,12 +4,32 @@

package p

func f1[M1 map[K1]int, K1 comparable](m1 M1) {}
// The core type of M2 unifies with the type of m1
// during function argument type inference.
// M2's constraint is unnamed.
func f1[K1 comparable, E1 any](m1 map[K1]E1) {}

func f2[M2 map[K2]int, K2 comparable](m2 M2) {
func f2[M2 map[string]int](m2 M2) {
f1(m2)
}

// The core type of M3 unifies with the type of m1
// during function argument type inference.
// M3's constraint is named.
type Map3 map[string]int

func f3[M3 Map3](m3 M3) {
f1(m3)
}

// The core type of M5 unifies with the core type of M4
// during constraint type inference.
func f4[M4 map[K4]int, K4 comparable](m4 M4) {}

func f5[M5 map[K5]int, K5 comparable](m5 M5) {
f4(m5)
}

// test case from issue

func Copy[MC ~map[KC]VC, KC comparable, VC any](dst, src MC) {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/types2/unify.go
Expand Up @@ -27,7 +27,7 @@ import (
// parameter P ("x" side), but the argument type P must be left alone so
// that unification resolves the type parameter P to P.
//
// For bidirection unification, both sets are provided. This enables
// For bidirectional unification, both sets are provided. This enables
// unification to go from argument to parameter type and vice versa.
// For constraint type inference, we use bidirectional unification
// where both the x and y type parameters are identical. This is done
Expand Down
24 changes: 22 additions & 2 deletions src/go/types/testdata/fixedbugs/issue50755.go2
Expand Up @@ -4,12 +4,32 @@

package p

func f1[M1 map[K1]int, K1 comparable](m1 M1) {}
// The core type of M2 unifies with the type of m1
// during function argument type inference.
// M2's constraint is unnamed.
func f1[K1 comparable, E1 any](m1 map[K1]E1) {}

func f2[M2 map[K2]int, K2 comparable](m2 M2) {
func f2[M2 map[string]int](m2 M2) {
f1(m2)
}

// The core type of M3 unifies with the type of m1
// during function argument type inference.
// M3's constraint is named.
type Map3 map[string]int

func f3[M3 Map3](m3 M3) {
f1(m3)
}

// The core type of M5 unifies with the core type of M4
// during constraint type inference.
func f4[M4 map[K4]int, K4 comparable](m4 M4) {}

func f5[M5 map[K5]int, K5 comparable](m5 M5) {
f4(m5)
}

// test case from issue

func Copy[MC ~map[KC]VC, KC comparable, VC any](dst, src MC) {
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/unify.go
Expand Up @@ -27,7 +27,7 @@ import (
// parameter P ("x" side), but the argument type P must be left alone so
// that unification resolves the type parameter P to P.
//
// For bidirection unification, both sets are provided. This enables
// For bidirectional unification, both sets are provided. This enables
// unification to go from argument to parameter type and vice versa.
// For constraint type inference, we use bidirectional unification
// where both the x and y type parameters are identical. This is done
Expand Down

0 comments on commit 5bd7348

Please sign in to comment.