github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/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 = local A (a)
    43  //  *t0 = a
    44  //  t1 = local B (b)
    45  //  *t1 = b
    46  //  t2 = new bool (x)
    47  //  *t2 = true:bool
    48  //  t3 = instantiated[bool](t2)
    49  //  t4 = new int (y)
    50  //  *t4 = 1:int
    51  //  t5 = instantiated[int](t4)
    52  //  t6 = *t0
    53  //  t7 = interfaceInstantiated[testdata.A](t6)
    54  //  t8 = *t1
    55  //  t9 = interfaceInstantiated[testdata.B](t8)
    56  //  return
    57  //
    58  //func interfaceInstantiated[testdata.B](x B):
    59  //  t0 = local B (x)
    60  //  *t0 = x
    61  //  t1 = *t0
    62  //  t2 = (B).Bar(t1)
    63  //  return
    64  //
    65  //func interfaceInstantiated[X I](x X):
    66  //        (external)
    67  
    68  // WANT:
    69  // Foo: instantiated[bool](t2) -> instantiated[bool]; instantiated[int](t4) -> instantiated[int]; interfaceInstantiated[testdata.A](t6) -> interfaceInstantiated[testdata.A]; interfaceInstantiated[testdata.B](t8) -> interfaceInstantiated[testdata.B]
    70  // interfaceInstantiated[testdata.B]: (B).Bar(t1) -> B.Bar
    71  // interfaceInstantiated[testdata.A]: (A).Bar(t1) -> A.Bar