Keith Randall | ddc6b64 | 2016-03-09 19:27:57 -0800 | [diff] [blame] | 1 | // run |
| 2 | |
Emmanuel Odeke | 53fd522 | 2016-04-10 14:32:26 -0700 | [diff] [blame] | 3 | // Copyright 2016 The Go Authors. All rights reserved. |
Keith Randall | ddc6b64 | 2016-03-09 19:27:57 -0800 | [diff] [blame] | 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 "fmt" |
| 10 | |
| 11 | func f1() (x int) { |
| 12 | for { |
| 13 | defer func() { |
| 14 | recover() |
| 15 | x = 1 |
| 16 | }() |
| 17 | panic(nil) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | var sink *int |
| 22 | |
| 23 | func f2() (x int) { |
| 24 | sink = &x |
| 25 | defer func() { |
| 26 | recover() |
| 27 | x = 1 |
| 28 | }() |
| 29 | panic(nil) |
| 30 | } |
| 31 | |
| 32 | func f3(b bool) (x int) { |
| 33 | sink = &x |
| 34 | defer func() { |
| 35 | recover() |
| 36 | x = 1 |
| 37 | }() |
| 38 | if b { |
| 39 | panic(nil) |
| 40 | } |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | func main() { |
| 45 | if x := f1(); x != 1 { |
| 46 | panic(fmt.Sprintf("f1 returned %d, wanted 1", x)) |
| 47 | } |
| 48 | if x := f2(); x != 1 { |
| 49 | panic(fmt.Sprintf("f2 returned %d, wanted 1", x)) |
| 50 | } |
| 51 | if x := f3(true); x != 1 { |
| 52 | panic(fmt.Sprintf("f3(true) returned %d, wanted 1", x)) |
| 53 | } |
| 54 | if x := f3(false); x != 1 { |
| 55 | panic(fmt.Sprintf("f3(false) returned %d, wanted 1", x)) |
| 56 | } |
| 57 | } |