File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
test/refactoring/ConvertAsync Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff 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 );
Original file line number Diff line number Diff line change 1+ // RUN: %empty-directory(%t)
2+
13enum 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
You can’t perform that action at this time.
0 commit comments