]> Cypherpunks.ru repositories - gostls13.git/blob - test/checkbce.go
cmd/compile: bce when max and limit are consts
[gostls13.git] / test / checkbce.go
1 // +build amd64
2 // errorcheck -0 -d=ssa/check_bce/debug=3
3
4 package main
5
6 func f0(a []int) {
7         a[0] = 1 // ERROR "Found IsInBounds$"
8         a[0] = 1
9         a[6] = 1 // ERROR "Found IsInBounds$"
10         a[6] = 1
11         a[5] = 1
12         a[5] = 1
13 }
14
15 func f1(a [256]int, i int) {
16         useInt(a[i])     // ERROR "Found IsInBounds$"
17         useInt(a[i%256]) // ERROR "Found IsInBounds$"
18         useInt(a[i&255])
19         useInt(a[i&17])
20
21         if 4 <= i && i < len(a) {
22                 useInt(a[i])
23                 useInt(a[i-1]) // ERROR "Found IsInBounds$"
24                 useInt(a[i-4]) // ERROR "Found IsInBounds$"
25         }
26 }
27
28 func f2(a [256]int, i uint) {
29         useInt(a[i]) // ERROR "Found IsInBounds$"
30         useInt(a[i%256])
31         useInt(a[i&255])
32         useInt(a[i&17])
33 }
34
35 func f3(a [256]int, i uint8) {
36         useInt(a[i])
37         useInt(a[i+10])
38         useInt(a[i+14])
39 }
40
41 func f4(a [27]int, i uint8) {
42         useInt(a[i%15])
43         useInt(a[i%19])
44         useInt(a[i%27])
45 }
46
47 func f5(a []int) {
48         if len(a) > 5 {
49                 useInt(a[5])
50                 useSlice(a[6:])
51                 useSlice(a[:6]) // ERROR "Found IsSliceInBounds$"
52         }
53 }
54
55 func f6(a [32]int, b [64]int, i int) {
56         useInt(a[uint32(i*0x07C4ACDD)>>27])
57         useInt(b[uint64(i*0x07C4ACDD)>>58])
58         useInt(a[uint(i*0x07C4ACDD)>>59])
59
60         // The following bounds should not be removed because they can overflow.
61         useInt(a[uint32(i*0x106297f105d0cc86)>>26]) // ERROR "Found IsInBounds$"
62         useInt(b[uint64(i*0x106297f105d0cc86)>>57]) // ERROR "Found IsInBounds$"
63         useInt(a[int32(i*0x106297f105d0cc86)>>26])  // ERROR "Found IsInBounds$"
64         useInt(b[int64(i*0x106297f105d0cc86)>>57])  // ERROR "Found IsInBounds$"
65 }
66
67 func g1(a []int) {
68         for i := range a {
69                 a[i] = i
70                 useSlice(a[:i+1])
71                 useSlice(a[:i])
72         }
73 }
74
75 func g2(a []int) {
76         useInt(a[3]) // ERROR "Found IsInBounds$"
77         useInt(a[2])
78         useInt(a[1])
79         useInt(a[0])
80 }
81
82 func g3(a []int) {
83         for i := range a[:256] { // ERROR "Found IsSliceInBounds$"
84                 useInt(a[i]) // ERROR "Found IsInBounds$"
85         }
86         b := a[:256]
87         for i := range b {
88                 useInt(b[i])
89         }
90 }
91
92 func g4(a [100]int) {
93         for i := 10; i < 50; i++ {
94                 useInt(a[i-10])
95                 useInt(a[i])
96                 useInt(a[i+25])
97                 useInt(a[i+50])
98
99                 // The following are out of bounds.
100                 useInt(a[i-11]) // ERROR "Found IsInBounds$"
101                 useInt(a[i+51]) // ERROR "Found IsInBounds$"
102         }
103 }
104
105 //go:noinline
106 func useInt(a int) {
107 }
108
109 //go:noinline
110 func useSlice(a []int) {
111 }
112
113 func main() {
114 }