Skip to content

Commit 86c47e4

Browse files
committed
[Refactoring] Do not add "async" if function is already async
Convert Function to Async is available on an async function. It could be useful to run this refactoring still, as it would attempt to convert any completion-handler functions to their async alternatives. Keep allowing this, but make sure not to re-add "async" to the function declaration. Resolves rdar://82156720
1 parent 0daa9cc commit 86c47e4

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
@@ -6655,7 +6655,8 @@ class AsyncConverter : private SourceEntityWalker {
66556655
addRange(MidStartLoc, MidEndLoc);
66566656

66576657
// Third chunk: add in async and throws if necessary
6658-
OS << " async";
6658+
if (!FD->hasAsync())
6659+
OS << " async";
66596660
if (FD->hasThrows() || TopHandler.HasError)
66606661
// TODO: Add throws if converting a function and it has a converted call
66616662
// without a do/catch

test/refactoring/ConvertAsync/convert_function.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ func simpleErr(arg: String) async throws -> String { }
2121
func simpleRes(arg: String, _ completion: @escaping (Result<String, Error>) -> Void) { }
2222
func simpleRes(arg: String) async throws -> String { }
2323

24+
// RUN: %refactor-check-compiles -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=ALREADY-ASYNC %s
25+
func alreadyAsync() async {
26+
simple {
27+
print($0)
28+
}
29+
}
30+
// ALREADY-ASYNC: func alreadyAsync() async {
31+
// ALREADY-ASYNC-NEXT: let val0 = await simple()
32+
// ALREADY-ASYNC-NEXT: print(val0)
33+
// ALREADY-ASYNC-NEXT: }
34+
2435
// RUN: %refactor-check-compiles -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=NESTED %s
2536
func nested() {
2637
simple {

0 commit comments

Comments
 (0)