github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/callgraph/vta/testdata/src/callgraph_collections.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // go:build ignore
     6  
     7  package testdata
     8  
     9  type I interface {
    10  	Foo()
    11  }
    12  
    13  type A struct{}
    14  
    15  func (a A) Foo() {}
    16  
    17  type B struct{}
    18  
    19  func (b B) Foo() {}
    20  
    21  func Do(a A, b B) map[I]I {
    22  	m := make(map[I]I)
    23  	m[a] = B{}
    24  	m[b] = b
    25  	return m
    26  }
    27  
    28  func Baz(a A, b B) {
    29  	var x []I
    30  	for k, v := range Do(a, b) {
    31  		k.Foo()
    32  		v.Foo()
    33  
    34  		x = append(x, k)
    35  	}
    36  
    37  	x[len(x)-1].Foo()
    38  }
    39  
    40  // Relevant SSA:
    41  // func Baz(a A, b B):
    42  //   ...
    43  //   t4 = Do(t2, t3)
    44  //   t5 = range t4
    45  //   jump 1
    46  //  1:
    47  //   t6 = phi [0: nil:[]I, 2: t16] #x
    48  //   t7 = next t5
    49  //   t8 = extract t7 #0
    50  //   if t8 goto 2 else 3
    51  //  2:
    52  //   t9 = extract t7 #1
    53  //   t10 = extract t7 #2
    54  //   t11 = invoke t9.Foo()
    55  //   t12 = invoke t10.Foo()
    56  //   ...
    57  //   jump 1
    58  //  3:
    59  //   t17 = len(t6)
    60  //   t18 = t17 - 1:int
    61  //   t19 = &t6[t18]
    62  //   t20 = *t19
    63  //   t21 = invoke t20.Foo()
    64  //   return
    65  
    66  // WANT:
    67  // Baz: Do(t2, t3) -> Do; invoke t10.Foo() -> B.Foo; invoke t20.Foo() -> A.Foo, B.Foo; invoke t9.Foo() -> A.Foo, B.Foo