[dev.go2go] cmd/compile/internal/types2: add special case report for generic map key type

Port of go/types CL https://golang.org/cl/247258. (The typeparamsB.go2
version of typeparams.go2 doesn't exist yet for types2.)

Updates #40551.

Change-Id: If2b329372bd4d160b4def8b66cc87f25ed4823fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/248057
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Robert Griesemer 2020-08-11 16:51:11 -07:00
parent cb2cbbcda2
commit fc29b9010e
2 changed files with 6 additions and 2 deletions

View File

@ -66,7 +66,7 @@ func new(type T)() *T {
var _ = new /* ERROR cannot use generic function new */
var _ *int = new(int)()
func _(type T)(map[T /* ERROR invalid map key type */]int) // w/o contract we don't know if T is comparable
func _(type T)(map[T /* ERROR invalid map key type T \(missing comparable constraint\) */]int) // w/o constraint we don't know if T is comparable
func f1(type T1)(struct{T1}) int
var _ = f1(int)(struct{T1}{})

View File

@ -550,7 +550,11 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *Named) (T Type) {
// it is safe to continue in any case (was issue 6667).
check.atEnd(func() {
if !Comparable(typ.key) {
check.errorf(e.Key.Pos(), "invalid map key type %s", typ.key)
var why string
if typ.key.TypeParam() != nil {
why = " (missing comparable constraint)"
}
check.errorf(e.Key.Pos(), "invalid map key type %s%s", typ.key, why)
}
})