| 1 |
// run |
| 2 |
// Copyright 2025 The Go Authors. All rights reserved. |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
package main |
| 6 |
|
| 7 |
//go:noinline |
| 8 |
func i() { |
| 9 |
for range yieldInts { |
| 10 |
defer func() { |
| 11 |
println("I") |
| 12 |
recover() |
| 13 |
}() |
| 14 |
} |
| 15 |
// This panic causes dead code elimination of the return block. |
| 16 |
// The compiler should nonetheless emit a deferreturn. |
| 17 |
panic("i panic") |
| 18 |
} |
| 19 |
|
| 20 |
//go:noinline |
| 21 |
func h() { |
| 22 |
defer func() { |
| 23 |
println("H first") |
| 24 |
}() |
| 25 |
for range yieldInts { |
| 26 |
defer func() { |
| 27 |
println("H second") |
| 28 |
}() |
| 29 |
} |
| 30 |
defer func() { |
| 31 |
println("H third") |
| 32 |
}() |
| 33 |
for range yieldIntsPanic { |
| 34 |
defer func() { |
| 35 |
println("h recover:called") |
| 36 |
recover() |
| 37 |
}() |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
//go:noinline |
| 42 |
func yieldInts(yield func(int) bool) { |
| 43 |
if !yield(0) { |
| 44 |
return |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
//go:noinline |
| 49 |
func g() { |
| 50 |
defer func() { |
| 51 |
println("G first") |
| 52 |
}() |
| 53 |
for range yieldIntsPanic { |
| 54 |
defer func() { |
| 55 |
println("g recover:called") |
| 56 |
recover() |
| 57 |
}() |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
//go:noinline |
| 62 |
func yieldIntsPanic(yield func(int) bool) { |
| 63 |
if !yield(0) { |
| 64 |
return |
| 65 |
} |
| 66 |
panic("yield stop") |
| 67 |
} |
| 68 |
|
| 69 |
//go:noinline |
| 70 |
func next(i int) int { |
| 71 |
if i == 0 { |
| 72 |
panic("next stop") |
| 73 |
} |
| 74 |
return i + 1 |
| 75 |
} |
| 76 |
|
| 77 |
//go:noinline |
| 78 |
func f() { |
| 79 |
defer func() { |
| 80 |
println("F first") |
| 81 |
}() |
| 82 |
for i := 0; i < 1; i = next(i) { |
| 83 |
defer func() { |
| 84 |
println("f recover:called") |
| 85 |
recover() |
| 86 |
}() |
| 87 |
} |
| 88 |
} |
| 89 |
func main() { |
| 90 |
f() |
| 91 |
println("f returned") |
| 92 |
g() |
| 93 |
println("g returned") |
| 94 |
h() |
| 95 |
println("h returned") |
| 96 |
i() |
| 97 |
println("i returned") |
| 98 |
|
| 99 |
} |