github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/pointer/testdata/panic.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package main
     5  
     6  // Test of value flow from panic() to recover().
     7  // We model them as stores/loads of a global location.
     8  // We ignore concrete panic types originating from the runtime.
     9  
    10  var someval int
    11  
    12  type myPanic struct{}
    13  
    14  func f(int) {}
    15  
    16  func g() string { return "" }
    17  
    18  func deadcode() {
    19  	panic(123) // not reached
    20  }
    21  
    22  func main() {
    23  	switch someval {
    24  	case 0:
    25  		panic("oops")
    26  	case 1:
    27  		panic(myPanic{})
    28  	case 2:
    29  		panic(f)
    30  	case 3:
    31  		panic(g)
    32  	}
    33  	ex := recover()
    34  	print(ex)                 // @types myPanic | string | func(int) | func() string
    35  	print(ex.(func(int)))     // @pointsto command-line-arguments.f
    36  	print(ex.(func() string)) // @pointsto command-line-arguments.g
    37  }