Skip to content

Commit 061a8f4

Browse files
authored
Merge pull request swiftlang#38855 from apple/async-refactoring-void-result
[Refactoring] Fix invalid legacy body for Result<Void, ...> handler
2 parents e9e9bf0 + 9fc758a commit 061a8f4

File tree

3 files changed

+85
-53
lines changed

3 files changed

+85
-53
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7854,8 +7854,13 @@ class AsyncConverter : private SourceEntityWalker {
78547854
}
78557855
case HandlerType::RESULT: {
78567856
if (!ResultName.empty()) {
7857-
OS << tok::period_prefix << "success" << tok::l_paren << ResultName
7858-
<< tok::r_paren;
7857+
OS << tok::period_prefix << "success" << tok::l_paren;
7858+
if (!HandlerDesc.willAsyncReturnVoid()) {
7859+
OS << ResultName;
7860+
} else {
7861+
OS << tok::l_paren << tok::r_paren;
7862+
}
7863+
OS << tok::r_paren;
78597864
} else {
78607865
OS << tok::period_prefix << "failure" << tok::l_paren;
78617866
addForwardedErrorArgument("error", HandlerDesc);

test/refactoring/ConvertAsync/basic.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,21 @@ func voidAndErrorCompletion(completion: @escaping (Void, Error?) -> Void) {}
475475
// VOID-AND-ERROR-HANDLER-NEXT: }
476476
// VOID-AND-ERROR-HANDLER: func voidAndErrorCompletion() async throws {}
477477

478+
// RUN: %refactor-check-compiles -add-async-alternative -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix VOID-RESULT-HANDLER %s
479+
func voidResultCompletion(completion: @escaping (Result<Void, Error>) -> Void) {}
480+
// VOID-RESULT-HANDLER: {
481+
// VOID-RESULT-HANDLER-NEXT: Task {
482+
// VOID-RESULT-HANDLER-NEXT: do {
483+
// VOID-RESULT-HANDLER-NEXT: try await voidResultCompletion()
484+
// VOID-RESULT-HANDLER-NEXT: completion(.success(()))
485+
// VOID-RESULT-HANDLER-NEXT: } catch {
486+
// VOID-RESULT-HANDLER-NEXT: completion(.failure(error))
487+
// VOID-RESULT-HANDLER-NEXT: }
488+
// VOID-RESULT-HANDLER-NEXT: }
489+
// VOID-RESULT-HANDLER-NEXT: }
490+
// VOID-RESULT-HANDLER: func voidResultCompletion() async throws {
491+
492+
478493
// 2. Check that the various ways to call a function (and the positions the
479494
// refactoring is called from) are handled correctly
480495

0 commit comments

Comments
 (0)