github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/pointer/testdata/arrays_go117.go (about) 1 //go:build ignore 2 // +build ignore 3 4 package main 5 6 // Forked from arrays.go. Requires go1.17 to parse slice to array casts. 7 // TODO(taking): Merge back into arrays.go once we can assume go1.17. 8 9 var unknown bool // defeat dead-code elimination 10 11 var a, b int 12 13 func array1() { 14 sliceA := make([]*int, 10) // @line a1make 15 sliceA[0] = &a 16 17 var sliceB []*int 18 sliceB = append(sliceB, &b) // @line a1append 19 20 print(sliceA) // @pointsto makeslice@a1make:16 21 print(sliceA[0]) // @pointsto command-line-arguments.a 22 23 print(sliceB) // @pointsto append@a1append:17 24 print(sliceB[100]) // @pointsto command-line-arguments.b 25 } 26 27 func array2() { 28 sliceA := make([]*int, 10) // @line a2make 29 sliceA[0] = &a 30 31 sliceB := sliceA[:] 32 33 print(sliceA) // @pointsto makeslice@a2make:16 34 print(sliceA[0]) // @pointsto command-line-arguments.a 35 36 print(sliceB) // @pointsto makeslice@a2make:16 37 print(sliceB[0]) // @pointsto command-line-arguments.a 38 } 39 40 func array3() { 41 a := []interface{}{"", 1} 42 b := []interface{}{true, func() {}} 43 print(a[0]) // @types string | int 44 print(b[0]) // @types bool | func() 45 } 46 47 // Test of append, copy, slice. 48 func array4() { 49 var s2 struct { // @line a4L0 50 a [3]int 51 b struct{ c, d int } 52 } 53 var sl1 = make([]*int, 10) // @line a4make 54 var someint int // @line a4L1 55 sl1[1] = &someint 56 sl2 := append(sl1, &s2.a[1]) // @line a4append1 57 print(sl1) // @pointsto makeslice@a4make:16 58 print(sl2) // @pointsto append@a4append1:15 | makeslice@a4make:16 59 print(sl1[0]) // @pointsto someint@a4L1:6 | s2.a[*]@a4L0:6 60 print(sl2[0]) // @pointsto someint@a4L1:6 | s2.a[*]@a4L0:6 61 62 // In z=append(x,y) we should observe flow from y[*] to x[*]. 63 var sl3 = make([]*int, 10) // @line a4L2 64 _ = append(sl3, &s2.a[1]) 65 print(sl3) // @pointsto makeslice@a4L2:16 66 print(sl3[0]) // @pointsto s2.a[*]@a4L0:6 67 68 var sl4 = []*int{&a} // @line a4L3 69 sl4a := append(sl4) // @line a4L4 70 print(sl4a) // @pointsto slicelit@a4L3:18 | append@a4L4:16 71 print(&sl4a[0]) // @pointsto slicelit[*]@a4L3:18 | append[*]@a4L4:16 72 print(sl4a[0]) // @pointsto command-line-arguments.a 73 74 var sl5 = []*int{&b} // @line a4L5 75 copy(sl5, sl4) 76 print(sl5) // @pointsto slicelit@a4L5:18 77 print(&sl5[0]) // @pointsto slicelit[*]@a4L5:18 78 print(sl5[0]) // @pointsto command-line-arguments.b | command-line-arguments.a 79 80 var sl6 = sl5[:0] 81 print(sl6) // @pointsto slicelit@a4L5:18 82 print(&sl6[0]) // @pointsto slicelit[*]@a4L5:18 83 print(sl6[0]) // @pointsto command-line-arguments.b | command-line-arguments.a 84 } 85 86 func array5() { 87 var arr [2]*int 88 arr[0] = &a 89 arr[1] = &b 90 91 var n int 92 print(arr[n]) // @pointsto command-line-arguments.a | command-line-arguments.b 93 } 94 95 func array6() { 96 var n int 97 98 sl0 := []*int{&a} 99 ap0 := (*[1]*int)(sl0) 100 ar0 := *ap0 101 102 print(ap0[n]) // @pointsto command-line-arguments.a 103 print(sl0[n]) // @pointsto command-line-arguments.a 104 print(ar0[n]) // @pointsto command-line-arguments.a 105 106 sl1 := []*int{&a} 107 ap1 := (*[1]*int)(sl1) 108 ar1 := *ap1 109 110 ar1[0] = &b 111 print(ap1[n]) // @pointsto command-line-arguments.a 112 print(sl1[n]) // @pointsto command-line-arguments.a 113 print(ar1[n]) // @pointsto command-line-arguments.a | command-line-arguments.b 114 115 sl2 := []*int{&a} 116 ap2 := (*[1]*int)(sl2) 117 ar2 := *ap2 118 119 ap2[0] = &b 120 print(ap2[n]) // @pointsto command-line-arguments.a | command-line-arguments.b 121 print(sl2[n]) // @pointsto command-line-arguments.a | command-line-arguments.b 122 print(ar2[n]) // @pointsto command-line-arguments.a | command-line-arguments.b 123 124 sl3 := []*int{&b, nil} 125 ap3 := (*[1]*int)(sl3) 126 ar3 := *ap3 127 128 print(sl3[n]) // @pointsto command-line-arguments.b 129 print(ap3[n]) // @pointsto command-line-arguments.b 130 print(ar3[n]) // @pointsto command-line-arguments.b 131 } 132 133 func array7() { 134 var n int 135 136 sl0 := []*int{nil, nil, nil} 137 ap0 := (*[2]*int)(sl0) 138 ap1 := (*[1]*int)(sl0[2:]) 139 140 ap1[0] = &a 141 142 print(sl0[n]) // @pointsto command-line-arguments.a 143 print(ap0[n]) // @pointsto command-line-arguments.a 144 print(ap1[n]) // @pointsto command-line-arguments.a 145 } 146 147 func array8() { 148 var n int 149 150 sl1 := make([]*int, 1, 1) 151 sl2 := make([]*int, 1, 1) 152 pa1 := (*[1]*int)(sl1) 153 pa2 := (*[1]*int)(sl2) 154 sl1[0] = &a 155 sl2[0] = &b 156 print(pa1[n]) // @pointsto command-line-arguments.a 157 print(pa2[n]) // @pointsto command-line-arguments.b 158 159 pa2 = pa1 160 print(pa1[n]) // @pointsto command-line-arguments.a 161 print(pa2[n]) // @pointsto command-line-arguments.a 162 } 163 164 func main() { 165 array1() 166 array2() 167 array3() 168 array4() 169 array5() 170 array6() 171 array7() 172 array8() 173 }