From 6bedf4a2b45068c486c69a04410f2c2469152f2d Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 18 Aug 2022 16:59:10 -0700 Subject: [PATCH] go/types: match types2 error for invalid map key Use "invalid" rather than "incomparable" in error message for map key types that are not comparable. This is the original compiler error message and many tests check for this specific message. The type checker does provide an additional explanation if the reason for the error is not obvious (e.g. for type parameters). For #54511. Change-Id: Idb76c48b4dfbfd66a7deac728a552e07f14e06d7 Reviewed-on: https://go-review.googlesource.com/c/go/+/424905 Reviewed-by: Robert Findley Reviewed-by: Alan Donovan Reviewed-by: Robert Griesemer --- src/go/types/testdata/check/cycles0.go | 8 ++++---- src/go/types/testdata/check/typeparams.go | 2 +- src/go/types/typexpr.go | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/go/types/testdata/check/cycles0.go b/src/go/types/testdata/check/cycles0.go index 27b6111822..998f9f7da9 100644 --- a/src/go/types/testdata/check/cycles0.go +++ b/src/go/types/testdata/check/cycles0.go @@ -66,7 +66,7 @@ type ( I6 interface{ I5 } // maps - M0 map[M0 /* ERROR incomparable map key */ ]M0 + M0 map[M0 /* ERROR invalid map key */ ]M0 // channels C0 chan C0 @@ -115,7 +115,7 @@ func _() { i0 /* ERROR cycle */ interface{ i0 } // maps - m0 map[m0 /* ERROR incomparable map key */ ]m0 + m0 map[m0 /* ERROR invalid map key */ ]m0 // channels c0 chan c0 @@ -124,10 +124,10 @@ func _() { // test cases for issue 6667 -type A [10]map[A /* ERROR incomparable map key */ ]bool +type A [10]map[A /* ERROR invalid map key */ ]bool type S struct { - m map[S /* ERROR incomparable map key */ ]bool + m map[S /* ERROR invalid map key */ ]bool } // test cases for issue 7236 diff --git a/src/go/types/testdata/check/typeparams.go b/src/go/types/testdata/check/typeparams.go index 199828f55f..95bd3e46b9 100644 --- a/src/go/types/testdata/check/typeparams.go +++ b/src/go/types/testdata/check/typeparams.go @@ -75,7 +75,7 @@ func new[T any]() *T { var _ = new /* ERROR cannot use generic function new */ var _ *int = new[int]() -func _[T any](map[T /* ERROR incomparable map key type T \(missing comparable constraint\) */]int) {} // w/o constraint we don't know if T is comparable +func _[T any](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[T1 any](struct{T1 /* ERROR cannot be a .* type parameter */ }) int { panic(0) } var _ = f1[int](struct{T1}{}) diff --git a/src/go/types/typexpr.go b/src/go/types/typexpr.go index b02929df22..397bd75db3 100644 --- a/src/go/types/typexpr.go +++ b/src/go/types/typexpr.go @@ -348,7 +348,7 @@ func (check *Checker) typInternal(e0 ast.Expr, def *Named) (T Type) { if isTypeParam(typ.key) { why = " (missing comparable constraint)" } - check.errorf(e.Key, _IncomparableMapKey, "incomparable map key type %s%s", typ.key, why) + check.errorf(e.Key, _IncomparableMapKey, "invalid map key type %s%s", typ.key, why) } }).describef(e.Key, "check map key %s", typ.key) -- 2.50.0