@@ -7804,18 +7804,27 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
7804
7804
auto *body = args->getExpr (1 );
7805
7805
auto bodyTy = cs.getType (body)->getWithoutSpecifierType ();
7806
7806
auto bodyFnTy = bodyTy->castTo <FunctionType>();
7807
- auto escapableParams = bodyFnTy->getParams ();
7808
7807
auto resultType = bodyFnTy->getResult ();
7809
7808
7810
7809
// The body is immediately called, so is obviously noescape.
7810
+ // Coerce the argument function to be escaping even if it happens to
7811
+ // be nonescaping, since we need the dynamic state of the escaping
7812
+ // closure to do the dynamic noescape check.
7813
+ auto bodyArgFnTy = bodyFnTy->getParams ()[0 ].getPlainType ()
7814
+ ->castTo <FunctionType>();
7815
+
7816
+ bodyArgFnTy = cast<FunctionType>(
7817
+ bodyArgFnTy->withExtInfo (bodyArgFnTy->getExtInfo ().withNoEscape (false )));
7811
7818
bodyFnTy = cast<FunctionType>(
7812
- bodyFnTy->withExtInfo (bodyFnTy->getExtInfo ().withNoEscape ()));
7819
+ FunctionType::get (bodyFnTy->getParams ()[0 ].withType (bodyArgFnTy),
7820
+ bodyFnTy->getResult ())
7821
+ ->withExtInfo (bodyFnTy->getExtInfo ().withNoEscape ()));
7813
7822
body = coerceToType (body, bodyFnTy, locator);
7814
7823
assert (body && " can't make nonescaping?!" );
7815
7824
7816
7825
auto escapable = new (ctx)
7817
7826
OpaqueValueExpr (apply->getFn ()->getSourceRange (), Type ());
7818
- cs.setType (escapable, escapableParams[ 0 ]. getOldType () );
7827
+ cs.setType (escapable, bodyArgFnTy );
7819
7828
7820
7829
auto *argList = ArgumentList::forImplicitUnlabeled (ctx, {escapable});
7821
7830
auto callSubExpr = CallExpr::createImplicit (ctx, body, argList);
0 commit comments