| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2016 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 |
var out int |
| 10 |
var zero int |
| 11 |
|
| 12 |
func main() { |
| 13 |
wantPanic("test1", func() { |
| 14 |
out = 1 / zero |
| 15 |
}) |
| 16 |
wantPanic("test2", func() { |
| 17 |
_ = 1 / zero |
| 18 |
}) |
| 19 |
wantPanic("test3", func() { |
| 20 |
v := 0 |
| 21 |
_ = 1 / v |
| 22 |
}) |
| 23 |
wantPanic("test4", func() { divby(0) }) |
| 24 |
} |
| 25 |
|
| 26 |
func wantPanic(test string, fn func()) { |
| 27 |
defer func() { |
| 28 |
if e := recover(); e == nil { |
| 29 |
panic(test + ": expected panic") |
| 30 |
} |
| 31 |
}() |
| 32 |
fn() |
| 33 |
} |
| 34 |
|
| 35 |
//go:noinline |
| 36 |
func divby(v int) { |
| 37 |
_ = 1 / v |
| 38 |
} |