golang.org/x/tools@v0.21.1-0.20240520172518-788d39e776b1/go/callgraph/vta/testdata/src/callgraph_generics.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  func instantiated[X any](x *X) int {
    10  	print(x)
    11  	return 0
    12  }
    13  
    14  type I interface {
    15  	Bar()
    16  }
    17  
    18  func interfaceInstantiated[X I](x X) {
    19  	x.Bar()
    20  }
    21  
    22  type A struct{}
    23  
    24  func (a A) Bar() {}
    25  
    26  type B struct{}
    27  
    28  func (b B) Bar() {}
    29  
    30  func Foo(a A, b B) {
    31  	x := true
    32  	instantiated[bool](&x)
    33  	y := 1
    34  	instantiated[int](&y)
    35  
    36  	interfaceInstantiated[A](a)
    37  	interfaceInstantiated[B](b)
    38  }
    39  
    40  // Relevant SSA:
    41  //func Foo(a A, b B):
    42  //  t0 = new bool (x)
    43  //  *t0 = true:bool
    44  //  t1 = instantiated[bool](t2)
    45  //  t1 = new int (y)
    46  //  *t2 = 1:int
    47  //  t3 = instantiated[[int]](t4)
    48  //  t4 = interfaceInstantiated[testdata.A](a)
    49  //  t5 = interfaceInstantiated[testdata.B](b)
    50  //  return
    51  //
    52  //func interfaceInstantiated[[testdata.B]](x B):
    53  //  t0 = (B).Bar(b)
    54  //  return
    55  //
    56  //func interfaceInstantiated[X I](x X):
    57  //        (external)
    58  
    59  // WANT:
    60  // Foo: instantiated[bool](t0) -> instantiated[bool]; instantiated[int](t2) -> instantiated[int]; interfaceInstantiated[testdata.A](a) -> interfaceInstantiated[testdata.A]; interfaceInstantiated[testdata.B](b) -> interfaceInstantiated[testdata.B]
    61  // interfaceInstantiated[testdata.B]: (B).Bar(x) -> B.Bar
    62  // interfaceInstantiated[testdata.A]: (A).Bar(x) -> A.Bar