Skip to content

Commit 6d33075

Browse files
tdakkotagriesemer
authored andcommitted
[dev.go2go] go/types: add special case report for generic map key type
Fixes #40551. Change-Id: Ifc10dbdcdbee6af5bdbfc36ec78e1a4361672acc GitHub-Last-Rev: 8d373de GitHub-Pull-Request: #40628 Reviewed-on: https://go-review.googlesource.com/c/go/+/247258 Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent ead7e9b commit 6d33075

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

src/go/types/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 constraint 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/go/types/testdata/typeparamsB.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 constraint 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/go/types/typexpr.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,11 @@ func (check *Checker) typInternal(e0 ast.Expr, def *Named) (T Type) {
528528
// it is safe to continue in any case (was issue 6667).
529529
check.atEnd(func() {
530530
if !Comparable(typ.key) {
531-
check.errorf(e.Key.Pos(), "invalid map key type %s", typ.key)
531+
var why string
532+
if typ.key.TypeParam() != nil {
533+
why = " (missing comparable constraint)"
534+
}
535+
check.errorf(e.Key.Pos(), "invalid map key type %s%s", typ.key, why)
532536
}
533537
})
534538

0 commit comments

Comments
 (0)