Skip to content

[Async Refactoring] Add @discardableResult for defaulted completion #37882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/IDE/Refactoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4413,6 +4413,14 @@ struct AsyncHandlerParamDesc : public AsyncHandlerDesc {
OS << tok::r_paren;
}

/// Retrieves the parameter decl for the completion handler parameter, or
/// \c nullptr if no valid completion parameter is present.
const ParamDecl *getHandlerParam() const {
if (!isValid())
return nullptr;
return Func->getParameters()->get(Index);
}

bool operator==(const AsyncHandlerParamDesc &Other) const {
return Handler == Other.Handler && Type == Other.Type &&
HasError == Other.HasError && Index == Other.Index;
Expand Down Expand Up @@ -6313,6 +6321,13 @@ class AsyncConverter : private SourceEntityWalker {

void addFuncDecl(const FuncDecl *FD) {
auto *Params = FD->getParameters();
auto *HandlerParam = TopHandler.getHandlerParam();

// If the completion handler parameter has a default argument, the async
// version is effectively @discardableResult, as not all the callers care
// about receiving the completion call.
if (HandlerParam && HandlerParam->isDefaultArgument())
OS << tok::at_sign << "discardableResult" << "\n";

// First chunk: start -> the parameter to remove (if any)
SourceLoc LeftEndLoc = Params->getLParenLoc().getAdvancedLoc(1);
Expand Down
20 changes: 20 additions & 0 deletions test/refactoring/ConvertAsync/convert_function.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// RUN: %empty-directory(%t)

enum CustomError : Error {
case Bad
}
Expand Down Expand Up @@ -309,3 +311,21 @@ func rdar78693050(_ completion: () -> Void) {
// RDAR78693050-NEXT: }
// RDAR78693050-NEXT: return
// RDAR78693050-NEXT: }

// RUN: %refactor-check-compiles -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=DISCARDABLE-RESULT %s
func withDefaultedCompletion(arg: String, completion: @escaping (String) -> Void = {_ in}) {
completion(arg)
}

// DISCARDABLE-RESULT: @discardableResult
// DISCARDABLE-RESULT-NEXT: func withDefaultedCompletion(arg: String) async -> String {
// DISCARDABLE-RESULT-NEXT: return arg
// DISCARDABLE-RESULT-NEXT: }

// RUN: %refactor-check-compiles -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=DEFAULT-ARG %s
func withDefaultArg(x: String = "") {
}

// DEFAULT-ARG: convert_function.swift [[# @LINE-3]]:1 -> [[# @LINE-2]]:2
// DEFAULT-ARG-NOT: @discardableResult
// DEFAULT-ARG-NEXT: {{^}}func withDefaultArg(x: String = "") async