@@ -3532,11 +3532,29 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
35323532 }
35333533 }
35343534
3535+ auto resultMatchKind = subKind;
3536+ // Performance optimization: Propagate fully or partially resolved contextual
3537+ // type down into the body of result builder transformed closure by eagerly
3538+ // binding intermediate body result type to the contextual one. This helps to
3539+ // determine when closure body could be solved early.
3540+ //
3541+ // TODO: This could be extended to cover all multi-statement closures.
3542+ //
3543+ // See \c BindingSet::favoredOverConjunction for more details.
3544+ if (resultMatchKind >= ConstraintKind::Subtype &&
3545+ !func2->getResult()->isTypeVariableOrMember()) {
3546+ if (auto *closure = getAsExpr<ClosureExpr>(locator.trySimplifyToExpr())) {
3547+ if (!closure->hasExplicitResultType() &&
3548+ getAppliedResultBuilderTransform(closure)) {
3549+ resultMatchKind = ConstraintKind::Equal;
3550+ }
3551+ }
3552+ }
3553+
35353554 // Result type can be covariant (or equal).
3536- return matchTypes(func1->getResult(), func2->getResult(), subKind,
3537- subflags,
3538- locator.withPathElement(
3539- ConstraintLocator::FunctionResult));
3555+ return matchTypes(func1->getResult(), func2->getResult(), resultMatchKind,
3556+ subflags,
3557+ locator.withPathElement(ConstraintLocator::FunctionResult));
35403558}
35413559
35423560ConstraintSystem::TypeMatchResult
0 commit comments