defer.go
1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// RUN: llgo -o %t %s
// RUN: %t 2>&1 | FileCheck %s
// CHECK: f2.1
// CHECK-NEXT: f5
// CHECK-NEXT: recovered no error
// CHECK-NEXT: f5
// CHECK-NEXT: recovered: meep meep
// CHECK-NEXT: 888
// CHECK-NEXT: f5
// CHECK-NEXT: recovered no error
// CHECK-NEXT: f5
// CHECK-NEXT: recovered no error
// CHECK-NEXT: 888
// CHECK-NEXT: 456
// CHECK-NEXT: 999
// CHECK-NEXT: 999
// CHECK-NEXT: 123
// CHECK-NEXT: 999
// CHECK-NEXT: 999
// CHECK-NEXT: 246
// CHECK-NEXT: f2.2
// CHECK-NEXT: f2.3
// CHECK-NEXT: f1.1
// CHECK-NEXT: f1.2
// CHECK-NEXT: recovered: second
// CHECK-NEXT: ahoy
package main
type T struct {
value int
}
type T1 struct {
T
}
func (t T) abc() {
println(t.value)
}
func (t *T) def() {
println(t.value)
}
func (t *T) ghi(v int) {
println(v)
}
func printerr(err interface{}) {
if err != nil {
println("recovered:", err.(string))
} else {
println("recovered no error")
}
}
func f6() {
defer func() { printerr(recover()) }()
defer func() { panic("second") }()
panic("first")
}
func f5(panic_ bool) {
var t1 T1
t1.T.value = 888
defer t1.abc()
var f func(int)
f = func(recursion int) {
if recursion > 0 {
f(recursion - 1)
return
}
println("f5")
printerr(recover())
}
defer f(0) // will recover (after f(1))
defer f(1) // won't recover
if panic_ {
panic("meep meep")
}
}
func f4() {
var a T = T{999}
var b *T = &a
defer a.abc()
defer a.def()
defer a.ghi(123)
defer b.abc()
defer b.def()
defer b.ghi(456)
f5(true)
f5(false) // verify the recover in f5 works
}
func f3() (a int) {
defer func() { a *= 2 }()
f4()
return 123
}
func f2() {
defer func() { println("f2.3") }()
defer func(s string) { println(s) }("f2.2")
println("f2.1")
println(f3())
}
func f1() {
defer func() { println("f1.2") }()
defer func() { println("f1.1") }()
f2()
}
func builtins() {
defer println("ahoy")
}
func main() {
f1()
f6()
builtins()
}