1
0
mirror of https://github.com/golang/go synced 2024-07-05 09:50:19 +00:00

go/types, types2: better errors for == when type sets are empty

For #51525.

Change-Id: I3762bc4a48a1aaab3b006b1ad1400f866892243c
Reviewed-on: https://go-review.googlesource.com/c/go/+/413934
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2022-06-23 20:54:52 -07:00
parent d38f1d13fa
commit 3e58ef6cc7
8 changed files with 84 additions and 32 deletions

View File

@ -147,7 +147,17 @@ func comparable(T Type, dynamic bool, seen map[Type]bool, reportf func(string, .
}
return true
case *Interface:
return dynamic && !isTypeParam(T) || t.typeSet().IsComparable(seen)
if dynamic && !isTypeParam(T) || t.typeSet().IsComparable(seen) {
return true
}
if reportf != nil {
if t.typeSet().IsEmpty() {
reportf("empty type set")
} else {
reportf("incomparable types in type set")
}
}
// fallthrough
}
return false
}

View File

@ -23,18 +23,18 @@ func _[P comparable](x P, y any) {
}
func _[P any](x, y P) {
_ = x /* ERROR type parameter P is not comparable with == */ == x
_ = x /* ERROR type parameter P is not comparable with == */ == y
_ = y /* ERROR type parameter P is not comparable with == */ == x
_ = y /* ERROR type parameter P is not comparable with == */ == y
_ = x /* ERROR incomparable types in type set */ == x
_ = x /* ERROR incomparable types in type set */ == y
_ = y /* ERROR incomparable types in type set */ == x
_ = y /* ERROR incomparable types in type set */ == y
_ = x /* ERROR type parameter P is not comparable with < */ < y
}
func _[P any](x P, y any) {
_ = x /* ERROR type parameter P is not comparable with == */ == x
_ = x /* ERROR type parameter P is not comparable with == */ == y
_ = y == x // ERROR type parameter P is not comparable with ==
_ = x /* ERROR incomparable types in type set */ == x
_ = x /* ERROR incomparable types in type set */ == y
_ = y == x // ERROR incomparable types in type set
_ = y == y
_ = x /* ERROR type parameter P is not comparable with < */ < y

View File

@ -0,0 +1,16 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p
func _[T interface {
int
string
}](x T) {
_ = x /* ERROR empty type set */ == x
}
func _[T interface{ int | []byte }](x T) {
_ = x /* ERROR incomparable types in type set */ == x
}

View File

@ -40,7 +40,7 @@ func _() {
_ = m /* ERROR map can only be compared to nil */ == m
_ = c == c
_ = b /* ERROR mismatched types */ == nil
_ = b /* ERROR mismatched types */ == nil
_ = a /* ERROR mismatched types */ == nil
_ = l == nil
_ = s /* ERROR mismatched types */ == nil
@ -73,7 +73,7 @@ func _[
J comparable,
M map[string]int,
C chan int,
] (
](
b B,
a A,
l L,
@ -86,14 +86,14 @@ func _[
c C,
) {
_ = b == b
_ = a /* ERROR type parameter A is not comparable with == */ == a
_ = l /* ERROR type parameter L is not comparable with == */ == l
_ = s /* ERROR type parameter S is not comparable with == */ == s
_ = a /* ERROR incomparable types in type set */ == a
_ = l /* ERROR incomparable types in type set */ == l
_ = s /* ERROR incomparable types in type set */ == s
_ = p == p
_ = f /* ERROR type parameter F is not comparable with == */ == f
_ = i /* ERROR type parameter I is not comparable with == */ == i
_ = f /* ERROR incomparable types in type set */ == f
_ = i /* ERROR incomparable types in type set */ == i
_ = j == j
_ = m /* ERROR type parameter M is not comparable with == */ == m
_ = m /* ERROR incomparable types in type set */ == m
_ = c == c
_ = b /* ERROR mismatched types */ == nil

View File

@ -149,7 +149,17 @@ func comparable(T Type, dynamic bool, seen map[Type]bool, reportf func(string, .
}
return true
case *Interface:
return dynamic && !isTypeParam(T) || t.typeSet().IsComparable(seen)
if dynamic && !isTypeParam(T) || t.typeSet().IsComparable(seen) {
return true
}
if reportf != nil {
if t.typeSet().IsEmpty() {
reportf("empty type set")
} else {
reportf("incomparable types in type set")
}
}
// fallthrough
}
return false
}

View File

@ -23,18 +23,18 @@ func _[P comparable](x P, y any) {
}
func _[P any](x, y P) {
_ = x /* ERROR type parameter P is not comparable with == */ == x
_ = x /* ERROR type parameter P is not comparable with == */ == y
_ = y /* ERROR type parameter P is not comparable with == */ == x
_ = y /* ERROR type parameter P is not comparable with == */ == y
_ = x /* ERROR incomparable types in type set */ == x
_ = x /* ERROR incomparable types in type set */ == y
_ = y /* ERROR incomparable types in type set */ == x
_ = y /* ERROR incomparable types in type set */ == y
_ = x /* ERROR type parameter P is not comparable with < */ < y
}
func _[P any](x P, y any) {
_ = x /* ERROR type parameter P is not comparable with == */ == x
_ = x /* ERROR type parameter P is not comparable with == */ == y
_ = y == x // ERROR type parameter P is not comparable with ==
_ = x /* ERROR incomparable types in type set */ == x
_ = x /* ERROR incomparable types in type set */ == y
_ = y == x // ERROR incomparable types in type set
_ = y == y
_ = x /* ERROR type parameter P is not comparable with < */ < y

View File

@ -0,0 +1,16 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p
func _[T interface {
int
string
}](x T) {
_ = x /* ERROR empty type set */ == x
}
func _[T interface{ int | []byte }](x T) {
_ = x /* ERROR incomparable types in type set */ == x
}

View File

@ -40,7 +40,7 @@ func _() {
_ = m /* ERROR map can only be compared to nil */ == m
_ = c == c
_ = b /* ERROR mismatched types */ == nil
_ = b /* ERROR mismatched types */ == nil
_ = a /* ERROR mismatched types */ == nil
_ = l == nil
_ = s /* ERROR mismatched types */ == nil
@ -73,7 +73,7 @@ func _[
J comparable,
M map[string]int,
C chan int,
] (
](
b B,
a A,
l L,
@ -86,14 +86,14 @@ func _[
c C,
) {
_ = b == b
_ = a /* ERROR type parameter A is not comparable with == */ == a
_ = l /* ERROR type parameter L is not comparable with == */ == l
_ = s /* ERROR type parameter S is not comparable with == */ == s
_ = a /* ERROR incomparable types in type set */ == a
_ = l /* ERROR incomparable types in type set */ == l
_ = s /* ERROR incomparable types in type set */ == s
_ = p == p
_ = f /* ERROR type parameter F is not comparable with == */ == f
_ = i /* ERROR type parameter I is not comparable with == */ == i
_ = f /* ERROR incomparable types in type set */ == f
_ = i /* ERROR incomparable types in type set */ == i
_ = j == j
_ = m /* ERROR type parameter M is not comparable with == */ == m
_ = m /* ERROR incomparable types in type set */ == m
_ = c == c
_ = b /* ERROR mismatched types */ == nil