...
1
2
3
4
5
6
7 package main
8
9 func left(x int) { _ = 1 << x }
10 func right(x int) { _ = 1 >> x }
11
12 func main() {
13 wantPanic(
14 func() {
15 left(-1)
16 },
17 "runtime error: negative shift amount",
18 )
19 wantPanic(
20 func() {
21 right(-1)
22 },
23 "runtime error: negative shift amount",
24 )
25 wantPanic(
26 func() {
27 const maxInt32 = 1<<31 - 1
28 var idx int64 = maxInt32*2 + 8
29 x := make([]int, 16)
30 _ = x[idx]
31 },
32 "runtime error: runtime error: index out of range [4294967302] with length 16",
33 )
34 }
35
36 func wantPanic(fn func(), s string) {
37 defer func() {
38 err := recover()
39 if err == nil {
40 panic("expected panic")
41 }
42 if got := err.(error).Error(); got != s {
43 panic("expected panic " + s + " got " + got)
44 }
45 }()
46 fn()
47 }
48
View as plain text