18
18
#include " swift/AST/Comment.h"
19
19
#include " swift/AST/Decl.h"
20
20
#include " swift/AST/ExistentialLayout.h"
21
+ #include " swift/AST/ForeignAsyncConvention.h"
21
22
#include " swift/AST/ForeignErrorConvention.h"
22
23
#include " swift/AST/GenericEnvironment.h"
23
24
#include " swift/AST/PrettyStackTrace.h"
@@ -464,6 +465,7 @@ class DeclAndTypePrinter::Implementation
464
465
465
466
Type getForeignResultType (AbstractFunctionDecl *AFD,
466
467
FunctionType *methodTy,
468
+ Optional<ForeignAsyncConvention> asyncConvention,
467
469
Optional<ForeignErrorConvention> errorConvention) {
468
470
// A foreign error convention can affect the result type as seen in
469
471
// Objective-C.
@@ -484,6 +486,11 @@ class DeclAndTypePrinter::Implementation
484
486
}
485
487
}
486
488
489
+ // Asynchronous methods return their results via completion handler.
490
+ if (asyncConvention) {
491
+ return getASTContext ().TheEmptyTupleType ;
492
+ }
493
+
487
494
auto result = methodTy->getResult ();
488
495
if (result->isUninhabited ())
489
496
return getASTContext ().TheEmptyTupleType ;
@@ -513,16 +520,20 @@ class DeclAndTypePrinter::Implementation
513
520
}
514
521
}
515
522
523
+ Optional<ForeignAsyncConvention> asyncConvention
524
+ = AFD->getForeignAsyncConvention ();
516
525
Optional<ForeignErrorConvention> errorConvention
517
526
= AFD->getForeignErrorConvention ();
518
527
Type rawMethodTy = AFD->getMethodInterfaceType ();
519
528
auto methodTy = rawMethodTy->castTo <FunctionType>();
520
- auto resultTy = getForeignResultType (AFD, methodTy, errorConvention);
529
+ auto resultTy = getForeignResultType (
530
+ AFD, methodTy, asyncConvention, errorConvention);
521
531
522
532
// Constructors and methods returning DynamicSelf return
523
533
// instancetype.
524
534
if (isa<ConstructorDecl>(AFD) ||
525
- (isa<FuncDecl>(AFD) && cast<FuncDecl>(AFD)->hasDynamicSelfResult ())) {
535
+ (isa<FuncDecl>(AFD) && cast<FuncDecl>(AFD)->hasDynamicSelfResult () &&
536
+ !AFD->hasAsync ())) {
526
537
if (errorConvention && errorConvention->stripsResultOptionality ()) {
527
538
printNullability (OTK_Optional, NullabilityPrintKind::ContextSensitive);
528
539
} else if (auto ctor = dyn_cast<ConstructorDecl>(AFD)) {
@@ -578,6 +589,16 @@ class DeclAndTypePrinter::Implementation
578
589
StringRef piece = selectorPieces[i].empty () ? StringRef (" " )
579
590
: selectorPieces[i].str ();
580
591
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
+
581
602
// If we have an error convention and this is the error
582
603
// parameter, print it.
583
604
if (errorConvention && i == errorConvention->getErrorParameterIndex ()) {
@@ -660,7 +681,9 @@ class DeclAndTypePrinter::Implementation
660
681
if (looksLikeInitMethod (AFD->getObjCSelector ())) {
661
682
os << " SWIFT_METHOD_FAMILY(none)" ;
662
683
}
663
- if (methodTy->getResult ()->isUninhabited ()) {
684
+ if (asyncConvention) {
685
+ // Async methods don't have result types to annotate.
686
+ } else if (methodTy->getResult ()->isUninhabited ()) {
664
687
os << " SWIFT_NORETURN" ;
665
688
} else if (!methodTy->getResult ()->isVoid () &&
666
689
!AFD->getAttrs ().hasAttribute <DiscardableResultAttr>()) {
@@ -697,12 +720,15 @@ class DeclAndTypePrinter::Implementation
697
720
698
721
void printAbstractFunctionAsFunction (FuncDecl *FD) {
699
722
printDocumentationComment (FD);
723
+ Optional<ForeignAsyncConvention> asyncConvention
724
+ = FD->getForeignAsyncConvention ();
700
725
Optional<ForeignErrorConvention> errorConvention
701
726
= FD->getForeignErrorConvention ();
702
727
assert (!FD->getGenericSignature () &&
703
728
" top-level generic functions not supported here" );
704
729
auto funcTy = FD->getInterfaceType ()->castTo <FunctionType>();
705
- auto resultTy = getForeignResultType (FD, funcTy, errorConvention);
730
+ auto resultTy = getForeignResultType (
731
+ FD, funcTy, asyncConvention, errorConvention);
706
732
707
733
// The result type may be a partial function type we need to close
708
734
// up later.
0 commit comments