@@ -7612,17 +7612,32 @@ class AsyncConverter : private SourceEntityWalker {
7612
7612
OS << tok::r_paren;
7613
7613
}
7614
7614
7615
- // / If the error type of \p HandlerDesc is more specialized than \c Error,
7616
- // / adds an 'as! CustomError' cast to the more specialized error type to the
7617
- // / output stream.
7618
- void
7619
- addCastToCustomErrorTypeIfNecessary (const AsyncHandlerDesc &HandlerDesc) {
7620
- const ASTContext &Ctx = HandlerDesc.getHandler ()->getASTContext ();
7615
+ // / Adds a forwarded error argument to a completion handler call. If the error
7616
+ // / type of \p HandlerDesc is more specialized than \c Error, an
7617
+ // / 'as! CustomError' cast to the more specialized error type will be added to
7618
+ // / the output stream.
7619
+ void addForwardedErrorArgument (StringRef ErrorName,
7620
+ const AsyncHandlerDesc &HandlerDesc) {
7621
+ // If the error type is already Error, we can pass it as-is.
7621
7622
auto ErrorType = *HandlerDesc.getErrorType ();
7622
- if (ErrorType->getCanonicalType () != Ctx .getExceptionType ()) {
7623
- OS << " " << tok::kw_as << tok::exclaim_postfix << " " ;
7624
- ErrorType-> lookThroughSingleOptionalType ()-> print (OS) ;
7623
+ if (ErrorType->getCanonicalType () == getASTContext () .getExceptionType ()) {
7624
+ OS << ErrorName ;
7625
+ return ;
7625
7626
}
7627
+
7628
+ // Otherwise we need to add a force cast to the destination custom error
7629
+ // type. If this is for an Error? parameter, we'll need to add parens around
7630
+ // the cast to silence a compiler warning about force casting never
7631
+ // producing nil.
7632
+ auto RequiresParens = HandlerDesc.getErrorParam ().hasValue ();
7633
+ if (RequiresParens)
7634
+ OS << tok::l_paren;
7635
+
7636
+ OS << ErrorName << " " << tok::kw_as << tok::exclaim_postfix << " " ;
7637
+ ErrorType->lookThroughSingleOptionalType ()->print (OS);
7638
+
7639
+ if (RequiresParens)
7640
+ OS << tok::r_paren;
7626
7641
}
7627
7642
7628
7643
// / If \p T has a natural default value like \c nil for \c Optional or \c ()
@@ -7653,8 +7668,7 @@ class AsyncConverter : private SourceEntityWalker {
7653
7668
if (HandlerDesc.HasError && Index == HandlerDesc.params ().size () - 1 ) {
7654
7669
// The error parameter is the last argument of the completion handler.
7655
7670
if (ResultName.empty ()) {
7656
- OS << " error" ;
7657
- addCastToCustomErrorTypeIfNecessary (HandlerDesc);
7671
+ addForwardedErrorArgument (" error" , HandlerDesc);
7658
7672
} else {
7659
7673
addDefaultValueOrPlaceholder (HandlerDesc.params ()[Index].getPlainType ());
7660
7674
}
@@ -7723,8 +7737,8 @@ class AsyncConverter : private SourceEntityWalker {
7723
7737
OS << tok::period_prefix << " success" << tok::l_paren << ResultName
7724
7738
<< tok::r_paren;
7725
7739
} else {
7726
- OS << tok::period_prefix << " failure" << tok::l_paren << " error " ;
7727
- addCastToCustomErrorTypeIfNecessary ( HandlerDesc);
7740
+ OS << tok::period_prefix << " failure" << tok::l_paren;
7741
+ addForwardedErrorArgument ( " error " , HandlerDesc);
7728
7742
OS << tok::r_paren;
7729
7743
}
7730
7744
break ;
0 commit comments