Skip to content

Commit 7b19c9f

Browse files
committed
[Refactoring] Fix invalid legacy body for Result<Void, ...> handler
When adding an async alternative for a function with a `(Result<Void, ...>) -> Void` completion handler, the legacy body was adding a call to the handler with `.success(result)`. We don't assign to `result` for `Void` handlers, so pass `()` instead. Resolves rdar://81829392
1 parent 4fef4b6 commit 7b19c9f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7864,8 +7864,13 @@ class AsyncConverter : private SourceEntityWalker {
78647864
}
78657865
case HandlerType::RESULT: {
78667866
if (!ResultName.empty()) {
7867-
OS << tok::period_prefix << "success" << tok::l_paren << ResultName
7868-
<< tok::r_paren;
7867+
OS << tok::period_prefix << "success" << tok::l_paren;
7868+
if (!HandlerDesc.willAsyncReturnVoid()) {
7869+
OS << ResultName;
7870+
} else {
7871+
OS << tok::l_paren << tok::r_paren;
7872+
}
7873+
OS << tok::r_paren;
78697874
} else {
78707875
OS << tok::period_prefix << "failure" << tok::l_paren;
78717876
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)