Confirm that the current implementation of core type unification
looks correct and update the respective comment. Add an extra test.
Fixes #51376.
Change-Id: I6a603a4baeee2ede5bb4a1d60766204a808936d7
Reviewed-on: https://go-review.googlesource.com/c/go/+/388294
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>
(cherry picked from commit
6da16b6ad5787a043fc9978d2d009934e3b2e165)
Reviewed-on: https://go-review.googlesource.com/c/go/+/390016
Trust: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
--- /dev/null
+// Copyright 2022 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
+
+type Map map[string]int
+
+func f[M ~map[K]V, K comparable, V any](M) {}
+func g[M map[K]V, K comparable, V any](M) {}
+
+func _[M1 ~map[K]V, M2 map[K]V, K comparable, V any]() {
+ var m1 M1
+ f(m1)
+ g( /* ERROR M1 does not implement map\[K\]V */ m1) // M1 has tilde
+
+ var m2 M2
+ f(m2)
+ g(m2) // M1 does not have tilde
+
+ var m3 Map
+ f(m3)
+ g( /* ERROR Map does not implement map\[string\]int */ m3) // M in g does not have tilde
+}
// (see issue #50755 for a test case).
if enableCoreTypeUnification && !u.exact {
if isTypeParam(x) && !hasName(y) {
- // Caution: This may not be correct in light of ~ constraints.
- // See issue #51376.
- // TODO(gri) investigate!
- //
// When considering the type parameter for unification
- // we look at the adjusted core type (coreTerm).
+ // we look at the adjusted core term (adjusted core type
+ // with tilde information).
// If the adjusted core type is a named type N; the
// corresponding core type is under(N). Since !u.exact
// and y doesn't have a name, unification will end up
// comparing under(N) to y, so we can just use the core
- // type instead. Optimization.
+ // type instead. And we can ignore the tilde because we
+ // already look at the underlying types on both sides
+ // and we have known types on both sides.
+ // Optimization.
if cx := coreType(x); cx != nil {
if traceInference {
u.tracef("core %s ≡ %s", x, y)
--- /dev/null
+// Copyright 2022 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
+
+type Map map[string]int
+
+func f[M ~map[K]V, K comparable, V any](M) {}
+func g[M map[K]V, K comparable, V any](M) {}
+
+func _[M1 ~map[K]V, M2 map[K]V, K comparable, V any]() {
+ var m1 M1
+ f(m1)
+ g /* ERROR M1 does not implement map\[K\]V */ (m1) // M1 has tilde
+
+ var m2 M2
+ f(m2)
+ g(m2) // M1 does not have tilde
+
+ var m3 Map
+ f(m3)
+ g /* ERROR Map does not implement map\[string\]int */ (m3) // M in g does not have tilde
+}
// (see issue #50755 for a test case).
if enableCoreTypeUnification && !u.exact {
if isTypeParam(x) && !hasName(y) {
- // Caution: This may not be correct in light of ~ constraints.
- // See issue #51376.
- // TODO(gri) investigate!
- //
// When considering the type parameter for unification
- // we look at the adjusted core type (coreTerm).
+ // we look at the adjusted core term (adjusted core type
+ // with tilde information).
// If the adjusted core type is a named type N; the
// corresponding core type is under(N). Since !u.exact
// and y doesn't have a name, unification will end up
// comparing under(N) to y, so we can just use the core
- // type instead. Optimization.
+ // type instead. And we can ignore the tilde because we
+ // already look at the underlying types on both sides
+ // and we have known types on both sides.
+ // Optimization.
if cx := coreType(x); cx != nil {
if traceInference {
u.tracef("core %s ≡ %s", x, y)