Skip to content

Commit 1d570fc

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 d3cf72b commit 1d570fc

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

66736673
// Third chunk: add in async and throws if necessary
6674-
OS << " async";
6674+
if (!FD->hasAsync())
6675+
OS << " async";
66756676
if (FD->hasThrows() || TopHandler.HasError)
66766677
// TODO: Add throws if converting a function and it has a converted call
66776678
// 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)