golang.org/x/tools@v0.21.1-0.20240520172518-788d39e776b1/go/callgraph/vta/testdata/src/callgraph_comma_maps.go (about)

     1  // Copyright 2023 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  	Name() string
    11  	Foo()
    12  }
    13  
    14  var is = make(map[string]I)
    15  
    16  func init() {
    17  	register(A{})
    18  	register(B{})
    19  }
    20  
    21  func register(i I) {
    22  	is[i.Name()] = i
    23  }
    24  
    25  type A struct{}
    26  
    27  func (a A) Foo()         {}
    28  func (a A) Name() string { return "a" }
    29  
    30  type B struct{}
    31  
    32  func (b B) Foo()         {}
    33  func (b B) Name() string { return "b" }
    34  
    35  func Do(n string) {
    36  	i, ok := is[n]
    37  	if !ok {
    38  		return
    39  	}
    40  	i.Foo()
    41  }
    42  
    43  func Go(n string) {
    44  	if i, ok := is[n]; !ok {
    45  		return
    46  	} else {
    47  		i.Foo()
    48  	}
    49  }
    50  
    51  func To(n string) {
    52  	var i I
    53  	var ok bool
    54  
    55  	if i, ok = is[n]; !ok {
    56  		return
    57  	}
    58  	i.Foo()
    59  }
    60  
    61  func Ro(n string) {
    62  	i := is[n]
    63  	i.Foo()
    64  }
    65  
    66  // Relevant SSA:
    67  // func Do(n string):
    68  //        t0 = *is
    69  //        t1 = t0[n],ok
    70  //        t2 = extract t1 #0
    71  //        t3 = extract t1 #1
    72  //        if t3 goto 2 else 1
    73  // 1:
    74  //        return
    75  // 2:
    76  //        t4 = invoke t2.Foo()
    77  //        return
    78  
    79  // WANT:
    80  // register: invoke i.Name() -> A.Name, B.Name
    81  // Do: invoke t2.Foo() -> A.Foo, B.Foo
    82  // Go: invoke t2.Foo() -> A.Foo, B.Foo
    83  // To: invoke t2.Foo() -> A.Foo, B.Foo
    84  // Ro: invoke t1.Foo() -> A.Foo, B.Foo