diff options
| author | Robert Griesemer <gri@golang.org> | 2022-01-24 17:49:11 -0800 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2022-01-25 22:04:10 +0000 |
| commit | 38729cff96ad38b2d5b530c1009ff0403ebff903 (patch) | |
| tree | ee27e280812028c75fcf8fe448e06698623272a5 /test/typeparam | |
| parent | b66bc0a9d5eba193f7d7f4977ca64a77527f4b3b (diff) | |
| download | go-38729cff96ad38b2d5b530c1009ff0403ebff903.tar.xz | |
go/types, types2: all interfaces implement comparable (add tests)
For #50646.
Change-Id: I7420545556e0df2659836364a62ce2c32ad7a8b1
Reviewed-on: https://go-review.googlesource.com/c/go/+/380654
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'test/typeparam')
| -rw-r--r-- | test/typeparam/issue50646.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/test/typeparam/issue50646.go b/test/typeparam/issue50646.go new file mode 100644 index 0000000000..44bbe2ae6f --- /dev/null +++ b/test/typeparam/issue50646.go @@ -0,0 +1,39 @@ +// 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) }) +} |
