github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/go/ssa/interp/testdata/typeassert.go (about)

     1  // Tests of type asserts.
     2  // Requires type parameters.
     3  package typeassert
     4  
     5  type fooer interface{ foo() string }
     6  
     7  type X int
     8  
     9  func (_ X) foo() string { return "x" }
    10  
    11  func f[T fooer](x T) func() string {
    12  	return x.foo
    13  }
    14  
    15  func main() {
    16  	if f[X](0)() != "x" {
    17  		panic("f[X]() != 'x'")
    18  	}
    19  
    20  	p := false
    21  	func() {
    22  		defer func() {
    23  			if recover() != nil {
    24  				p = true
    25  			}
    26  		}()
    27  		f[fooer](nil) // panics on x.foo when T is an interface and nil.
    28  	}()
    29  	if !p {
    30  		panic("f[fooer] did not panic")
    31  	}
    32  }