golang.org/x/tools@v0.21.1-0.20240520172518-788d39e776b1/go/callgraph/vta/testdata/src/select.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() string
    11  }
    12  
    13  type J interface {
    14  	I
    15  }
    16  
    17  type B struct {
    18  	p string
    19  }
    20  
    21  func (b B) Foo() string { return b.p }
    22  
    23  func Baz(b1, b2 B, c1 chan I, c2 chan J) {
    24  	for {
    25  		select {
    26  		case c1 <- b1:
    27  			print("b1")
    28  		case c2 <- b2:
    29  			print("b2")
    30  		case <-c1:
    31  			print("c1")
    32  		case k := <-c2:
    33  			print(k.Foo())
    34  			return
    35  		}
    36  	}
    37  }
    38  
    39  // Relevant SSA:
    40  // func Baz(b1 B, b2 B, c1 chan I, c2 chan J):
    41  //   ...
    42  //   t0 = make I <- B (b1)
    43  //   t1 = make J <- B (b2)
    44  //   t2 = select blocking [c1<-t0, c2<-t1, <-c1, <-c2] (index int, ok bool, I, J)
    45  //   t3 = extract t2 #0
    46  //   t4 = t73== 0:int
    47  //   if t4 goto 2 else 3
    48  //         ...
    49  //  8:
    50  //   t12 = extract t2 #3
    51  //   t13 = invoke t12.Foo()
    52  //   t14 = print(t15)
    53  
    54  // WANT:
    55  // Local(t0) -> Channel(chan testdata.I)
    56  // Local(t1) -> Channel(chan testdata.J)
    57  // Channel(chan testdata.I) -> Local(t2[2])
    58  // Channel(chan testdata.J) -> Local(t2[3])