Skip to content

Commit ea26618

Browse files
committed
[Refactoring] Convert completion handler when converting function to async
Convert function to async currently only adds "async" to the function and runs the convert call refactoring on the body. This was intentional, but it turns out to be somewhat confusing. Instead, run the same refactoring as the add async alternative refactoring but just replace rather than add. Resolves rdar://77103049
1 parent e4498b2 commit ea26618

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5379,8 +5379,8 @@ bool RefactoringActionConvertToAsync::performChange() {
53795379
assert(FD &&
53805380
"Should not run performChange when refactoring is not applicable");
53815381

5382-
AsyncHandlerDesc TempDesc;
5383-
AsyncConverter Converter(SM, DiagEngine, FD, TempDesc);
5382+
auto HandlerDesc = AsyncHandlerDesc::find(FD, /*ignoreName=*/true);
5383+
AsyncConverter Converter(SM, DiagEngine, FD, HandlerDesc);
53845384
if (!Converter.convert())
53855385
return true;
53865386

test/refactoring/ConvertAsync/basic.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,9 @@ func retStruct() -> MyStruct { return MyStruct() }
151151

152152
protocol MyProtocol {
153153
// RUN: %refactor -add-async-alternative -dump-text -source-filename %s -pos=%(line+2):3 | %FileCheck -check-prefix=PROTO-MEMBER %s
154-
// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=PROTO-MEMBER-TO-ASYNC %s
154+
// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=PROTO-MEMBER %s
155155
func protoMember(completion: (String) -> Void)
156156
// PROTO-MEMBER: func protoMember() async -> String{{$}}
157-
158-
// FIXME: The current async refactoring only refactors the client side and thus only adds the 'async' keyword.
159-
// We should be refactoring the entire method signature here and removing the completion parameter.
160-
// This test currently checks that we are not crashing.
161-
// PROTO-MEMBER-TO-ASYNC: func protoMember(completion: (String) -> Void) async
162157
}
163158

164159
// RUN: not %refactor -add-async-alternative -dump-text -source-filename %s -pos=%(line+1):1

test/refactoring/ConvertAsync/convert_function.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func manyNested() {
7171
// MANY-NESTED-NEXT: print("after")
7272
// MANY-NESTED-NEXT: }
7373

74+
// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+2):1 | %FileCheck -check-prefix=ASYNC-SIMPLE %s
7475
// RUN: %refactor -add-async-alternative -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=ASYNC-SIMPLE %s
7576
func asyncParams(arg: String, _ completion: (String?, Error?) -> Void) {
7677
simpleErr(arg: arg) { str, err in
@@ -196,3 +197,8 @@ func voidResultCompletion(completion: (Result<Void, Error>) -> Void) {
196197
// VOID-RESULT-HANDLER-NEXT: throw CustomError.Bad
197198
// VOID-RESULT-HANDLER-NEXT: }
198199
// VOID-RESULT-HANDLER-NEXT: }
200+
201+
// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+2):1 | %FileCheck -check-prefix=NON-COMPLETION-HANDLER %s
202+
// RUN: %refactor -add-async-alternative -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=NON-COMPLETION-HANDLER %s
203+
func functionWithSomeHandler(handler: (String) -> Void) {}
204+
// NON-COMPLETION-HANDLER: func functionWithSomeHandler() async -> String {}

0 commit comments

Comments
 (0)