[Refactoring] Improve Void handling for async conversion

When converting a function with a completion handler
that has a Void success parameter, e.g
`(Void?, Error?) -> Void`, or more likely a
`Result<Void, Error>` parameter, make sure to omit
the `-> Void` from the resulting async function
conversion.

In addition, strip any Void bindings from an async
function call, and any explicit Void return values
from inside the async function.

Resolves rdar://75189289
This commit is contained in:
Hamish Knight
2021-04-30 01:09:04 +01:00
parent a5b2423c6f
commit c1e4dc3d82
4 changed files with 200 additions and 50 deletions

View File

@@ -190,6 +190,18 @@ func blockConvention(completion: @convention(block) () -> Void) { }
func cConvention(completion: @convention(c) () -> Void) { }
// C-CONVENTION: func cConvention() async { }
// RUN: %refactor -add-async-alternative -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix VOID-HANDLER %s
func voidCompletion(completion: (Void) -> Void) {}
// VOID-HANDLER: func voidCompletion() async {}
// RUN: %refactor -add-async-alternative -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix VOID-AND-ERROR-HANDLER %s
func voidAndErrorCompletion(completion: (Void?, Error?) -> Void) {}
// VOID-AND-ERROR-HANDLER: func voidAndErrorCompletion() async throws {}
// RUN: %refactor -add-async-alternative -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix TOO-MUCH-VOID-AND-ERROR-HANDLER %s
func tooMuchVoidAndErrorCompletion(completion: (Void?, Void?, Error?) -> Void) {}
// TOO-MUCH-VOID-AND-ERROR-HANDLER: func tooMuchVoidAndErrorCompletion() async throws {}
// 2. Check that the various ways to call a function (and the positions the
// refactoring is called from) are handled correctly
@@ -348,5 +360,26 @@ func testCalls() {
}
// C-CONVENTION-CALL: await cConvention(){{$}}
// C-CONVENTION-CALL-NEXT: {{^}}print("cConvention")
// RUN: %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=VOID-AND-ERROR-CALL %s
voidAndErrorCompletion { v, err in
print("void and error completion \(v)")
}
// VOID-AND-ERROR-CALL: {{^}}try await voidAndErrorCompletion(){{$}}
// VOID-AND-ERROR-CALL: {{^}}print("void and error completion \(<#v#>)"){{$}}
// RUN: %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=VOID-AND-ERROR-CALL2 %s
voidAndErrorCompletion { _, err in
print("void and error completion 2")
}
// VOID-AND-ERROR-CALL2: {{^}}try await voidAndErrorCompletion(){{$}}
// VOID-AND-ERROR-CALL2: {{^}}print("void and error completion 2"){{$}}
// RUN: %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=VOID-AND-ERROR-CALL3 %s
tooMuchVoidAndErrorCompletion { v, v1, err in
print("void and error completion 3")
}
// VOID-AND-ERROR-CALL3: {{^}}try await tooMuchVoidAndErrorCompletion(){{$}}
// VOID-AND-ERROR-CALL3: {{^}}print("void and error completion 3"){{$}}
}
// CONVERT-FUNC: {{^}}}