Skip to content

Commit f7c7599

Browse files
committed
[Async Refactoring] Add return keyword if wrapping ReturnStmt is implicit
Previously, in the following case we were failing to add a `return` keyword inside `withImplicitReturn`. ``` func withImplicitReturn(completionHandler: (String) -> Void) { simple { completionHandler($0) } } ``` This is because the call of `completionHandler($0)` is wrapped by an implicit `ReturnStmt` and thus we assumed that there was already a `return` keyword present. Fix this issue by checking if the wrapping `ReturnStmt` is implicit and if it is, add the `return` keyword. Fixes rdar://80009760
1 parent 4e4752a commit f7c7599

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6460,7 +6460,8 @@ class AsyncConverter : private SourceEntityWalker {
64606460
// for the completion handler call, e.g 'return completion(args...)'. In
64616461
// that case, be sure not to add another return.
64626462
auto *parent = getWalker().Parent.getAsStmt();
6463-
AddedReturnOrThrow = !(parent && isa<ReturnStmt>(parent));
6463+
AddedReturnOrThrow = !(parent && isa<ReturnStmt>(parent) &&
6464+
!cast<ReturnStmt>(parent)->isImplicit());
64646465
if (AddedReturnOrThrow)
64656466
OS << tok::kw_return;
64666467
} else {

test/refactoring/ConvertAsync/convert_function.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,14 @@ func withDefaultArg(x: String = "") {
329329
// DEFAULT-ARG: convert_function.swift [[# @LINE-3]]:1 -> [[# @LINE-2]]:2
330330
// DEFAULT-ARG-NOT: @discardableResult
331331
// DEFAULT-ARG-NEXT: {{^}}func withDefaultArg(x: String = "") async
332+
333+
// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=IMPLICIT-RETURN %s
334+
func withImplicitReturn(completionHandler: (String) -> Void) {
335+
simple {
336+
completionHandler($0)
337+
}
338+
}
339+
// IMPLICIT-RETURN: func withImplicitReturn() async -> String {
340+
// IMPLICIT-RETURN-NEXT: let val0 = await simple()
341+
// IMPLICIT-RETURN-NEXT: return val0
342+
// IMPLICIT-RETURN-NEXT: }

0 commit comments

Comments
 (0)