summaryrefslogtreecommitdiffstats
path: root/test/fixedbugs/issue29504.go
blob: e311f84edcb263f442e2b5cc331797fa82ee0095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// run

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Make sure that in code involving indexing, the bounds
// check always fails at the line number of the '[' token.

package main

import (
	"fmt"
	"runtime"
	"strings"
)

type T struct{ a, b, c, d, e int } // unSSAable

func main() {
	shouldPanic(func() {
		var a [1]int
		sink = a /*line :999999:1*/ [ /*line :100:1*/ i]
	})
	shouldPanic(func() {
		var a [3]int
		sink = a /*line :999999:1*/ [ /*line :200:1*/ i]
	})
	shouldPanic(func() {
		var a []int
		sink = a /*line :999999:1*/ [ /*line :300:1*/ i]
	})
	shouldPanic(func() {
		var a [1]int
		a /*line :999999:1*/ [ /*line :400:1*/ i] = 1
	})
	shouldPanic(func() {
		var a [3]int
		a /*line :999999:1*/ [ /*line :500:1*/ i] = 1
	})
	shouldPanic(func() {
		var a []int
		a /*line :999999:1*/ [ /*line :600:1*/ i] = 1
	})

	shouldPanic(func() {
		var a [3]T
		sinkT = a /*line :999999:1*/ [ /*line :700:1*/ i]
	})
	shouldPanic(func() {
		var a []T
		sinkT = a /*line :999999:1*/ [ /*line :800:1*/ i]
	})
	shouldPanic(func() {
		var a [3]T
		a /*line :999999:1*/ [ /*line :900:1*/ i] = T{}
	})
	shouldPanic(func() {
		var a []T
		a /*line :999999:1*/ [ /*line :1000:1*/ i] = T{}
	})

	shouldPanic(func() {
		var a [3]int
		sinkS = a /*line :999999:1*/ [ /*line :1100:1*/ i:]
	})
	shouldPanic(func() {
		var a []int
		sinkS = a /*line :999999:1*/ [ /*line :1200:1*/ i:]
	})
	shouldPanic(func() {
		var a [3]int
		sinkS = a /*line :999999:1*/ [: /*line :1300:1*/ i]
	})
	shouldPanic(func() {
		var a []int
		sinkS = a /*line :999999:1*/ [: /*line :1400:1*/ i]
	})

	shouldPanic(func() {
		var a [3]T
		sinkST = a /*line :999999:1*/ [ /*line :1500:1*/ i:]
	})
	shouldPanic(func() {
		var a []T
		sinkST = a /*line :999999:1*/ [ /*line :1600:1*/ i:]
	})
	shouldPanic(func() {
		var a [3]T
		sinkST = a /*line :999999:1*/ [: /*line :1700:1*/ i]
	})
	shouldPanic(func() {
		var a []T
		sinkST = a /*line :999999:1*/ [: /*line :1800:1*/ i]
	})

	shouldPanic(func() {
		s := "foo"
		sinkB = s /*line :999999:1*/ [ /*line :1900:1*/ i]
	})
	shouldPanic(func() {
		s := "foo"
		sinkStr = s /*line :999999:1*/ [ /*line :2000:1*/ i:]
	})
	shouldPanic(func() {
		s := "foo"
		sinkStr = s /*line :999999:1*/ [: /*line :2100:1*/ i]
	})

	if bad {
		panic("ERRORS")
	}
}

var i = 9
var sink int
var sinkS []int
var sinkT T
var sinkST []T
var sinkB byte
var sinkStr string

var bad = false

func shouldPanic(f func()) {
	defer func() {
		if recover() == nil {
			panic("did not panic")
		}
		var pcs [10]uintptr
		n := runtime.Callers(1, pcs[:])
		iter := runtime.CallersFrames(pcs[:n])
		buf := ""
		for {
			frame, more := iter.Next()
			buf += fmt.Sprintf("%s:%d %s\n", frame.File, frame.Line, frame.Function)
			if !more {
				break
			}
		}
		if !strings.Contains(buf, "999999") {
			fmt.Printf("could not find marker line in traceback:\n%s\n", buf)
			bad = true
		}
	}()
	f()
}