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 {
4413
4413
OS << tok::r_paren;
4414
4414
}
4415
4415
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
+
4416
4424
bool operator ==(const AsyncHandlerParamDesc &Other) const {
4417
4425
return Handler == Other.Handler && Type == Other.Type &&
4418
4426
HasError == Other.HasError && Index == Other.Index ;
@@ -6313,6 +6321,13 @@ class AsyncConverter : private SourceEntityWalker {
6313
6321
6314
6322
void addFuncDecl (const FuncDecl *FD) {
6315
6323
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 " ;
6316
6331
6317
6332
// First chunk: start -> the parameter to remove (if any)
6318
6333
SourceLoc LeftEndLoc = Params->getLParenLoc ().getAdvancedLoc (1 );
Original file line number Diff line number Diff line change
1
+ // RUN: %empty-directory(%t)
2
+
1
3
enum CustomError : Error {
2
4
case Bad
3
5
}
@@ -309,3 +311,21 @@ func rdar78693050(_ completion: () -> Void) {
309
311
// RDAR78693050-NEXT: }
310
312
// RDAR78693050-NEXT: return
311
313
// 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