@@ -759,22 +759,27 @@ namespace {
759759 new (ctx) DeclRefExpr (ref, loc, implicit, semantics, fullType);
760760 cs.cacheType (declRefExpr);
761761 declRefExpr->setFunctionRefKind (choice.getFunctionRefKind ());
762- Expr *result = adjustTypeForDeclReference (
763- declRefExpr, fullType, adjustedFullType, locator);
764- // If we have to load, do so now.
765- if (loadImmediately)
766- result = cs.addImplicitLoadExpr (result);
767762
768- result = forceUnwrapIfExpected (result , locator);
763+ Expr * result = forceUnwrapIfExpected (declRefExpr , locator);
769764
770765 if (auto *fnDecl = dyn_cast<AbstractFunctionDecl>(decl)) {
771766 if (AnyFunctionRef (fnDecl).hasExternalPropertyWrapperParameters () &&
772767 (declRefExpr->getFunctionRefKind () == FunctionRefKind::Compound ||
773768 declRefExpr->getFunctionRefKind () == FunctionRefKind::Unapplied)) {
774- result = buildSingleCurryThunk (result, fnDecl, locator);
769+ // We don't need to do any further adjustment once we've built the
770+ // curry thunk.
771+ return buildSingleCurryThunk (result, fnDecl,
772+ adjustedFullType->castTo <FunctionType>(),
773+ locator);
775774 }
776775 }
777776
777+ result = adjustTypeForDeclReference (result, fullType, adjustedFullType,
778+ locator);
779+ // If we have to load, do so now.
780+ if (loadImmediately)
781+ result = cs.addImplicitLoadExpr (result);
782+
778783 return result;
779784 }
780785
@@ -1406,41 +1411,20 @@ namespace {
14061411 // / parameters.
14071412 // / \param declOrClosure The underlying function-like declaration or
14081413 // / closure we're going to call.
1414+ // / \param thunkTy The type of the resulting thunk. This should be the
1415+ // / type of the \c fnExpr, with any potential adjustments for things like
1416+ // / concurrency.
14091417 // / \param locator The locator pinned on the function reference carried
14101418 // / by \p fnExpr. If the function has associated applied property wrappers,
14111419 // / the locator is used to pull them in.
14121420 AutoClosureExpr *buildSingleCurryThunk (Expr *fnExpr,
14131421 DeclContext *declOrClosure,
1422+ FunctionType *thunkTy,
14141423 ConstraintLocatorBuilder locator) {
1415- auto *const thunkTy = cs.getType (fnExpr)->castTo <FunctionType>();
1416-
14171424 return buildSingleCurryThunk (/* baseExpr=*/ nullptr , fnExpr, declOrClosure,
14181425 thunkTy, locator);
14191426 }
14201427
1421- // / Build a "{ args in base.fn(args) }" single-expression curry thunk.
1422- // /
1423- // / \param baseExpr The base expression to be captured.
1424- // / \param fnExpr The expression to be called by consecutively applying
1425- // / the \p baseExpr and thunk parameters.
1426- // / \param declOrClosure The underlying function-like declaration or
1427- // / closure we're going to call.
1428- // / \param locator The locator pinned on the function reference carried
1429- // / by \p fnExpr. If the function has associated applied property wrappers,
1430- // / the locator is used to pull them in.
1431- AutoClosureExpr *buildSingleCurryThunk (Expr *baseExpr, Expr *fnExpr,
1432- DeclContext *declOrClosure,
1433- ConstraintLocatorBuilder locator) {
1434- assert (baseExpr);
1435- auto *const thunkTy = cs.getType (fnExpr)
1436- ->castTo <FunctionType>()
1437- ->getResult ()
1438- ->castTo <FunctionType>();
1439-
1440- return buildSingleCurryThunk (baseExpr, fnExpr, declOrClosure, thunkTy,
1441- locator);
1442- }
1443-
14441428 // / Build a "{ self in { args in self.fn(args) } }" nested curry thunk.
14451429 // /
14461430 // / \param memberRef The expression to be called in the inner thunk by
@@ -1909,9 +1893,10 @@ namespace {
19091893 // have side effects, instead of abstracting out a 'self' parameter.
19101894 const auto isSuperPartialApplication = needsCurryThunk && isSuper;
19111895 if (isSuperPartialApplication) {
1912- ref = buildSingleCurryThunk (base, declRefExpr,
1913- cast<AbstractFunctionDecl>(member),
1914- memberLocator);
1896+ ref = buildSingleCurryThunk (
1897+ base, declRefExpr, cast<AbstractFunctionDecl>(member),
1898+ adjustedOpenedType->castTo <FunctionType>(),
1899+ memberLocator);
19151900 } else if (needsCurryThunk) {
19161901 // Another case where we want to build a single closure is when
19171902 // we have a partial application of a static member. It is better
@@ -1927,14 +1912,13 @@ namespace {
19271912 cs.getType (base));
19281913 cs.setType (base, base->getType ());
19291914
1930- auto *closure = buildSingleCurryThunk (
1931- base, declRefExpr, cast<AbstractFunctionDecl>(member),
1932- memberLocator);
1933-
19341915 // Skip the code below -- we're not building an extra level of
19351916 // call by applying the metatype; instead, the closure we just
19361917 // built is the curried reference.
1937- return closure;
1918+ return buildSingleCurryThunk (
1919+ base, declRefExpr, cast<AbstractFunctionDecl>(member),
1920+ adjustedOpenedType->castTo <FunctionType>(),
1921+ memberLocator);
19381922 } else {
19391923 // Add a useless ".self" to avoid downstream diagnostics, in case
19401924 // the type ref is still a TypeExpr.
@@ -8874,8 +8858,10 @@ namespace {
88748858 rewriteFunction (closure);
88758859
88768860 if (AnyFunctionRef (closure).hasExternalPropertyWrapperParameters ()) {
8861+ auto *thunkTy = Rewriter.cs .getType (closure)->castTo <FunctionType>();
88778862 return Action::SkipNode (Rewriter.buildSingleCurryThunk (
8878- closure, closure, Rewriter.cs .getConstraintLocator (closure)));
8863+ closure, closure, thunkTy,
8864+ Rewriter.cs .getConstraintLocator (closure)));
88798865 }
88808866
88818867 return Action::SkipNode (closure);
0 commit comments