Rémy Oudompheng | 2ece2f5 | 2012-02-18 22:15:42 +0100 | [diff] [blame] | 1 | // run |
Russ Cox | 46deaa2 | 2011-12-06 10:48:17 -0500 | [diff] [blame] | 2 | |
| 3 | // Copyright 2011 The Go Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
Rob Pike | 13514d4 | 2012-02-19 17:33:41 +1100 | [diff] [blame] | 7 | // Test run-time error detection for interface values containing types |
| 8 | // that cannot be compared for equality. |
Russ Cox | 46deaa2 | 2011-12-06 10:48:17 -0500 | [diff] [blame] | 9 | |
| 10 | package main |
| 11 | |
| 12 | func main() { |
| 13 | cmp(1) |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 14 | |
Russ Cox | 46deaa2 | 2011-12-06 10:48:17 -0500 | [diff] [blame] | 15 | var ( |
| 16 | m map[int]int |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 17 | s struct{ x []int } |
Russ Cox | 46deaa2 | 2011-12-06 10:48:17 -0500 | [diff] [blame] | 18 | f func() |
| 19 | ) |
| 20 | noCmp(m) |
| 21 | noCmp(s) |
| 22 | noCmp(f) |
| 23 | } |
| 24 | |
| 25 | func cmp(x interface{}) bool { |
| 26 | return x == x |
| 27 | } |
| 28 | |
| 29 | func noCmp(x interface{}) { |
| 30 | shouldPanic(func() { cmp(x) }) |
| 31 | } |
| 32 | |
| 33 | func shouldPanic(f func()) { |
| 34 | defer func() { |
| 35 | if recover() == nil { |
| 36 | panic("function should panic") |
| 37 | } |
| 38 | }() |
| 39 | f() |
| 40 | } |