...

Source file src/golang.org/x/tools/go/callgraph/rta/testdata/reflectcall.go

Documentation: golang.org/x/tools/go/callgraph/rta/testdata

     1  //go:build ignore
     2  // +build ignore
     3  
     4  // Test of a reflective call to an address-taken function.
     5  //
     6  // Dynamically, this program executes both print statements.
     7  // RTA should report the hello methods as reachable,
     8  // even though there are no dynamic calls of type func(U)
     9  // and the type T is not live.
    10  
    11  package main
    12  
    13  import "reflect"
    14  
    15  type T int
    16  type U int // to ensure the hello methods' signatures are unique
    17  
    18  func (T) hello(U) { println("hello") }
    19  
    20  type T2 int
    21  
    22  func (T2) Hello(U, U) { println("T2.Hello") }
    23  
    24  func main() {
    25  	u := reflect.ValueOf(U(0))
    26  
    27  	// reflective call to bound method closure T.hello
    28  	reflect.ValueOf(T(0).hello).Call([]reflect.Value{u})
    29  
    30  	// reflective call to exported method "Hello" of rtype T2.
    31  	reflect.ValueOf(T2(0)).Method(0).Call([]reflect.Value{u, u})
    32  }
    33  
    34  // WANT:
    35  //
    36  //  edge (reflect.Value).Call --synthetic call--> (T).hello$bound
    37  //  edge (T).hello$bound --static method call--> (T).hello
    38  //  edge main --static function call--> reflect.ValueOf
    39  //  edge main --static method call--> (reflect.Value).Call
    40  //  edge (*T2).Hello --static method call--> (T2).Hello
    41  //
    42  //  reachable (T).hello
    43  //  reachable (T).hello$bound
    44  //  reachable (T2).Hello
    45  //
    46  // !rtype T
    47  //  rtype T2
    48  //  rtype U
    49  

View as plain text