Skip to content

Commit 36c02e4

Browse files
committed
[CSApply] Implement support for implicit .callAsFunction after init with trailing closures
Situations like `T(...) { ... }` where `T` is a callable type and trailing closure belongs to `.callAsFunction` should be rewritten as `T.init().callAsFunction { ... }`.
1 parent 9e2137d commit 36c02e4

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

lib/Sema/CSApply.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7709,7 +7709,38 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
77097709
apply->setFn(declRef);
77107710

77117711
// Tail-recur to actually call the constructor.
7712-
return finishApply(apply, openedType, locator, ctorLocator);
7712+
auto *ctorCall = finishApply(apply, openedType, locator, ctorLocator);
7713+
7714+
// Check whether this is a situation like `T(...) { ... }` where `T` is
7715+
// a callable type and trailing closure(s) are associated with implicit
7716+
// `.callAsFunction` instead of constructor.
7717+
{
7718+
auto callAsFunction =
7719+
solution.ImplicitCallAsFunctionRoots.find(ctorLocator);
7720+
if (callAsFunction != solution.ImplicitCallAsFunctionRoots.end()) {
7721+
auto *dotExpr = callAsFunction->second;
7722+
auto resultTy = solution.getResolvedType(dotExpr);
7723+
7724+
auto *implicitCall = CallExpr::createImplicit(
7725+
cs.getASTContext(), ctorCall,
7726+
solution.getArgumentList(cs.getConstraintLocator(
7727+
dotExpr, ConstraintLocator::ApplyArgument)));
7728+
7729+
implicitCall->setType(resultTy);
7730+
cs.cacheType(implicitCall);
7731+
7732+
auto *memberCalleeLoc =
7733+
cs.getConstraintLocator(dotExpr,
7734+
{ConstraintLocator::ApplyFunction,
7735+
ConstraintLocator::ImplicitCallAsFunction},
7736+
/*summaryFlags=*/0);
7737+
7738+
return finishApply(implicitCall, resultTy, cs.getConstraintLocator(dotExpr),
7739+
memberCalleeLoc);
7740+
}
7741+
}
7742+
7743+
return ctorCall;
77137744
}
77147745

77157746
/// Determine whether this closure should be treated as Sendable.

0 commit comments

Comments
 (0)