Skip to content

Commit 20a4c14

Browse files
authored
Merge pull request #38865 from apple/cherry-rdar81829392
[5.5][Refactoring] Fix invalid legacy body for Result<Void, ...> handler
2 parents 3621dfe + 7ffe07b commit 20a4c14

File tree

3 files changed

+97
-53
lines changed

3 files changed

+97
-53
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6906,7 +6906,8 @@ class AsyncConverter : private SourceEntityWalker {
69066906
// Synthesize the force unwrap so that we get the expected results.
69076907
if (TopHandler.getHandlerType() == HandlerType::PARAMS &&
69086908
TopHandler.HasError) {
6909-
if (auto DRE = dyn_cast<DeclRefExpr>(Elt)) {
6909+
if (auto DRE =
6910+
dyn_cast<DeclRefExpr>(Elt->getSemanticsProvidingExpr())) {
69106911
auto D = DRE->getDecl();
69116912
if (Unwraps.count(D)) {
69126913
Elt = new (getASTContext()) ForceValueExpr(Elt, SourceLoc());
@@ -7869,8 +7870,13 @@ class AsyncConverter : private SourceEntityWalker {
78697870
}
78707871
case HandlerType::RESULT: {
78717872
if (!ResultName.empty()) {
7872-
OS << tok::period_prefix << "success" << tok::l_paren << ResultName
7873-
<< tok::r_paren;
7873+
OS << tok::period_prefix << "success" << tok::l_paren;
7874+
if (!HandlerDesc.willAsyncReturnVoid()) {
7875+
OS << ResultName;
7876+
} else {
7877+
OS << tok::l_paren << tok::r_paren;
7878+
}
7879+
OS << tok::r_paren;
78747880
} else {
78757881
OS << tok::period_prefix << "failure" << tok::l_paren;
78767882
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)