Skip to content

Commit 0eeaec1

Browse files
authored
Merge pull request #37882 from hamishknight/a-non-complete-agreement
[Async Refactoring] Add @discardableResult for defaulted completion
2 parents 88cf178 + e97c781 commit 0eeaec1

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4413,6 +4413,14 @@ struct AsyncHandlerParamDesc : public AsyncHandlerDesc {
44134413
OS << tok::r_paren;
44144414
}
44154415

4416+
/// Retrieves the parameter decl for the completion handler parameter, or
4417+
/// \c nullptr if no valid completion parameter is present.
4418+
const ParamDecl *getHandlerParam() const {
4419+
if (!isValid())
4420+
return nullptr;
4421+
return Func->getParameters()->get(Index);
4422+
}
4423+
44164424
bool operator==(const AsyncHandlerParamDesc &Other) const {
44174425
return Handler == Other.Handler && Type == Other.Type &&
44184426
HasError == Other.HasError && Index == Other.Index;
@@ -6313,6 +6321,13 @@ class AsyncConverter : private SourceEntityWalker {
63136321

63146322
void addFuncDecl(const FuncDecl *FD) {
63156323
auto *Params = FD->getParameters();
6324+
auto *HandlerParam = TopHandler.getHandlerParam();
6325+
6326+
// If the completion handler parameter has a default argument, the async
6327+
// version is effectively @discardableResult, as not all the callers care
6328+
// about receiving the completion call.
6329+
if (HandlerParam && HandlerParam->isDefaultArgument())
6330+
OS << tok::at_sign << "discardableResult" << "\n";
63166331

63176332
// First chunk: start -> the parameter to remove (if any)
63186333
SourceLoc LeftEndLoc = Params->getLParenLoc().getAdvancedLoc(1);

test/refactoring/ConvertAsync/convert_function.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// RUN: %empty-directory(%t)
2+
13
enum CustomError : Error {
24
case Bad
35
}
@@ -309,3 +311,21 @@ func rdar78693050(_ completion: () -> Void) {
309311
// RDAR78693050-NEXT: }
310312
// RDAR78693050-NEXT: return
311313
// RDAR78693050-NEXT: }
314+
315+
// RUN: %refactor-check-compiles -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=DISCARDABLE-RESULT %s
316+
func withDefaultedCompletion(arg: String, completion: @escaping (String) -> Void = {_ in}) {
317+
completion(arg)
318+
}
319+
320+
// DISCARDABLE-RESULT: @discardableResult
321+
// DISCARDABLE-RESULT-NEXT: func withDefaultedCompletion(arg: String) async -> String {
322+
// DISCARDABLE-RESULT-NEXT: return arg
323+
// DISCARDABLE-RESULT-NEXT: }
324+
325+
// RUN: %refactor-check-compiles -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=DEFAULT-ARG %s
326+
func withDefaultArg(x: String = "") {
327+
}
328+
329+
// DEFAULT-ARG: convert_function.swift [[# @LINE-3]]:1 -> [[# @LINE-2]]:2
330+
// DEFAULT-ARG-NOT: @discardableResult
331+
// DEFAULT-ARG-NEXT: {{^}}func withDefaultArg(x: String = "") async

0 commit comments

Comments
 (0)