Skip to content

Commit bc52297

Browse files
committed
[Refactoring] Handle closures with capture list in async refactoring
Resolves rdar://74064061
1 parent 9cddf49 commit bc52297

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4991,7 +4991,12 @@ class AsyncConversionStringBuilder : private SourceEntityWalker {
49914991
DiagEngine.diagnose(CE->getStartLoc(), diag::missing_callback_arg);
49924992
return;
49934993
}
4994+
49944995
auto Callback = dyn_cast<ClosureExpr>(ArgList.ref()[HandlerDesc.Index]);
4996+
auto Capture = dyn_cast<CaptureListExpr>(ArgList.ref()[HandlerDesc.Index]);
4997+
if (Capture) {
4998+
Callback = Capture->getClosureBody();
4999+
}
49955000
if (!Callback) {
49965001
DiagEngine.diagnose(CE->getStartLoc(), diag::missing_callback_arg);
49975002
return;

test/refactoring/ConvertAsync/basic.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func noParamAutoclosure(completion: @autoclosure () -> Void) { }
171171
// 2. Check that the various ways to call a function (and the positions the
172172
// refactoring is called from) are handled correctly
173173

174-
// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefixes=CONVERT-FUNC,CALL,CALL-NOLABEL,CALL-WRAPPED,TRAILING,TRAILING-PARENS,TRAILING-WRAPPED,TRAILING-ARG %s
174+
// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefixes=CONVERT-FUNC,CALL,CALL-NOLABEL,CALL-WRAPPED,TRAILING,TRAILING-PARENS,TRAILING-WRAPPED,CALL-ARG,MANY-CALL,MEMBER-CALL,MEMBER-CALL2,MEMBER-PARENS,EMPTY-CAPTURE,CAPTURE %s
175175
func testCalls() {
176176
// CONVERT-FUNC: {{^}}func testCalls() async {
177177
// RUN: %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+4):3 | %FileCheck -check-prefix=CALL %s
@@ -269,5 +269,20 @@ func testCalls() {
269269

270270
// RUN: not %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3
271271
noParamAutoclosure(completion: print("autoclosure"))
272+
273+
// RUN: %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=EMPTY-CAPTURE %s
274+
simple { [] str in
275+
print("closure with empty capture list")
276+
}
277+
// EMPTY-CAPTURE: let str = await simple(){{$}}
278+
// EMPTY-CAPTURE-NEXT: {{^}}print("closure with empty capture list")
279+
280+
// RUN: %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+2):3 | %FileCheck -check-prefix=CAPTURE %s
281+
let anything = "anything"
282+
simple { [unowned anything] str in
283+
print("closure with capture list \(anything)")
284+
}
285+
// CAPTURE: let str = await simple(){{$}}
286+
// CAPTURE-NEXT: {{^}}print("closure with capture list \(anything)")
272287
}
273288
// CONVERT-FUNC: {{^}}}

0 commit comments

Comments
 (0)