1 // $G $D/$F.go && $L $F.$A &&
2 // ./$A.out -pass 0 >tmp.go && $G tmp.go && $L -o tmp1.$A tmp.$A && ./tmp1.$A &&
3 // ./$A.out -pass 1 >tmp.go && errchk $G -e tmp.go &&
4 // ./$A.out -pass 2 >tmp.go && errchk $G -e tmp.go
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.
30 type quad struct { x, y, z, w int }
38 ci64bigger int64 = 1<<32
45 cni64big int64 = -1<<31
46 cni64bigger int64 = -1<<32
54 var i64big int64 = 1<<31
55 var i64bigger int64 = 1<<32
56 var huge uint64 = 1<<64 - 1
62 var ni64big int64 = -1<<31
63 var ni64bigger int64 = -1<<32
64 var nhuge int64 = -1<<63
66 var si []int = make([]int, 10)
68 var pai *[10]int = &ai
70 var sq []quad = make([]quad, 10)
72 var paq *[10]quad = &aq
83 var t = T{si, ai, pai, sq, aq, paq}
85 var pt = &T{si, ai, pai, sq, aq, paq}
88 func test(f func(), s string) {
90 if err := recover(); err == nil {
91 _, file, line, _ := runtime.Caller(2)
93 print(file, ":", line, ": ", s, " did not panic\n")
100 func use(y interface{}) {
117 // 0 - dynamic checks
118 // 1 - static checks of invalid constants (cannot assign to types)
119 // 2 - static checks of array bounds
120 var pass = flag.Int("pass", 0, "which test (0,1,2)")
122 func testExpr(b *bufio.Writer, expr string) {
124 fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr)
126 fmt.Fprintf(b, "\tuse(%s) // ERROR \"index|overflow\"\n", expr)
131 b := bufio.NewWriter(os.Stdout)
136 fmt.Fprint(b, "// $G $D/$F.go && $L $F.$A && ./$A.out\n\n")
138 fmt.Fprint(b, "// errchk $G -e $D/$F.go\n\n")
140 fmt.Fprint(b, prolog)
142 var choices = [][]string{
143 // Direct value, fetch from struct, fetch from struct pointer.
144 // The last two cases get us to oindex_const_sudo in gsubr.c.
145 []string{"", "t.", "pt."},
147 // Array, pointer to array, slice.
148 []string{"a", "pa", "s"},
150 // Element is int, element is quad (struct).
151 // This controls whether we end up in gsubr.c (i) or cgen.c (q).
154 // Variable or constant.
157 // Positive or negative.
161 []string{"j", "i", "i32", "i64", "i64big", "i64bigger", "huge"},
164 forall(choices, func(x []string) {
165 p, a, e, c, n, i := x[0], x[1], x[2], x[3], x[4], x[5]
167 // Pass: dynamic=0, static=1, 2.
168 // Which cases should be caught statically?
169 // Only constants, obviously.
170 // Beyond that, must be one of these:
171 // indexing into array or pointer to array
175 if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge") {
177 // Due to a detail of 6g's internals,
178 // the huge constant errors happen in an
179 // earlier pass than the others and inhibits
180 // the next pass from running.
181 // So run it as a separate check.
188 // Only print the test case if it is appropriate for this pass.
189 if thisPass == *pass {
194 testExpr(b, pae + "[" + cni + "]")
197 // Low index 0 is a special case in ggen.c
198 // so test both 0 and 1.
199 testExpr(b, pae + "[0:" + cni + "]")
200 testExpr(b, pae + "[1:" + cni + "]")
201 testExpr(b, pae + "[" + cni + ":]")
202 testExpr(b, pae + "[" + cni + ":" + cni + "]")
210 func forall(choices [][]string, f func([]string)) {
211 x := make([]string, len(choices))
213 var recurse func(d int)
214 recurse = func(d int) {
215 if d >= len(choices) {
219 for _, x[d] = range choices[d] {