github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/pointer/testdata/conv.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package main
     5  
     6  import "unsafe"
     7  
     8  var a int
     9  
    10  func conv1() {
    11  	// Conversions of channel direction.
    12  	ch := make(chan int)    // @line c1make
    13  	print((<-chan int)(ch)) // @pointsto makechan@c1make:12
    14  	print((chan<- int)(ch)) // @pointsto makechan@c1make:12
    15  }
    16  
    17  func conv2() {
    18  	// string -> []byte/[]rune conversion
    19  	s := "foo"
    20  	ba := []byte(s) // @line c2ba
    21  	ra := []rune(s) // @line c2ra
    22  	print(ba)       // @pointsto convert@c2ba:14
    23  	print(ra)       // @pointsto convert@c2ra:14
    24  }
    25  
    26  func conv3() {
    27  	// Conversion of same underlying types.
    28  	type PI *int
    29  	pi := PI(&a)
    30  	print(pi) // @pointsto command-line-arguments.a
    31  
    32  	pint := (*int)(pi)
    33  	print(pint) // @pointsto command-line-arguments.a
    34  
    35  	// Conversions between pointers to identical base types.
    36  	var y *PI = &pi
    37  	var x **int = (**int)(y)
    38  	print(*x) // @pointsto command-line-arguments.a
    39  	print(*y) // @pointsto command-line-arguments.a
    40  	y = (*PI)(x)
    41  	print(*y) // @pointsto command-line-arguments.a
    42  }
    43  
    44  func conv4() {
    45  	// Handling of unsafe.Pointer conversion is unsound:
    46  	// we lose the alias to command-line-arguments.a and get something like new(int) instead.
    47  	p := (*int)(unsafe.Pointer(&a)) // @line c2p
    48  	print(p)                        // @pointsto convert@c2p:13
    49  }
    50  
    51  // Regression test for b/8231.
    52  func conv5() {
    53  	type P unsafe.Pointer
    54  	var i *struct{}
    55  	_ = P(i)
    56  }
    57  
    58  func main() {
    59  	conv1()
    60  	conv2()
    61  	conv3()
    62  	conv4()
    63  	conv5()
    64  }