mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +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:
parent
d38f1d13fa
commit
3e58ef6cc7
8 changed files with 84 additions and 32 deletions
|
|
@ -147,7 +147,17 @@ func comparable(T Type, dynamic bool, seen map[Type]bool, reportf func(string, .
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
case *Interface:
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,18 +23,18 @@ func _[P comparable](x P, y any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func _[P any](x, y P) {
|
func _[P any](x, y P) {
|
||||||
_ = x /* ERROR type parameter P is not comparable with == */ == x
|
_ = x /* ERROR incomparable types in type set */ == x
|
||||||
_ = x /* ERROR type parameter P is not comparable with == */ == y
|
_ = x /* ERROR incomparable types in type set */ == y
|
||||||
_ = y /* ERROR type parameter P is not comparable with == */ == x
|
_ = y /* ERROR incomparable types in type set */ == x
|
||||||
_ = y /* ERROR type parameter P is not comparable with == */ == y
|
_ = y /* ERROR incomparable types in type set */ == y
|
||||||
|
|
||||||
_ = x /* ERROR type parameter P is not comparable with < */ < y
|
_ = x /* ERROR type parameter P is not comparable with < */ < y
|
||||||
}
|
}
|
||||||
|
|
||||||
func _[P any](x P, y any) {
|
func _[P any](x P, y any) {
|
||||||
_ = x /* ERROR type parameter P is not comparable with == */ == x
|
_ = x /* ERROR incomparable types in type set */ == x
|
||||||
_ = x /* ERROR type parameter P is not comparable with == */ == y
|
_ = x /* ERROR incomparable types in type set */ == y
|
||||||
_ = y == x // ERROR type parameter P is not comparable with ==
|
_ = y == x // ERROR incomparable types in type set
|
||||||
_ = y == y
|
_ = y == y
|
||||||
|
|
||||||
_ = x /* ERROR type parameter P is not comparable with < */ < y
|
_ = x /* ERROR type parameter P is not comparable with < */ < y
|
||||||
|
|
|
||||||
16
src/cmd/compile/internal/types2/testdata/fixedbugs/issue51525.go
vendored
Normal file
16
src/cmd/compile/internal/types2/testdata/fixedbugs/issue51525.go
vendored
Normal 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
|
||||||
|
}
|
||||||
|
|
@ -86,14 +86,14 @@ func _[
|
||||||
c C,
|
c C,
|
||||||
) {
|
) {
|
||||||
_ = b == b
|
_ = b == b
|
||||||
_ = a /* ERROR type parameter A is not comparable with == */ == a
|
_ = a /* ERROR incomparable types in type set */ == a
|
||||||
_ = l /* ERROR type parameter L is not comparable with == */ == l
|
_ = l /* ERROR incomparable types in type set */ == l
|
||||||
_ = s /* ERROR type parameter S is not comparable with == */ == s
|
_ = s /* ERROR incomparable types in type set */ == s
|
||||||
_ = p == p
|
_ = p == p
|
||||||
_ = f /* ERROR type parameter F is not comparable with == */ == f
|
_ = f /* ERROR incomparable types in type set */ == f
|
||||||
_ = i /* ERROR type parameter I is not comparable with == */ == i
|
_ = i /* ERROR incomparable types in type set */ == i
|
||||||
_ = j == j
|
_ = j == j
|
||||||
_ = m /* ERROR type parameter M is not comparable with == */ == m
|
_ = m /* ERROR incomparable types in type set */ == m
|
||||||
_ = c == c
|
_ = c == c
|
||||||
|
|
||||||
_ = b /* ERROR mismatched types */ == nil
|
_ = b /* ERROR mismatched types */ == nil
|
||||||
|
|
|
||||||
|
|
@ -149,7 +149,17 @@ func comparable(T Type, dynamic bool, seen map[Type]bool, reportf func(string, .
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
case *Interface:
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
14
src/go/types/testdata/fixedbugs/issue48712.go
vendored
14
src/go/types/testdata/fixedbugs/issue48712.go
vendored
|
|
@ -23,18 +23,18 @@ func _[P comparable](x P, y any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func _[P any](x, y P) {
|
func _[P any](x, y P) {
|
||||||
_ = x /* ERROR type parameter P is not comparable with == */ == x
|
_ = x /* ERROR incomparable types in type set */ == x
|
||||||
_ = x /* ERROR type parameter P is not comparable with == */ == y
|
_ = x /* ERROR incomparable types in type set */ == y
|
||||||
_ = y /* ERROR type parameter P is not comparable with == */ == x
|
_ = y /* ERROR incomparable types in type set */ == x
|
||||||
_ = y /* ERROR type parameter P is not comparable with == */ == y
|
_ = y /* ERROR incomparable types in type set */ == y
|
||||||
|
|
||||||
_ = x /* ERROR type parameter P is not comparable with < */ < y
|
_ = x /* ERROR type parameter P is not comparable with < */ < y
|
||||||
}
|
}
|
||||||
|
|
||||||
func _[P any](x P, y any) {
|
func _[P any](x P, y any) {
|
||||||
_ = x /* ERROR type parameter P is not comparable with == */ == x
|
_ = x /* ERROR incomparable types in type set */ == x
|
||||||
_ = x /* ERROR type parameter P is not comparable with == */ == y
|
_ = x /* ERROR incomparable types in type set */ == y
|
||||||
_ = y == x // ERROR type parameter P is not comparable with ==
|
_ = y == x // ERROR incomparable types in type set
|
||||||
_ = y == y
|
_ = y == y
|
||||||
|
|
||||||
_ = x /* ERROR type parameter P is not comparable with < */ < y
|
_ = x /* ERROR type parameter P is not comparable with < */ < y
|
||||||
|
|
|
||||||
16
src/go/types/testdata/fixedbugs/issue51525.go
vendored
Normal file
16
src/go/types/testdata/fixedbugs/issue51525.go
vendored
Normal 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
|
||||||
|
}
|
||||||
12
src/go/types/testdata/spec/comparisons.go
vendored
12
src/go/types/testdata/spec/comparisons.go
vendored
|
|
@ -86,14 +86,14 @@ func _[
|
||||||
c C,
|
c C,
|
||||||
) {
|
) {
|
||||||
_ = b == b
|
_ = b == b
|
||||||
_ = a /* ERROR type parameter A is not comparable with == */ == a
|
_ = a /* ERROR incomparable types in type set */ == a
|
||||||
_ = l /* ERROR type parameter L is not comparable with == */ == l
|
_ = l /* ERROR incomparable types in type set */ == l
|
||||||
_ = s /* ERROR type parameter S is not comparable with == */ == s
|
_ = s /* ERROR incomparable types in type set */ == s
|
||||||
_ = p == p
|
_ = p == p
|
||||||
_ = f /* ERROR type parameter F is not comparable with == */ == f
|
_ = f /* ERROR incomparable types in type set */ == f
|
||||||
_ = i /* ERROR type parameter I is not comparable with == */ == i
|
_ = i /* ERROR incomparable types in type set */ == i
|
||||||
_ = j == j
|
_ = j == j
|
||||||
_ = m /* ERROR type parameter M is not comparable with == */ == m
|
_ = m /* ERROR incomparable types in type set */ == m
|
||||||
_ = c == c
|
_ = c == c
|
||||||
|
|
||||||
_ = b /* ERROR mismatched types */ == nil
|
_ = b /* ERROR mismatched types */ == nil
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue