@@ -10495,6 +10495,32 @@ bool ConstraintSystem::simplifyAppliedOverloads(
10495
10495
numOptionalUnwraps, locator);
10496
10496
}
10497
10497
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
+
10498
10524
ConstraintSystem::SolutionKind
10499
10525
ConstraintSystem::simplifyApplicableFnConstraint (
10500
10526
Type type1, Type type2,
@@ -10654,7 +10680,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
10654
10680
auto resultTy = func2->getResult ();
10655
10681
10656
10682
// 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
10658
10684
// the constructor but rather to implicit `callAsFunction`,
10659
10685
// there is no way to determine that without trying.
10660
10686
if (resultTy->isCallableNominalType (DC) &&
@@ -10720,28 +10746,8 @@ ConstraintSystem::simplifyApplicableFnConstraint(
10720
10746
/* RParen=*/ SourceLoc (),
10721
10747
/* firstTrailingClosureIndex=*/ 0 );
10722
10748
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);
10745
10751
10746
10752
auto callAsFunctionArguments =
10747
10753
FunctionType::get (trailingClosureTypes, callAsFunctionResultTy,
@@ -10753,7 +10759,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
10753
10759
addUnsolvedConstraint (Constraint::create (
10754
10760
*this , ConstraintKind::ApplicableFunction, callAsFunctionArguments,
10755
10761
callableType,
10756
- getConstraintLocator (implicitCall ,
10762
+ getConstraintLocator (implicitRef ,
10757
10763
ConstraintLocator::ApplyFunction)));
10758
10764
break ;
10759
10765
}
@@ -12005,6 +12011,15 @@ void ConstraintSystem::recordMatchCallArgumentResult(
12005
12011
argumentMatchingChoices.insert ({locator, result});
12006
12012
}
12007
12013
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
+
12008
12023
ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint (
12009
12024
ConstraintFix *fix, Type type1, Type type2, ConstraintKind matchKind,
12010
12025
TypeMatchOptions flags, ConstraintLocatorBuilder locator) {
0 commit comments