modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/interface/noeq.go (about)

     1  // run
     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  
     7  // Test run-time error detection for interface values containing types
     8  // that cannot be compared for equality.
     9  
    10  package main
    11  
    12  func main() {
    13  	cmp(1)
    14  
    15  	var (
    16  		m map[int]int
    17  		s struct{ x []int }
    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  }