...

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

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

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package main
     5  
     6  // Test of interface calls.
     7  
     8  func use(interface{})
     9  
    10  type A byte // instantiated but not a reflect type
    11  
    12  func (A) f() {} // called directly
    13  func (A) F() {} // unreachable
    14  
    15  type B int // a reflect type
    16  
    17  func (*B) f() {} // reachable via interface invoke
    18  func (*B) F() {} // reachable: exported method of reflect type
    19  
    20  type B2 int // a reflect type, and *B2 also
    21  
    22  func (B2) f() {} // reachable via interface invoke
    23  func (B2) g() {} // reachable: exported method of reflect type
    24  
    25  type C string // not instantiated
    26  
    27  func (C) f() {} // unreachable
    28  func (C) F() {} // unreachable
    29  
    30  type D uint // instantiated only in dead code
    31  
    32  func (D) f() {} // unreachable
    33  func (D) F() {} // unreachable
    34  
    35  func main() {
    36  	A(0).f()
    37  
    38  	use(new(B))
    39  	use(B2(0))
    40  
    41  	var i interface {
    42  		f()
    43  	}
    44  	i.f() // calls (*B).f, (*B2).f and (B2.f)
    45  
    46  	live()
    47  }
    48  
    49  func live() {
    50  	var j interface {
    51  		f()
    52  		g()
    53  	}
    54  	j.f() // calls (B2).f and (*B2).f but not (*B).f (no g method).
    55  }
    56  
    57  func dead() {
    58  	use(D(0))
    59  }
    60  
    61  // WANT:
    62  //
    63  //  edge live --dynamic method call--> (*B2).f
    64  //  edge live --dynamic method call--> (B2).f
    65  //  edge main --dynamic method call--> (*B).f
    66  //  edge main --dynamic method call--> (*B2).f
    67  //  edge main --dynamic method call--> (B2).f
    68  //
    69  //  reachable (A).f
    70  // !reachable (A).F
    71  //  reachable (*B).f
    72  //  reachable (*B).F
    73  //  reachable (B2).f
    74  // !reachable (B2).g
    75  //  reachable (*B2).f
    76  // !reachable (*B2).g
    77  // !reachable (C).f
    78  // !reachable (C).F
    79  // !reachable (D).f
    80  // !reachable (D).F
    81  //  reachable main
    82  //  reachable live
    83  //  reachable use
    84  // !reachable dead
    85  //
    86  // !rtype A
    87  //  rtype *B
    88  //  rtype *B2
    89  //  rtype B
    90  //  rtype B2
    91  // !rtype C
    92  // !rtype D
    93  

View as plain text