1 // $G $D/$F.go && $L $F.$A &&
2 // ./$A.out -pass 0 >tmp.go && $G tmp.go && $L -o $A.out1 tmp.$A && ./$A.out1 &&
3 // ./$A.out -pass 1 >tmp.go && errchk $G -e tmp.go &&
4 // ./$A.out -pass 2 >tmp.go && errchk $G -e tmp.go
5 // rm -f tmp.go $A.out1
7 // Copyright 2010 The Go Authors. All rights reserved.
8 // Use of this source code is governed by a BSD-style
9 // license that can be found in the LICENSE file.
11 // Generate test of index and slice bounds checks.
12 // The output is compiled and run.
31 type quad struct { x, y, z, w int }
39 ci64bigger int64 = 1<<32
46 cni64big int64 = -1<<31
47 cni64bigger int64 = -1<<32
55 var i64big int64 = 1<<31
56 var i64bigger int64 = 1<<32
57 var huge uint64 = 1<<64 - 1
63 var ni64big int64 = -1<<31
64 var ni64bigger int64 = -1<<32
65 var nhuge int64 = -1<<63
67 var si []int = make([]int, 10)
69 var pai *[10]int = &ai
71 var sq []quad = make([]quad, 10)
73 var paq *[10]quad = &aq
84 var t = T{si, ai, pai, sq, aq, paq}
86 var pt = &T{si, ai, pai, sq, aq, paq}
89 func test(f func(), s string) {
91 if err := recover(); err == nil {
92 _, file, line, _ := runtime.Caller(2)
94 print(file, ":", line, ": ", s, " did not panic\n")
101 func use(y interface{}) {
118 // 0 - dynamic checks
119 // 1 - static checks of invalid constants (cannot assign to types)
120 // 2 - static checks of array bounds
121 var pass = flag.Int("pass", 0, "which test (0,1,2)")
123 func testExpr(b *bufio.Writer, expr string) {
125 fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr)
127 fmt.Fprintf(b, "\tuse(%s) // ERROR \"index|overflow\"\n", expr)
132 b := bufio.NewWriter(os.Stdout)
137 fmt.Fprint(b, "// $G $D/$F.go && $L $F.$A && ./$A.out\n\n")
139 fmt.Fprint(b, "// errchk $G -e $D/$F.go\n\n")
141 fmt.Fprint(b, prolog)
143 var choices = [][]string{
144 // Direct value, fetch from struct, fetch from struct pointer.
145 // The last two cases get us to oindex_const_sudo in gsubr.c.
146 []string{"", "t.", "pt."},
148 // Array, pointer to array, slice.
149 []string{"a", "pa", "s"},
151 // Element is int, element is quad (struct).
152 // This controls whether we end up in gsubr.c (i) or cgen.c (q).
155 // Variable or constant.
158 // Positive or negative.
162 []string{"j", "i", "i32", "i64", "i64big", "i64bigger", "huge"},
165 forall(choices, func(x []string) {
166 p, a, e, c, n, i := x[0], x[1], x[2], x[3], x[4], x[5]
168 // Pass: dynamic=0, static=1, 2.
169 // Which cases should be caught statically?
170 // Only constants, obviously.
171 // Beyond that, must be one of these:
172 // indexing into array or pointer to array
176 if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge") {
178 // Due to a detail of 6g's internals,
179 // the huge constant errors happen in an
180 // earlier pass than the others and inhibits
181 // the next pass from running.
182 // So run it as a separate check.
189 // Only print the test case if it is appropriate for this pass.
190 if thisPass == *pass {
195 testExpr(b, pae + "[" + cni + "]")
198 // Low index 0 is a special case in ggen.c
199 // so test both 0 and 1.
200 testExpr(b, pae + "[0:" + cni + "]")
201 testExpr(b, pae + "[1:" + cni + "]")
202 testExpr(b, pae + "[" + cni + ":]")
203 testExpr(b, pae + "[" + cni + ":" + cni + "]")
211 func forall(choices [][]string, f func([]string)) {
212 x := make([]string, len(choices))
214 var recurse func(d int)
215 recurse = func(d int) {
216 if d >= len(choices) {
220 for _, x[d] = range choices[d] {