You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: test/expr/closure/closures.swift
+11-5Lines changed: 11 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -177,8 +177,6 @@ class ExplicitSelfRequiredTest {
177
177
doVoidStuff({ _ ="\(x)"}) // expected-error {{reference to property 'x' in closure requires explicit use of 'self' to make capture semantics explicit}} expected-note{{capture 'self' explicitly to enable implicit 'self' in this closure}} {{18-18= [self] in}} expected-note{{reference 'self.' explicitly}} {{26-26=self.}}
178
178
doVoidStuff({[y =self]in x +=1}) // expected-warning {{capture 'y' was never used}} expected-error {{reference to property 'x' in closure requires explicit use of 'self' to make capture semantics explicit}} expected-note{{capture 'self' explicitly to enable implicit 'self' in this closure}} {{20-20=self, }} expected-note{{reference 'self.' explicitly}} {{33-33=self.}}
179
179
doStuff({[y =self]in x+1}) // expected-warning {{capture 'y' was never used}} expected-error {{reference to property 'x' in closure requires explicit use of 'self' to make capture semantics explicit}} expected-note{{capture 'self' explicitly to enable implicit 'self' in this closure}} {{16-16=self, }} expected-note{{reference 'self.' explicitly}} {{29-29=self.}}
180
-
doVoidStuff({[weak self]in x +=1}) // expected-error {{explicit use of 'self' is required when 'self' is optional, to make control flow explicit}} expected-note {{reference 'self?.' explicitly}} expected-warning {{variable 'self' was written to, but never read}}
181
-
doStuff({[weak self]in x+1}) // expected-error {{explicit use of 'self' is required when 'self' is optional, to make control flow explicit}} expected-note {{reference 'self?.' explicitly}}
182
180
doVoidStuff({[self=ExplicitSelfRequiredTest()]in x +=1}) // expected-note {{variable other than 'self' captured here under the name 'self' does not enable implicit 'self'}} expected-warning {{reference to property 'x' in closure requires explicit use of 'self' to make capture semantics explicit; this is an error in Swift 6}}
183
181
doStuff({[self=ExplicitSelfRequiredTest()]in x+1}) // expected-note {{variable other than 'self' captured here under the name 'self' does not enable implicit 'self'}} expected-warning {{reference to property 'x' in closure requires explicit use of 'self' to make capture semantics explicit; this is an error in Swift 6}}
184
182
@@ -189,8 +187,6 @@ class ExplicitSelfRequiredTest {
189
187
doVoidStuff{()->()in _ =method()} // expected-error {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}} expected-note{{capture 'self' explicitly to enable implicit 'self' in this closure}} {{18-18= [self]}} expected-note{{reference 'self.' explicitly}} {{35-35=self.}}
190
188
doVoidStuff{[y =self]in _ =method()} // expectedexpected-error {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}} expected-note{{capture 'self' explicitly to enable implicit 'self' in this closure}} {{20-20=self, }} expected-note{{reference 'self.' explicitly}} {{37-37=self.}}
191
189
doStuff({[y =self]inmethod()}) // expected-error {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}} expected-note{{capture 'self' explicitly to enable implicit 'self' in this closure}} {{16-16=self, }} expected-note{{reference 'self.' explicitly}} {{29-29=self.}}
192
-
doVoidStuff({[weak self]in _ =method()}) // expected-error {{explicit use of 'self' is required when 'self' is optional, to make control flow explicit}} expected-note{{reference 'self?.' explicitly}}
193
-
doStuff({[weak self]inmethod()}) // expected-error {{explicit use of 'self' is required when 'self' is optional, to make control flow explicit}} expected-note{{reference 'self?.' explicitly}}
194
190
doVoidStuff({[self=ExplicitSelfRequiredTest()]in _ =method()}) // expected-note {{variable other than 'self' captured here under the name 'self' does not enable implicit 'self'}} expected-warning {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit; this is an error in Swift 6}}
195
191
doStuff({[self=ExplicitSelfRequiredTest()]inmethod()}) // expected-note {{variable other than 'self' captured here under the name 'self' does not enable implicit 'self'}} expected-warning {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit; this is an error in Swift 6}}
196
192
doVoidStuff{ _ =self.method()}
@@ -228,7 +224,7 @@ class ExplicitSelfRequiredTest {
228
224
}
229
225
// expected-note@+2 {{capture 'self' explicitly to enable implicit 'self' in this closure}} {{14-14= [self] in}}
230
226
// Note: Trailing whitespace on the following line is intentional and should not be removed!
231
-
doStuff{
227
+
doStuff{
232
228
method() // expected-error {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}} expected-note{{reference 'self.' explicitly}} {{7-7=self.}}
233
229
}
234
230
// expected-note@+1 {{capture 'self' explicitly to enable implicit 'self' in this closure}} {{14-14= [self] in}}
@@ -260,6 +256,16 @@ class ExplicitSelfRequiredTest {
260
256
261
257
return42
262
258
}
259
+
260
+
// The error emitted by these cases cause `VarDeclUsageChecker` to not run analysis on this method,
261
+
// because its `sawError` flag is set to true. To preserve the "capture 'y' was never used" warnings
262
+
// above, we put these cases in their own method.
263
+
func weakSelfError(){
264
+
doVoidStuff({[weak self]in x +=1}) // expected-error {{explicit use of 'self' is required when 'self' is optional, to make control flow explicit}} expected-note {{reference 'self?.' explicitly}} expected-warning {{variable 'self' was written to, but never read}}
265
+
doStuff({[weak self]in x+1}) // expected-error {{explicit use of 'self' is required when 'self' is optional, to make control flow explicit}} expected-note {{reference 'self?.' explicitly}}
266
+
doVoidStuff({[weak self]in _ =method()}) // expected-error {{explicit use of 'self' is required when 'self' is optional, to make control flow explicit}} expected-note{{reference 'self?.' explicitly}}
267
+
doStuff({[weak self]inmethod()}) // expected-error {{explicit use of 'self' is required when 'self' is optional, to make control flow explicit}} expected-note{{reference 'self?.' explicitly}}
268
+
}
263
269
}
264
270
265
271
// If the implicit self is of value type, no diagnostic should be produced.
0 commit comments