@@ -413,14 +413,49 @@ ConstraintLocator *ConstraintSystem::getConstraintLocator(
413
413
return getConstraintLocator (anchor, path, builder.getSummaryFlags ());
414
414
}
415
415
416
- ConstraintLocator *ConstraintSystem::getCalleeLocator (Expr *expr) {
416
+ ConstraintLocator *
417
+ ConstraintSystem::getAnchormostCalleeLocator (ConstraintLocator *locator) {
418
+ auto *anchor = locator->getAnchor ();
419
+ assert (anchor && " Expected an anchor!" );
420
+
421
+ // If we have a locator that starts with a key path component element, we
422
+ // may have a callee given by a property or subscript component.
423
+ if (auto componentElt =
424
+ locator->getFirstElementAs <LocatorPathElt::KeyPathComponent>()) {
425
+ auto *kpExpr = cast<KeyPathExpr>(anchor);
426
+ auto component = kpExpr->getComponents ()[componentElt->getIndex ()];
427
+
428
+ using ComponentKind = KeyPathExpr::Component::Kind;
429
+ switch (component.getKind ()) {
430
+ case ComponentKind::UnresolvedSubscript:
431
+ case ComponentKind::Subscript:
432
+ // For a subscript the callee is given by 'component -> subscript member'.
433
+ return getConstraintLocator (
434
+ anchor, {*componentElt, ConstraintLocator::SubscriptMember});
435
+ case ComponentKind::UnresolvedProperty:
436
+ case ComponentKind::Property:
437
+ // For a property, the choice is just given by the component.
438
+ return getConstraintLocator (anchor, *componentElt);
439
+ case ComponentKind::TupleElement:
440
+ llvm_unreachable (" Not implemented by CSGen" );
441
+ break ;
442
+ case ComponentKind::Invalid:
443
+ case ComponentKind::OptionalForce:
444
+ case ComponentKind::OptionalChain:
445
+ case ComponentKind::OptionalWrap:
446
+ case ComponentKind::Identity:
447
+ // These components don't have any callee associated, so just continue.
448
+ break ;
449
+ }
450
+ }
451
+
417
452
// Make sure we handle subscripts before looking at apply exprs. We don't
418
453
// want to return a subscript member locator for an expression such as x[](y),
419
454
// as its callee is not the subscript, but rather the function it returns.
420
- if (isa<SubscriptExpr>(expr ))
421
- return getConstraintLocator (expr , ConstraintLocator::SubscriptMember);
455
+ if (isa<SubscriptExpr>(anchor ))
456
+ return getConstraintLocator (anchor , ConstraintLocator::SubscriptMember);
422
457
423
- if (auto *applyExpr = dyn_cast<ApplyExpr>(expr )) {
458
+ if (auto *applyExpr = dyn_cast<ApplyExpr>(anchor )) {
424
459
auto *fnExpr = applyExpr->getFn ();
425
460
// For an apply of a metatype, we have a short-form constructor. Unlike
426
461
// other locators to callees, these are anchored on the apply expression
@@ -436,27 +471,27 @@ ConstraintLocator *ConstraintSystem::getCalleeLocator(Expr *expr) {
436
471
// Otherwise fall through and look for locators anchored on the function
437
472
// expr. For CallExprs, this can look through things like parens and
438
473
// optional chaining.
439
- if (auto *callExpr = dyn_cast<CallExpr>(expr )) {
440
- expr = callExpr->getDirectCallee ();
474
+ if (auto *callExpr = dyn_cast<CallExpr>(anchor )) {
475
+ anchor = callExpr->getDirectCallee ();
441
476
} else {
442
- expr = fnExpr;
477
+ anchor = fnExpr;
443
478
}
444
479
}
445
480
446
- if (auto *UDE = dyn_cast<UnresolvedDotExpr>(expr )) {
481
+ if (auto *UDE = dyn_cast<UnresolvedDotExpr>(anchor )) {
447
482
return getConstraintLocator (
448
- expr , TC.getSelfForInitDelegationInConstructor (DC, UDE)
483
+ anchor , TC.getSelfForInitDelegationInConstructor (DC, UDE)
449
484
? ConstraintLocator::ConstructorMember
450
485
: ConstraintLocator::Member);
451
486
}
452
487
453
- if (isa<UnresolvedMemberExpr>(expr ))
454
- return getConstraintLocator (expr , ConstraintLocator::UnresolvedMember);
488
+ if (isa<UnresolvedMemberExpr>(anchor ))
489
+ return getConstraintLocator (anchor , ConstraintLocator::UnresolvedMember);
455
490
456
- if (isa<MemberRefExpr>(expr ))
457
- return getConstraintLocator (expr , ConstraintLocator::Member);
491
+ if (isa<MemberRefExpr>(anchor ))
492
+ return getConstraintLocator (anchor , ConstraintLocator::Member);
458
493
459
- return getConstraintLocator (expr );
494
+ return getConstraintLocator (anchor );
460
495
}
461
496
462
497
Type ConstraintSystem::openUnboundGenericType (UnboundGenericType *unbound,
@@ -2462,7 +2497,7 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
2462
2497
return false ;
2463
2498
2464
2499
const auto *fix = fixes.front ();
2465
- auto *calleeLocator = getCalleeLocator (fix->getAnchor ());
2500
+ auto *calleeLocator = getAnchormostCalleeLocator (fix->getLocator ());
2466
2501
if (commonCalleeLocator && commonCalleeLocator != calleeLocator)
2467
2502
return false ;
2468
2503
@@ -2829,7 +2864,9 @@ void ConstraintSystem::generateConstraints(
2829
2864
}
2830
2865
}
2831
2866
2832
- ConstraintLocator *ConstraintSystem::getArgumentInfoLocator (Expr *anchor) {
2867
+ ConstraintLocator *
2868
+ ConstraintSystem::getArgumentInfoLocator (ConstraintLocator *locator) {
2869
+ auto *anchor = locator->getAnchor ();
2833
2870
if (!anchor)
2834
2871
return nullptr ;
2835
2872
@@ -2838,12 +2875,12 @@ ConstraintLocator *ConstraintSystem::getArgumentInfoLocator(Expr *anchor) {
2838
2875
return getConstraintLocator (fnExpr);
2839
2876
}
2840
2877
2841
- return getCalleeLocator (anchor );
2878
+ return getAnchormostCalleeLocator (locator );
2842
2879
}
2843
2880
2844
2881
Optional<ConstraintSystem::ArgumentInfo>
2845
2882
ConstraintSystem::getArgumentInfo (ConstraintLocator *locator) {
2846
- if (auto *infoLocator = getArgumentInfoLocator (locator-> getAnchor () )) {
2883
+ if (auto *infoLocator = getArgumentInfoLocator (locator)) {
2847
2884
auto known = ArgumentInfos.find (infoLocator);
2848
2885
if (known != ArgumentInfos.end ())
2849
2886
return known->second ;
0 commit comments