Skip to content

[Refactoring] Convert completion handler when converting function to async #37110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/IDE/Refactoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5371,8 +5371,8 @@ bool RefactoringActionConvertToAsync::performChange() {
assert(FD &&
"Should not run performChange when refactoring is not applicable");

AsyncHandlerDesc TempDesc;
AsyncConverter Converter(SM, DiagEngine, FD, TempDesc);
auto HandlerDesc = AsyncHandlerDesc::find(FD, /*ignoreName=*/true);
AsyncConverter Converter(SM, DiagEngine, FD, HandlerDesc);
if (!Converter.convert())
return true;

Expand Down
7 changes: 1 addition & 6 deletions test/refactoring/ConvertAsync/basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,9 @@ func retStruct() -> MyStruct { return MyStruct() }

protocol MyProtocol {
// RUN: %refactor -add-async-alternative -dump-text -source-filename %s -pos=%(line+2):3 | %FileCheck -check-prefix=PROTO-MEMBER %s
// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=PROTO-MEMBER-TO-ASYNC %s
// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=PROTO-MEMBER %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test that checks that it does actually convert and not add anywhere? See the top of this file for an example (it's for add-async, convert should have an actual range rather than a 0 length one).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think so. I’ll add one in a follow-up PR.

func protoMember(completion: (String) -> Void)
// PROTO-MEMBER: func protoMember() async -> String{{$}}

// FIXME: The current async refactoring only refactors the client side and thus only adds the 'async' keyword.
// We should be refactoring the entire method signature here and removing the completion parameter.
// This test currently checks that we are not crashing.
// PROTO-MEMBER-TO-ASYNC: func protoMember(completion: (String) -> Void) async
}

// RUN: not %refactor -add-async-alternative -dump-text -source-filename %s -pos=%(line+1):1
Expand Down
6 changes: 6 additions & 0 deletions test/refactoring/ConvertAsync/convert_function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func manyNested() {
// MANY-NESTED-NEXT: print("after")
// MANY-NESTED-NEXT: }

// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+2):1 | %FileCheck -check-prefix=ASYNC-SIMPLE %s
// RUN: %refactor -add-async-alternative -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=ASYNC-SIMPLE %s
func asyncParams(arg: String, _ completion: (String?, Error?) -> Void) {
simpleErr(arg: arg) { str, err in
Expand Down Expand Up @@ -196,3 +197,8 @@ func voidResultCompletion(completion: (Result<Void, Error>) -> Void) {
// VOID-RESULT-HANDLER-NEXT: throw CustomError.Bad
// VOID-RESULT-HANDLER-NEXT: }
// VOID-RESULT-HANDLER-NEXT: }

// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+2):1 | %FileCheck -check-prefix=NON-COMPLETION-HANDLER %s
// RUN: %refactor -add-async-alternative -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=NON-COMPLETION-HANDLER %s
func functionWithSomeHandler(handler: (String) -> Void) {}
// NON-COMPLETION-HANDLER: func functionWithSomeHandler() async -> String {}