Skip to content

Commit

Permalink
[dev.go2go] go/types: add special case report for generic map key type
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Aug 6, 2020
1 parent ead7e9b commit c128b8f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/go/types/testdata/typeparams.go2
Expand Up @@ -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 constraint we don't know if T is comparable
func _(type T)(map[T /* ERROR map key type T does not have 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}{})
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/testdata/typeparamsB.go2
Expand Up @@ -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 constraint we don't know if T is comparable
func _[type T](map[T /* ERROR map key type T does not have 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}{})
Expand Down
7 changes: 6 additions & 1 deletion src/go/types/typexpr.go
Expand Up @@ -528,7 +528,12 @@ func (check *Checker) typInternal(e0 ast.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)
if typ.key.TypeParam() != nil {
check.errorf(e.Key.Pos(), "map key type %s does not have comparable constraint", typ.key)
} else {
check.errorf(e.Key.Pos(), "invalid map key type %s", typ.key)
}

}
})

Expand Down

0 comments on commit c128b8f

Please sign in to comment.