mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00:00
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>
40 lines
711 B
Go
40 lines
711 B
Go
// 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) })
|
|
}
|