@@ -1057,7 +1057,7 @@ namespace {
1057
1057
}
1058
1058
}
1059
1059
1060
- return finishApply (apply, memberRef, openedType, locator);
1060
+ return finishApply (apply, openedType, locator, memberLocator );
1061
1061
}
1062
1062
1063
1063
// / Convert the given literal expression via a protocol pair.
@@ -1113,15 +1113,16 @@ namespace {
1113
1113
// / \param apply The function application to finish type-checking, which
1114
1114
// / may be a newly-built expression.
1115
1115
// /
1116
- // / \param callee The callee for the function being applied.
1117
- // /
1118
1116
// / \param openedType The "opened" type this expression had during
1119
1117
// / type checking, which will be used to specialize the resulting,
1120
1118
// / type-checked expression appropriately.
1121
1119
// /
1122
1120
// / \param locator The locator for the original expression.
1123
- Expr *finishApply (ApplyExpr *apply, ConcreteDeclRef callee, Type openedType,
1124
- ConstraintLocatorBuilder locator);
1121
+ // /
1122
+ // / \param calleeLocator The locator that identifies the apply's callee.
1123
+ Expr *finishApply (ApplyExpr *apply, Type openedType,
1124
+ ConstraintLocatorBuilder locator,
1125
+ ConstraintLocatorBuilder calleeLocator);
1125
1126
1126
1127
// Resolve `@dynamicCallable` applications.
1127
1128
Expr *finishApplyDynamicCallable (ApplyExpr *apply,
@@ -2390,17 +2391,16 @@ namespace {
2390
2391
2391
2392
// If there was an argument, apply it.
2392
2393
if (auto arg = expr->getArgument ()) {
2393
- // Find the callee. Note this may be different to the member being
2394
- // referenced for things like callAsFunction.
2394
+ // Get the callee locator . Note this may be different to the locator for
2395
+ // the member being referenced for things like callAsFunction.
2395
2396
auto *calleeLoc = cs.getCalleeLocator (exprLoc);
2396
- auto calleeOverload = solution.getOverloadChoice (calleeLoc);
2397
- auto callee = resolveConcreteDeclRef (calleeOverload.choice .getDecl (),
2398
- calleeLoc);
2397
+
2398
+ // Build and finish the apply.
2399
2399
ApplyExpr *apply = CallExpr::create (
2400
2400
ctx, result, arg, expr->getArgumentLabels (),
2401
2401
expr->getArgumentLabelLocs (), expr->hasTrailingClosure (),
2402
2402
/* implicit=*/ expr->isImplicit (), Type (), getType);
2403
- result = finishApply (apply, callee, Type (), exprLoc);
2403
+ result = finishApply (apply, Type (), exprLoc, calleeLoc );
2404
2404
2405
2405
// FIXME: Application could fail, because some of the solutions
2406
2406
// are not expressible in AST (yet?), like certain tuple-to-tuple
@@ -2579,9 +2579,8 @@ namespace {
2579
2579
auto *call = new (cs.getASTContext ()) DotSyntaxCallExpr (ctorRef, dotLoc,
2580
2580
base);
2581
2581
2582
- return finishApply (call, callee, cs.getType (expr),
2583
- ConstraintLocatorBuilder (
2584
- cs.getConstraintLocator (expr)));
2582
+ return finishApply (call, cs.getType (expr), cs.getConstraintLocator (expr),
2583
+ ctorLocator);
2585
2584
}
2586
2585
2587
2586
Expr *applyMemberRefExpr (Expr *expr, Expr *base, SourceLoc dotLoc,
@@ -3027,17 +3026,8 @@ namespace {
3027
3026
Expr *visitApplyExpr (ApplyExpr *expr) {
3028
3027
auto *calleeLoc = CalleeLocators[expr];
3029
3028
assert (calleeLoc);
3030
-
3031
- // Resolve the callee for the application if we have one. Note that we're
3032
- // using `resolveConcreteDeclRef` instead `solution.resolveLocatorToDecl`
3033
- // here to benefit from ExprRewriter's cache of concrete refs.
3034
- ConcreteDeclRef callee;
3035
- if (auto overload = solution.getOverloadChoiceIfAvailable (calleeLoc)) {
3036
- auto *decl = overload->choice .getDeclOrNull ();
3037
- callee = resolveConcreteDeclRef (decl, calleeLoc);
3038
- }
3039
- return finishApply (expr, callee, cs.getType (expr),
3040
- cs.getConstraintLocator (expr));
3029
+ return finishApply (expr, cs.getType (expr), cs.getConstraintLocator (expr),
3030
+ calleeLoc);
3041
3031
}
3042
3032
3043
3033
Expr *visitRebindSelfInConstructorExpr (RebindSelfInConstructorExpr *expr) {
@@ -6465,13 +6455,15 @@ static bool isValidDynamicCallableMethod(FuncDecl *method,
6465
6455
// Build a reference to a `callAsFunction` method.
6466
6456
static Expr *buildCallAsFunctionMethodRef (
6467
6457
ExprRewriter &rewriter, ApplyExpr *apply, SelectedOverload selected,
6468
- ConstraintLocatorBuilder applyFunctionLoc) {
6469
- auto *fn = apply->getFn ();
6458
+ ConstraintLocator *calleeLoc) {
6459
+ assert (calleeLoc->isLastElement <LocatorPathElt::ImplicitCallAsFunction>());
6460
+ assert (cast<FuncDecl>(selected.choice .getDecl ())->isCallAsFunctionMethod ());
6461
+
6470
6462
// Create direct reference to `callAsFunction` method.
6463
+ auto *fn = apply->getFn ();
6471
6464
auto *declRef = rewriter.buildMemberRef (
6472
6465
fn, /* dotLoc*/ SourceLoc (), selected, DeclNameLoc (fn->getEndLoc ()),
6473
- applyFunctionLoc, applyFunctionLoc, /* implicit*/ true ,
6474
- AccessSemantics::Ordinary);
6466
+ calleeLoc, calleeLoc, /* implicit*/ true , AccessSemantics::Ordinary);
6475
6467
if (!declRef)
6476
6468
return nullptr ;
6477
6469
declRef->setImplicit (apply->isImplicit ());
@@ -6559,9 +6551,9 @@ ExprRewriter::finishApplyDynamicCallable(ApplyExpr *apply,
6559
6551
return result;
6560
6552
}
6561
6553
6562
- Expr *ExprRewriter::finishApply (ApplyExpr *apply, ConcreteDeclRef callee ,
6563
- Type openedType ,
6564
- ConstraintLocatorBuilder locator ) {
6554
+ Expr *ExprRewriter::finishApply (ApplyExpr *apply, Type openedType ,
6555
+ ConstraintLocatorBuilder locator ,
6556
+ ConstraintLocatorBuilder calleeLocator ) {
6565
6557
auto &ctx = cs.getASTContext ();
6566
6558
6567
6559
auto fn = apply->getFn ();
@@ -6705,7 +6697,24 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, ConcreteDeclRef callee,
6705
6697
llvm_unreachable (" Unhandled DeclTypeCheckingSemantics in switch." );
6706
6698
};
6707
6699
6708
- // Resolve `callAsFunction` and `@dynamicCallable` applications.
6700
+ // Resolve the callee for the application if we have one.
6701
+ ConcreteDeclRef callee;
6702
+ auto *calleeLoc = cs.getConstraintLocator (calleeLocator);
6703
+ auto overload = solution.getOverloadChoiceIfAvailable (calleeLoc);
6704
+ if (overload) {
6705
+ auto *decl = overload->choice .getDeclOrNull ();
6706
+ callee = resolveConcreteDeclRef (decl, calleeLoc);
6707
+ }
6708
+
6709
+ // If this is an implicit call to a `callAsFunction` method, build the
6710
+ // appropriate member reference.
6711
+ if (cs.getType (fn)->getRValueType ()->isCallableNominalType (dc)) {
6712
+ fn = buildCallAsFunctionMethodRef (*this , apply, *overload, calleeLoc);
6713
+ if (!fn)
6714
+ return nullptr ;
6715
+ }
6716
+
6717
+ // Resolve a `@dynamicCallable` application.
6709
6718
auto applyFunctionLoc =
6710
6719
locator.withPathElement (ConstraintLocator::ApplyFunction);
6711
6720
if (auto selected = solution.getOverloadChoiceIfAvailable (
@@ -6718,15 +6727,6 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, ConcreteDeclRef callee,
6718
6727
if (isValidDynamicCallableMethod (method, methodType))
6719
6728
return finishApplyDynamicCallable (
6720
6729
apply, *selected, method, methodType, applyFunctionLoc);
6721
-
6722
- // If this is an implicit call to a callAsFunction method, build the
6723
- // appropriate member reference.
6724
- if (method->isCallAsFunctionMethod ()) {
6725
- fn = buildCallAsFunctionMethodRef (*this , apply, *selected,
6726
- applyFunctionLoc);
6727
- if (!fn)
6728
- return nullptr ;
6729
- }
6730
6730
}
6731
6731
}
6732
6732
@@ -6854,12 +6854,8 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, ConcreteDeclRef callee,
6854
6854
assert (ty->getNominalOrBoundGenericNominal () || ty->is <DynamicSelfType>() ||
6855
6855
ty->isExistentialType () || ty->is <ArchetypeType>());
6856
6856
6857
- // We have the constructor.
6858
- auto choice = selected->choice ;
6859
-
6860
6857
// Consider the constructor decl reference expr 'implicit', but the
6861
6858
// constructor call expr itself has the apply's 'implicitness'.
6862
- auto ctorRef = resolveConcreteDeclRef (choice.getDecl (), ctorLocator);
6863
6859
Expr *declRef = buildMemberRef (fn, /* dotLoc=*/ SourceLoc (), *selected,
6864
6860
DeclNameLoc (fn->getEndLoc ()), locator,
6865
6861
ctorLocator, /* Implicit=*/ true ,
@@ -6870,7 +6866,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, ConcreteDeclRef callee,
6870
6866
apply->setFn (declRef);
6871
6867
6872
6868
// Tail-recur to actually call the constructor.
6873
- return finishApply (apply, ctorRef, openedType, locator);
6869
+ return finishApply (apply, openedType, locator, ctorLocator );
6874
6870
}
6875
6871
6876
6872
// Return the precedence-yielding parent of 'expr', along with the index of
0 commit comments