]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: better errors for == when type sets are empty
authorRobert Griesemer <gri@golang.org>
Fri, 24 Jun 2022 03:54:52 +0000 (20:54 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 24 Jun 2022 17:45:41 +0000 (17:45 +0000)
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>
src/cmd/compile/internal/types2/predicates.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue48712.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue51525.go [new file with mode: 0644]
src/cmd/compile/internal/types2/testdata/spec/comparisons.go
src/go/types/predicates.go
src/go/types/testdata/fixedbugs/issue48712.go
src/go/types/testdata/fixedbugs/issue51525.go [new file with mode: 0644]
src/go/types/testdata/spec/comparisons.go

index f7b5b16204e5c823a94f818d0018595f1437136b..c4d11dcac4b12c088bc9d56b8ed17e9bf206b303 100644 (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
 }
index ab397560a8fc00bda09950953dabb9b9456bfb64..63ce7bc5103442fe42421c0726eaa5b273415b0e 100644 (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
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51525.go b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51525.go
new file mode 100644 (file)
index 0000000..af1d1e6
--- /dev/null
@@ -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
+}
index 62c95d47d78609d4361a31013b2f97b4d72026f6..2a7598a5815d0f403ee07d17151fa0ef481f8a47 100644 (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
index 25db4acf4a7e1197f579f1ae1670f2921add92ba..aaf4dd52fc5228686cccdf8944d2cf5ba89a9a2b 100644 (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
 }
index ab397560a8fc00bda09950953dabb9b9456bfb64..63ce7bc5103442fe42421c0726eaa5b273415b0e 100644 (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
diff --git a/src/go/types/testdata/fixedbugs/issue51525.go b/src/go/types/testdata/fixedbugs/issue51525.go
new file mode 100644 (file)
index 0000000..af1d1e6
--- /dev/null
@@ -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
+}
index 62c95d47d78609d4361a31013b2f97b4d72026f6..2a7598a5815d0f403ee07d17151fa0ef481f8a47 100644 (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