Debian Codegraph @

go/callgraph/rta/testdata/reflectcall.txtar

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