1
0
mirror of https://github.com/golang/go synced 2024-09-29 09:24:28 -06:00
go/test/typeparam/issue50646.go
Robert Griesemer 38729cff96 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>
2022-01-25 22:04:10 +00:00

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) })
}