1818#include " swift/AST/Comment.h"
1919#include " swift/AST/Decl.h"
2020#include " swift/AST/ExistentialLayout.h"
21+ #include " swift/AST/ForeignAsyncConvention.h"
2122#include " swift/AST/ForeignErrorConvention.h"
2223#include " swift/AST/GenericEnvironment.h"
2324#include " swift/AST/PrettyStackTrace.h"
@@ -464,6 +465,7 @@ class DeclAndTypePrinter::Implementation
464465
465466 Type getForeignResultType (AbstractFunctionDecl *AFD,
466467 FunctionType *methodTy,
468+ Optional<ForeignAsyncConvention> asyncConvention,
467469 Optional<ForeignErrorConvention> errorConvention) {
468470 // A foreign error convention can affect the result type as seen in
469471 // Objective-C.
@@ -484,6 +486,11 @@ class DeclAndTypePrinter::Implementation
484486 }
485487 }
486488
489+ // Asynchronous methods return their results via completion handler.
490+ if (asyncConvention) {
491+ return getASTContext ().TheEmptyTupleType ;
492+ }
493+
487494 auto result = methodTy->getResult ();
488495 if (result->isUninhabited ())
489496 return getASTContext ().TheEmptyTupleType ;
@@ -513,16 +520,20 @@ class DeclAndTypePrinter::Implementation
513520 }
514521 }
515522
523+ Optional<ForeignAsyncConvention> asyncConvention
524+ = AFD->getForeignAsyncConvention ();
516525 Optional<ForeignErrorConvention> errorConvention
517526 = AFD->getForeignErrorConvention ();
518527 Type rawMethodTy = AFD->getMethodInterfaceType ();
519528 auto methodTy = rawMethodTy->castTo <FunctionType>();
520- auto resultTy = getForeignResultType (AFD, methodTy, errorConvention);
529+ auto resultTy = getForeignResultType (
530+ AFD, methodTy, asyncConvention, errorConvention);
521531
522532 // Constructors and methods returning DynamicSelf return
523533 // instancetype.
524534 if (isa<ConstructorDecl>(AFD) ||
525- (isa<FuncDecl>(AFD) && cast<FuncDecl>(AFD)->hasDynamicSelfResult ())) {
535+ (isa<FuncDecl>(AFD) && cast<FuncDecl>(AFD)->hasDynamicSelfResult () &&
536+ !AFD->hasAsync ())) {
526537 if (errorConvention && errorConvention->stripsResultOptionality ()) {
527538 printNullability (OTK_Optional, NullabilityPrintKind::ContextSensitive);
528539 } else if (auto ctor = dyn_cast<ConstructorDecl>(AFD)) {
@@ -578,6 +589,16 @@ class DeclAndTypePrinter::Implementation
578589 StringRef piece = selectorPieces[i].empty () ? StringRef (" " )
579590 : selectorPieces[i].str ();
580591
592+ // If we have an async convention and this is the completion handler
593+ // parameter, print it.
594+ if (asyncConvention &&
595+ i == asyncConvention->completionHandlerParamIndex ()) {
596+ os << piece << " :(" ;
597+ print (asyncConvention->completionHandlerType (), None);
598+ os << " )completionHandler" ;
599+ continue ;
600+ }
601+
581602 // If we have an error convention and this is the error
582603 // parameter, print it.
583604 if (errorConvention && i == errorConvention->getErrorParameterIndex ()) {
@@ -660,7 +681,9 @@ class DeclAndTypePrinter::Implementation
660681 if (looksLikeInitMethod (AFD->getObjCSelector ())) {
661682 os << " SWIFT_METHOD_FAMILY(none)" ;
662683 }
663- if (methodTy->getResult ()->isUninhabited ()) {
684+ if (asyncConvention) {
685+ // Async methods don't have result types to annotate.
686+ } else if (methodTy->getResult ()->isUninhabited ()) {
664687 os << " SWIFT_NORETURN" ;
665688 } else if (!methodTy->getResult ()->isVoid () &&
666689 !AFD->getAttrs ().hasAttribute <DiscardableResultAttr>()) {
@@ -697,12 +720,15 @@ class DeclAndTypePrinter::Implementation
697720
698721 void printAbstractFunctionAsFunction (FuncDecl *FD) {
699722 printDocumentationComment (FD);
723+ Optional<ForeignAsyncConvention> asyncConvention
724+ = FD->getForeignAsyncConvention ();
700725 Optional<ForeignErrorConvention> errorConvention
701726 = FD->getForeignErrorConvention ();
702727 assert (!FD->getGenericSignature () &&
703728 " top-level generic functions not supported here" );
704729 auto funcTy = FD->getInterfaceType ()->castTo <FunctionType>();
705- auto resultTy = getForeignResultType (FD, funcTy, errorConvention);
730+ auto resultTy = getForeignResultType (
731+ FD, funcTy, asyncConvention, errorConvention);
706732
707733 // The result type may be a partial function type we need to close
708734 // up later.
0 commit comments