Skip to content

Commit fc29b90

Browse files
committed
[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 <[email protected]>
1 parent cb2cbbc commit fc29b90

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/cmd/compile/internal/types2/testdata/typeparams.go2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func new(type T)() *T {
6666
var _ = new /* ERROR cannot use generic function new */
6767
var _ *int = new(int)()
6868

69-
func _(type T)(map[T /* ERROR invalid map key type */]int) // w/o contract we don't know if T is comparable
69+
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
7070

7171
func f1(type T1)(struct{T1}) int
7272
var _ = f1(int)(struct{T1}{})

src/cmd/compile/internal/types2/typexpr.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,11 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *Named) (T Type) {
550550
// it is safe to continue in any case (was issue 6667).
551551
check.atEnd(func() {
552552
if !Comparable(typ.key) {
553-
check.errorf(e.Key.Pos(), "invalid map key type %s", typ.key)
553+
var why string
554+
if typ.key.TypeParam() != nil {
555+
why = " (missing comparable constraint)"
556+
}
557+
check.errorf(e.Key.Pos(), "invalid map key type %s%s", typ.key, why)
554558
}
555559
})
556560

0 commit comments

Comments
 (0)