-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathcapture_order.swift
167 lines (145 loc) · 4.68 KB
/
capture_order.swift
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// RUN: %target-swift-emit-silgen %s -verify
/// We emit an invalid forward capture as an 'undef'; make sure
/// we cover the various possible cases.
public func captureBeforeDefLet(amount: Int) -> () -> Int {
func getter() -> Int { // expected-error {{closure captures 'modifiedAmount' before it is declared}}
return modifiedAmount // expected-note {{captured here}}
}
let closure = getter
let modifiedAmount = amount // expected-note{{captured value declared here}}
return closure
}
public func captureBeforeDefVar(amount: Int) -> () -> Int {
func incrementor() -> Int { // expected-error {{closure captures 'currentTotal' before it is declared}}
currentTotal += amount // expected-note {{captured here}}
return currentTotal
}
let closure = incrementor
var currentTotal = 0 // expected-note{{captured value declared here}}
currentTotal = 1
return closure
}
public func captureBeforeDefWeakVar(obj: AnyObject) -> () -> AnyObject? {
func getter() -> AnyObject? { // expected-error {{closure captures 'weakObj' before it is declared}}
return weakObj // expected-note {{captured here}}
}
let closure = getter
weak var weakObj: AnyObject? = obj // expected-note{{captured value declared here}}
return closure
}
public func captureBeforeDefUnownedLet(obj: AnyObject) -> () -> AnyObject? {
func getter() -> AnyObject? { // expected-error {{closure captures 'unownedObj' before it is declared}}
return unownedObj // expected-note {{captured here}}
}
let closure = getter
unowned let unownedObj: AnyObject = obj // expected-note{{captured value declared here}}
return closure
}
public func captureBeforeDefUnownedVar(obj: AnyObject) -> () -> AnyObject? {
func getter() -> AnyObject? { // expected-error {{closure captures 'unownedObj' before it is declared}}
return unownedObj // expected-note {{captured here}}
}
let closure = getter
unowned var unownedObj: AnyObject = obj // expected-note{{captured value declared here}}
// expected-warning@-1 {{variable 'unownedObj' was never mutated; consider changing to 'let' constant}}
return closure
}
/// Examples of transitive capture
func pingpong() {
func ping() -> Int {
return pong()
}
func pong() -> Int {
return ping()
}
_ = ping()
}
func transitiveForwardCapture() {
func ping() -> Int { // expected-error {{closure captures 'x' before it is declared}}
return pong()
}
_ = ping()
var x = 1 // expected-note {{captured value declared here}}
func pong() -> Int {
x += 1 // expected-note {{captured here}}
return ping()
}
}
func transitiveForwardCapture2() {
func ping() -> Int { // expected-error {{closure captures 'x' before it is declared}}
_ = pong()
}
_ = ping()
var x = 1 // expected-note {{captured value declared here}}
func pong() -> Int {
_ = pung()
}
func pung() -> Int {
x += 1 // expected-note {{captured here}}
return ping()
}
}
func transitiveForwardCapture3() {
var y = 2
func ping() -> Int { // expected-error {{closure captures 'x' before it is declared}}
_ = pong()
}
_ = ping()
var x = 1 // expected-note {{captured value declared here}}
func pung() -> Int {
x += 1 // expected-note {{captured here}}
return ping()
}
func pong() -> Int {
y += 2
_ = pung()
}
}
func captureInClosure() {
let x = { (i: Int) in // expected-error {{closure captures 'currentTotal' before it is declared}}
currentTotal += i // expected-note {{captured here}}
}
var currentTotal = 0 // expected-note {{captured value declared here}}
_ = x
}
/// Regression tests
class SR4812 {
public func foo() {
let bar = { [weak self] in
// expected-error@-1 {{closure captures 'bar' before it is declared}}
// expected-note@-2 {{captured value declared here}}
// expected-warning@-3 {{variable 'self' was written to, but never read}}
bar2()
}
func bar2() {
bar() // expected-note {{captured here}}
}
bar()
}
}
func timeout(_ f: @escaping () -> Void) {
f()
}
func sr10687() {
timeout { // expected-error {{closure captures 'x' before it is declared}}
proc()
}
let x = 0 // expected-note {{captured value declared here}}
func proc() {
_ = x // expected-note {{captured here}}
}
}
class rdar40600800 {
func foo() {
let callback = { // expected-error {{closure captures 'callback' before it is declared}}
// expected-note@-1 {{captured value declared here}}
innerFunction()
}
func innerFunction() {
let closure = {
// expected-warning@-1 {{initialization of immutable value 'closure' was never used; consider replacing with assignment to '_' or removing it}}
callback() // expected-note {{captured here}}
}
}
}
}