Skip to content

[Async Refactoring] Get semantics providing expr to decide if call is to completion handler #38261

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
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
14 changes: 8 additions & 6 deletions lib/IDE/Refactoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4258,10 +4258,12 @@ struct AsyncHandlerDesc {
if (!isValid())
return nullptr;

if (Node.isExpr(swift::ExprKind::Call)) {
CallExpr *CE = cast<CallExpr>(Node.dyn_cast<Expr *>());
if (CE->getFn()->getReferencedDecl().getDecl() == getHandler())
return CE;
if (auto E = Node.dyn_cast<Expr *>()) {
if (auto *CE = dyn_cast<CallExpr>(E->getSemanticsProvidingExpr())) {
if (CE->getFn()->getReferencedDecl().getDecl() == getHandler()) {
return CE;
}
}
}
return nullptr;
}
Expand Down Expand Up @@ -6343,10 +6345,10 @@ class AsyncConverter : private SourceEntityWalker {
}
} else if (CallExpr *CE = TopHandler.getAsHandlerCall(E)) {
if (Scopes.back().isWrappedInContination()) {
return addCustom(CE->getSourceRange(),
return addCustom(E->getSourceRange(),
[&]() { addHandlerCallToContinuation(CE); });
} else if (NestedExprCount == 0) {
return addCustom(CE->getSourceRange(), [&]() { addHandlerCall(CE); });
return addCustom(E->getSourceRange(), [&]() { addHandlerCall(CE); });
}
} else if (auto *CE = dyn_cast<CallExpr>(E)) {
// Try and hoist a call's completion handler. Don't do so if
Expand Down
7 changes: 2 additions & 5 deletions test/refactoring/ConvertAsync/convert_function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,12 @@ func testReturnHandling2(completion: @escaping (String) -> ()) {
// RETURN-HANDLING2-NEXT: }
// RETURN-HANDLING2-NEXT: }

// FIXME: We should arguably be able to handle transforming this completion handler call (rdar://78011350).
// RUN: %refactor -add-async-alternative -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=RETURN-HANDLING3 %s
// RUN: %refactor-check-compiles -add-async-alternative -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=RETURN-HANDLING3 %s
func testReturnHandling3(_ completion: (String?, Error?) -> Void) {
return (completion("", nil))
}
// RETURN-HANDLING3: func testReturnHandling3() async throws -> String {
// RETURN-HANDLING3-NEXT: return try await withCheckedThrowingContinuation { continuation in
// RETURN-HANDLING3-NEXT: (continuation.resume(returning: ""))
// RETURN-HANDLING3-NEXT: }
// RETURN-HANDLING3-NEXT: {{^}} return ""{{$}}
// RETURN-HANDLING3-NEXT: }

// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=RDAR78693050 %s
Expand Down
14 changes: 14 additions & 0 deletions test/refactoring/ConvertAsync/convert_to_continuation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ func testCreateContinuation(completionHandler: (Int) -> Void) {
// CREATE-CONTINUATION-NEXT: }
// CREATE-CONTINUATION-NEXT: }

// RUN: %refactor-check-compiles -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=CREATE-CONTINUATION-HANDLER-CALL-IN-PARENS %s
func testCreateContinuationWithCompletionHandlerCallInParens(completionHandler: (Int) -> Void) {
withoutAsyncAlternativeBecauseOfMismatchedCompletionHandlerName {
(completionHandler($0))
}
}
// CREATE-CONTINUATION-HANDLER-CALL-IN-PARENS: func testCreateContinuationWithCompletionHandlerCallInParens() async -> Int {
// CREATE-CONTINUATION-HANDLER-CALL-IN-PARENS-NEXT: return await withCheckedContinuation { continuation in
// CREATE-CONTINUATION-HANDLER-CALL-IN-PARENS-NEXT: withoutAsyncAlternativeBecauseOfMismatchedCompletionHandlerName {
// CREATE-CONTINUATION-HANDLER-CALL-IN-PARENS-NEXT: continuation.resume(returning: $0)
// CREATE-CONTINUATION-HANDLER-CALL-IN-PARENS-NEXT: }
// CREATE-CONTINUATION-HANDLER-CALL-IN-PARENS-NEXT: }
// CREATE-CONTINUATION-HANDLER-CALL-IN-PARENS-NEXT: }

// RUN: %refactor-check-compiles -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=CREATE-CONTINUATION-BECAUSE-RETURN-VALUE %s
func testCreateContinuationBecauseOfReturnValue(completionHandler: (Int) -> Void) {
_ = withoutAsyncAlternativeBecauseOfReturnValue {
Expand Down