| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2024 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. |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"strings" |
| 11 |
"unsafe" |
| 12 |
) |
| 13 |
|
| 14 |
func main() { |
| 15 |
shouldPanic("runtime error: index out of range", func() { f(0) }) |
| 16 |
shouldPanic("runtime error: index out of range", func() { g(0) }) |
| 17 |
} |
| 18 |
|
| 19 |
func f[T byte](t T) { |
| 20 |
const str = "a" |
| 21 |
_ = str[unsafe.Sizeof(t)] |
| 22 |
} |
| 23 |
|
| 24 |
func g[T byte](t T) { |
| 25 |
const str = "a" |
| 26 |
_ = str[unsafe.Sizeof(t)+0] |
| 27 |
} |
| 28 |
|
| 29 |
func shouldPanic(str string, f func()) { |
| 30 |
defer func() { |
| 31 |
err := recover() |
| 32 |
if err == nil { |
| 33 |
panic("did not panic") |
| 34 |
} |
| 35 |
s := err.(error).Error() |
| 36 |
if !strings.Contains(s, str) { |
| 37 |
panic("got panic " + s + ", want " + str) |
| 38 |
} |
| 39 |
}() |
| 40 |
|
| 41 |
f() |
| 42 |
} |