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