Skip to content

Commit 01a4468

Browse files
committed
[ConstraintSystem] Extract creation/recording of implicit .callAsFunction roots
1 parent 36c02e4 commit 01a4468

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3488,6 +3488,11 @@ class ConstraintSystem {
34883488
void recordMatchCallArgumentResult(ConstraintLocator *locator,
34893489
MatchCallArgumentResult result);
34903490

3491+
/// Record implicitly generated `callAsFunction` with root at the
3492+
/// given expression, located at \c locator.
3493+
void recordCallAsFunction(UnresolvedDotExpr *root, ArgumentList *arguments,
3494+
ConstraintLocator *locator);
3495+
34913496
/// Walk a closure AST to determine its effects.
34923497
///
34933498
/// \returns a function's extended info describing the effects, as

lib/Sema/CSSimplify.cpp

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10495,6 +10495,32 @@ bool ConstraintSystem::simplifyAppliedOverloads(
1049510495
numOptionalUnwraps, locator);
1049610496
}
1049710497

10498+
/// Create an implicit dot-member reference expression to be used
10499+
/// as a root for injected `.callAsFunction` call.
10500+
static UnresolvedDotExpr *
10501+
createImplicitRootForCallAsFunction(ConstraintSystem &cs, Type refType,
10502+
ArgumentList *arguments,
10503+
ConstraintLocator *calleeLocator) {
10504+
auto &ctx = cs.getASTContext();
10505+
auto *baseExpr = castToExpr(calleeLocator->getAnchor());
10506+
10507+
SmallVector<Identifier, 2> closureLabelsScratch;
10508+
// Create implicit `.callAsFunction` expression to use as an anchor
10509+
// for new argument list that only has trailing closures in it.
10510+
auto *implicitRef = UnresolvedDotExpr::createImplicit(
10511+
ctx, baseExpr, {ctx.Id_callAsFunction},
10512+
arguments->getArgumentLabels(closureLabelsScratch));
10513+
10514+
{
10515+
// Record a type of the new reference in the constraint system.
10516+
cs.setType(implicitRef, refType);
10517+
// Record new `.callAsFunction` in the constraint system.
10518+
cs.recordCallAsFunction(implicitRef, arguments, calleeLocator);
10519+
}
10520+
10521+
return implicitRef;
10522+
}
10523+
1049810524
ConstraintSystem::SolutionKind
1049910525
ConstraintSystem::simplifyApplicableFnConstraint(
1050010526
Type type1, Type type2,
@@ -10654,7 +10680,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
1065410680
auto resultTy = func2->getResult();
1065510681

1065610682
// If this is a call that constructs a callable type with
10657-
// a trailing closure(s), closure(s) might not belong to
10683+
// trailing closure(s), closure(s) might not belong to
1065810684
// the constructor but rather to implicit `callAsFunction`,
1065910685
// there is no way to determine that without trying.
1066010686
if (resultTy->isCallableNominalType(DC) &&
@@ -10720,28 +10746,8 @@ ConstraintSystem::simplifyApplicableFnConstraint(
1072010746
/*RParen=*/SourceLoc(),
1072110747
/*firstTrailingClosureIndex=*/0);
1072210748

10723-
// The base expression for `.callAsFunction`.
10724-
auto *baseExpr = castToExpr(argumentsLoc->getAnchor());
10725-
10726-
SmallVector<Identifier, 2> closureLabelsScratch;
10727-
// Create implicit `.callAsFunction` expression to use as an anchor
10728-
// for new argument list that only has trailing closures in it.
10729-
auto *implicitCall = UnresolvedDotExpr::createImplicit(
10730-
ctx, baseExpr, {ctx.Id_callAsFunction},
10731-
implicitCallArgumentList->getArgumentLabels(closureLabelsScratch));
10732-
10733-
{
10734-
10735-
// Record new root in the constraint system.
10736-
ImplicitCallAsFunctionRoots.insert({calleeLoc, implicitCall});
10737-
10738-
setType(implicitCall, callAsFunctionResultTy);
10739-
10740-
associateArgumentList(
10741-
getConstraintLocator(implicitCall,
10742-
ConstraintLocator::ApplyArgument),
10743-
implicitCallArgumentList);
10744-
}
10749+
auto *implicitRef = createImplicitRootForCallAsFunction(
10750+
*this, callAsFunctionResultTy, implicitCallArgumentList, calleeLoc);
1074510751

1074610752
auto callAsFunctionArguments =
1074710753
FunctionType::get(trailingClosureTypes, callAsFunctionResultTy,
@@ -10753,7 +10759,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
1075310759
addUnsolvedConstraint(Constraint::create(
1075410760
*this, ConstraintKind::ApplicableFunction, callAsFunctionArguments,
1075510761
callableType,
10756-
getConstraintLocator(implicitCall,
10762+
getConstraintLocator(implicitRef,
1075710763
ConstraintLocator::ApplyFunction)));
1075810764
break;
1075910765
}
@@ -12005,6 +12011,15 @@ void ConstraintSystem::recordMatchCallArgumentResult(
1200512011
argumentMatchingChoices.insert({locator, result});
1200612012
}
1200712013

12014+
void ConstraintSystem::recordCallAsFunction(UnresolvedDotExpr *root,
12015+
ArgumentList *arguments,
12016+
ConstraintLocator *locator) {
12017+
ImplicitCallAsFunctionRoots.insert({locator, root});
12018+
12019+
associateArgumentList(
12020+
getConstraintLocator(root, ConstraintLocator::ApplyArgument), arguments);
12021+
}
12022+
1200812023
ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1200912024
ConstraintFix *fix, Type type1, Type type2, ConstraintKind matchKind,
1201012025
TypeMatchOptions flags, ConstraintLocatorBuilder locator) {

0 commit comments

Comments
 (0)