3 // Copyright 2010 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Generate test of index and slice bounds checks.
8 // The actual tests are index0.go, index1.go, index2.go.
27 type quad struct { x, y, z, w int }
37 ci64bigger int64 = 1<<32
48 cni64big int64 = -1<<31
49 cni64bigger int64 = -1<<32
59 var i32 int32 = 100022
60 var i64 int64 = 100023
61 var i64big int64 = 1<<31
62 var i64bigger int64 = 1<<32
63 var huge uint64 = 1<<64 - 1
64 var fgood float64 = 2.0
65 var fbad float64 = 2.1
73 var ni64big int64 = -1<<31
74 var ni64bigger int64 = -1<<32
75 var nhuge int64 = -1<<63
76 var nfgood float64 = -2.0
77 var nfbad float64 = -2.1
79 var si []int = make([]int, 10)
81 var pai *[10]int = &ai
83 var sq []quad = make([]quad, 10)
85 var paq *[10]quad = &aq
87 var sib []int = make([]int, 100000)
89 var paib *[100000]int = &aib
91 var sqb []quad = make([]quad, 100000)
93 var paqb *[100000]quad = &aqb
111 var t = T{si, ai, pai, sq, aq, paq, sib, aib, paib, sqb, aqb, paqb}
113 var pt = &T{si, ai, pai, sq, aq, paq, sib, aib, paib, sqb, aqb, paqb}
115 // test that f panics
116 func test(f func(), s string) {
118 if err := recover(); err == nil {
119 _, file, line, _ := runtime.Caller(2)
121 print(file, ":", line, ": ", s, " did not panic\n")
122 } else if !contains(err.(error).Error(), "out of range") {
123 _, file, line, _ := runtime.Caller(2)
125 print(file, ":", line, ": ", s, " unexpected panic: ", err.(error).Error(), "\n")
131 func contains(x, y string) bool {
132 for i := 0; i+len(y) <= len(x); i++ {
133 if x[i:i+len(y)] == y {
142 func use(y interface{}) {
158 // pass variable set in index[012].go
159 // 0 - dynamic checks
160 // 1 - static checks of invalid constants (cannot assign to types)
161 // 2 - static checks of array bounds
163 func testExpr(b *bufio.Writer, expr string) {
165 fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr)
167 fmt.Fprintf(b, "\tuse(%s) // ERROR \"index|overflow|truncated|must be integer\"\n", expr)
172 b := bufio.NewWriter(os.Stdout)
175 fmt.Fprint(b, "// run\n\n")
177 fmt.Fprint(b, "// errorcheck\n\n")
179 fmt.Fprint(b, prolog)
181 var choices = [][]string{
182 // Direct value, fetch from struct, fetch from struct pointer.
183 // The last two cases get us to oindex_const_sudo in gsubr.c.
184 []string{"", "t.", "pt."},
186 // Array, pointer to array, slice.
187 []string{"a", "pa", "s"},
189 // Element is int, element is quad (struct).
190 // This controls whether we end up in gsubr.c (i) or cgen.c (q).
196 // Variable or constant.
199 // Positive or negative.
203 []string{"j", "i", "i8", "i16", "i32", "i64", "i64big", "i64bigger", "huge", "fgood", "fbad"},
206 forall(choices, func(x []string) {
207 p, a, e, big, c, n, i := x[0], x[1], x[2], x[3], x[4], x[5], x[6]
209 // Pass: dynamic=0, static=1, 2.
210 // Which cases should be caught statically?
211 // Only constants, obviously.
212 // Beyond that, must be one of these:
213 // indexing into array or pointer to array
217 if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge" || i == "fbad") {
219 // Due to a detail of gc's internals,
220 // the huge constant errors happen in an
221 // earlier pass than the others and inhibits
222 // the next pass from running.
223 // So run it as a separate check.
225 } else if a == "s" && n == "" && (i == "i64big" || i == "i64bigger") && unsafe.Sizeof(int(0)) > 4 {
226 // If int is 64 bits, these huge
227 // numbers do fit in an int, so they
228 // are not rejected at compile time.
235 pae := p + a + e + big
238 // If we're using the big-len data, positive int8 and int16 cannot overflow.
239 if big == "b" && n == "" && (i == "i8" || i == "i16") {
241 fmt.Fprintf(b, "\tuse(%s[%s])\n", pae, cni)
242 fmt.Fprintf(b, "\tuse(%s[0:%s])\n", pae, cni)
243 fmt.Fprintf(b, "\tuse(%s[1:%s])\n", pae, cni)
244 fmt.Fprintf(b, "\tuse(%s[%s:])\n", pae, cni)
245 fmt.Fprintf(b, "\tuse(%s[%s:%s])\n", pae, cni, cni)
250 // Float variables cannot be used as indices.
251 if c == "" && (i == "fgood" || i == "fbad") {
254 // Integral float constant is ok.
255 if c == "c" && n == "" && i == "fgood" {
257 fmt.Fprintf(b, "\tuse(%s[%s])\n", pae, cni)
258 fmt.Fprintf(b, "\tuse(%s[0:%s])\n", pae, cni)
259 fmt.Fprintf(b, "\tuse(%s[1:%s])\n", pae, cni)
260 fmt.Fprintf(b, "\tuse(%s[%s:])\n", pae, cni)
261 fmt.Fprintf(b, "\tuse(%s[%s:%s])\n", pae, cni, cni)
266 // Only print the test case if it is appropriate for this pass.
267 if thisPass == pass {
269 testExpr(b, pae+"["+cni+"]")
272 // Low index 0 is a special case in ggen.c
273 // so test both 0 and 1.
274 testExpr(b, pae+"[0:"+cni+"]")
275 testExpr(b, pae+"[1:"+cni+"]")
276 testExpr(b, pae+"["+cni+":]")
277 testExpr(b, pae+"["+cni+":"+cni+"]")
285 func forall(choices [][]string, f func([]string)) {
286 x := make([]string, len(choices))
288 var recurse func(d int)
289 recurse = func(d int) {
290 if d >= len(choices) {
294 for _, x[d] = range choices[d] {