aboutsummaryrefslogtreecommitdiff
path: root/test/typeparam
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2022-01-26 22:48:44 -0800
committerRobert Griesemer <gri@golang.org>2022-01-31 20:35:07 +0000
commit360e1b8197b78685cf08ab5914aa629fb739b2c3 (patch)
tree685df3a0f7eadbfb6891550e0c9071e7fc682bba /test/typeparam
parent41f485b9a7d8fd647c415be1d11b612063dff21c (diff)
downloadgo-360e1b8197b78685cf08ab5914aa629fb739b2c3.tar.xz
go/types, types2: fix implements and identical predicates
- Use the correct predicate in Checker.implements: for interfaces we cannot use the API Comparable because it always returns true for all non-type parameter interface types: Comparable simply answers if == and != is permitted, and it's always been permitted for interfaces. Instead we must use Interface.IsComparable which looks at the type set of an interface. - When comparing interfaces for identity, we must also consider the whether the type sets have the comparable bit set. With this change, `any` doesn't implement `comparable` anymore. This only matters for generic functions and types, and the API functions. It does mean that for now (until we allow type-constrained interfaces for general non-constraint use, at some point in the future) a type parameter that needs to be comparable cannot be instantiated with an interface anymore. For #50646. Change-Id: I7e7f711bdcf94461f330c90509211ec0c2cf3633 Reviewed-on: https://go-review.googlesource.com/c/go/+/381254 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'test/typeparam')
-rw-r--r--test/typeparam/issue48276a.go2
-rw-r--r--test/typeparam/issue48276a.out2
-rw-r--r--test/typeparam/issue50646.go39
3 files changed, 2 insertions, 41 deletions
diff --git a/test/typeparam/issue48276a.go b/test/typeparam/issue48276a.go
index 060ac3eb7f..25e939f536 100644
--- a/test/typeparam/issue48276a.go
+++ b/test/typeparam/issue48276a.go
@@ -9,7 +9,7 @@ package main
import "fmt"
func main() {
- IsZero[interface{}]("")
+ IsZero[int](0)
}
func IsZero[T comparable](val T) bool {
diff --git a/test/typeparam/issue48276a.out b/test/typeparam/issue48276a.out
index 7e8a8a9a2e..8f38db999d 100644
--- a/test/typeparam/issue48276a.out
+++ b/test/typeparam/issue48276a.out
@@ -1 +1 @@
-<nil>:
+0:0
diff --git a/test/typeparam/issue50646.go b/test/typeparam/issue50646.go
deleted file mode 100644
index 44bbe2ae6f..0000000000
--- a/test/typeparam/issue50646.go
+++ /dev/null
@@ -1,39 +0,0 @@
-// run -gcflags=-G=3
-
-// 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 main
-
-func eql[P comparable](x, y P) {
- if x != y {
- panic("not equal")
- }
-}
-
-func expectPanic(f func()) {
- defer func() {
- if recover() == nil {
- panic("function succeeded unexpectedly")
- }
- }()
- f()
-}
-
-func main() {
- eql[int](1, 1)
- eql(1, 1)
-
- // all interfaces implement comparable
- var x, y any = 2, 2
- eql[any](x, y)
- eql(x, y)
-
- // but we may get runtime panics
- x, y = 1, 2 // x != y
- expectPanic(func() { eql(x, y) })
-
- x, y = main, main // functions are not comparable
- expectPanic(func() { eql(x, y) })
-}