@@ -7596,17 +7596,32 @@ class AsyncConverter : private SourceEntityWalker {
7596
7596
OS << tok::r_paren;
7597
7597
}
7598
7598
7599
- // / If the error type of \p HandlerDesc is more specialized than \c Error,
7600
- // / adds an 'as! CustomError' cast to the more specialized error type to the
7601
- // / output stream.
7602
- void
7603
- addCastToCustomErrorTypeIfNecessary (const AsyncHandlerDesc &HandlerDesc) {
7604
- const ASTContext &Ctx = HandlerDesc.getHandler ()->getASTContext ();
7599
+ // / Adds a forwarded error argument to a completion handler call. If the error
7600
+ // / type of \p HandlerDesc is more specialized than \c Error, an
7601
+ // / 'as! CustomError' cast to the more specialized error type will be added to
7602
+ // / the output stream.
7603
+ void addForwardedErrorArgument (StringRef ErrorName,
7604
+ const AsyncHandlerDesc &HandlerDesc) {
7605
+ // If the error type is already Error, we can pass it as-is.
7605
7606
auto ErrorType = *HandlerDesc.getErrorType ();
7606
- if (ErrorType->getCanonicalType () != Ctx .getExceptionType ()) {
7607
- OS << " " << tok::kw_as << tok::exclaim_postfix << " " ;
7608
- ErrorType-> lookThroughSingleOptionalType ()-> print (OS) ;
7607
+ if (ErrorType->getCanonicalType () == getASTContext () .getExceptionType ()) {
7608
+ OS << ErrorName ;
7609
+ return ;
7609
7610
}
7611
+
7612
+ // Otherwise we need to add a force cast to the destination custom error
7613
+ // type. If this is for an Error? parameter, we'll need to add parens around
7614
+ // the cast to silence a compiler warning about force casting never
7615
+ // producing nil.
7616
+ auto RequiresParens = HandlerDesc.getErrorParam ().hasValue ();
7617
+ if (RequiresParens)
7618
+ OS << tok::l_paren;
7619
+
7620
+ OS << ErrorName << " " << tok::kw_as << tok::exclaim_postfix << " " ;
7621
+ ErrorType->lookThroughSingleOptionalType ()->print (OS);
7622
+
7623
+ if (RequiresParens)
7624
+ OS << tok::r_paren;
7610
7625
}
7611
7626
7612
7627
// / If \p T has a natural default value like \c nil for \c Optional or \c ()
@@ -7637,8 +7652,7 @@ class AsyncConverter : private SourceEntityWalker {
7637
7652
if (HandlerDesc.HasError && Index == HandlerDesc.params ().size () - 1 ) {
7638
7653
// The error parameter is the last argument of the completion handler.
7639
7654
if (ResultName.empty ()) {
7640
- OS << " error" ;
7641
- addCastToCustomErrorTypeIfNecessary (HandlerDesc);
7655
+ addForwardedErrorArgument (" error" , HandlerDesc);
7642
7656
} else {
7643
7657
addDefaultValueOrPlaceholder (HandlerDesc.params ()[Index].getPlainType ());
7644
7658
}
@@ -7707,8 +7721,8 @@ class AsyncConverter : private SourceEntityWalker {
7707
7721
OS << tok::period_prefix << " success" << tok::l_paren << ResultName
7708
7722
<< tok::r_paren;
7709
7723
} else {
7710
- OS << tok::period_prefix << " failure" << tok::l_paren << " error " ;
7711
- addCastToCustomErrorTypeIfNecessary ( HandlerDesc);
7724
+ OS << tok::period_prefix << " failure" << tok::l_paren;
7725
+ addForwardedErrorArgument ( " error " , HandlerDesc);
7712
7726
OS << tok::r_paren;
7713
7727
}
7714
7728
break ;
0 commit comments