github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/callgraph/vta/testdata/src/closures.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  func Do(i I) { i.Foo() }
    14  
    15  func Baz(b bool, h func(I)) {
    16  	var i I
    17  	a := func(g func(I)) {
    18  		g(i)
    19  	}
    20  
    21  	if b {
    22  		h = Do
    23  	}
    24  
    25  	a(h)
    26  }
    27  
    28  // Relevant SSA:
    29  //  func Baz(b bool, h func(I)):
    30  //    t0 = new I (i)
    31  //    t1 = make closure Baz$1 [t0]
    32  //    if b goto 1 else 2
    33  //   1:
    34  //         jump 2
    35  //   2:
    36  //    t2 = phi [0: h, 1: Do] #h
    37  //    t3 = t1(t2)
    38  //    return
    39  //
    40  // func Baz$1(g func(I)):
    41  //    t0 = *i
    42  //    t1 = g(t0)
    43  //    return
    44  
    45  // In the edge set Local(i) -> Local(t0), Local(t0) below,
    46  // two occurrences of t0 come from t0 in Baz and Baz$1.
    47  
    48  // WANT:
    49  // Function(Do) -> Local(t2)
    50  // Function(Baz$1) -> Local(t1)
    51  // Local(h) -> Local(t2)
    52  // Local(t0) -> Local(i)
    53  // Local(i) -> Local(t0), Local(t0)