Skip to content

Commit 40d2f51

Browse files
committed
[CS] Don't retrieve an overload choice for x[](y)
Previously we returned a subscript member locator for an apply of a subscript expr, which is incorrect because the callee is the function returned from the subscript rather than the subscript itself.
1 parent ebc2ff5 commit 40d2f51

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,12 @@ ConstraintLocator *ConstraintSystem::getConstraintLocator(
409409
}
410410

411411
ConstraintLocator *ConstraintSystem::getCalleeLocator(Expr *expr) {
412+
// Make sure we handle subscripts before looking at apply exprs. We don't
413+
// want to return a subscript member locator for an expression such as x[](y),
414+
// as its callee is not the subscript, but rather the function it returns.
415+
if (isa<SubscriptExpr>(expr))
416+
return getConstraintLocator(expr, ConstraintLocator::SubscriptMember);
417+
412418
if (auto *applyExpr = dyn_cast<ApplyExpr>(expr)) {
413419
auto *fnExpr = applyExpr->getFn();
414420
// For an apply of a metatype, we have a short-form constructor. Unlike
@@ -437,9 +443,6 @@ ConstraintLocator *ConstraintSystem::getCalleeLocator(Expr *expr) {
437443
if (isa<UnresolvedMemberExpr>(expr))
438444
return getConstraintLocator(locator, ConstraintLocator::UnresolvedMember);
439445

440-
if (isa<SubscriptExpr>(expr))
441-
return getConstraintLocator(locator, ConstraintLocator::SubscriptMember);
442-
443446
if (isa<MemberRefExpr>(expr))
444447
return getConstraintLocator(locator, ConstraintLocator::Member);
445448

test/Constraints/function.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,11 @@ func foo(block: () -> (), other: () -> Int) {
109109

110110
struct S {
111111
init<T>(_ x: T, _ y: T) {} // expected-note {{generic parameters are always considered '@escaping'}}
112+
subscript<T>() -> (T, T) -> Void { { _, _ in } } // expected-note {{generic parameters are always considered '@escaping'}}
113+
112114
init(fn: () -> Int) {
113115
self.init({ 0 }, fn) // expected-error {{converting non-escaping parameter 'fn' to generic parameter 'T' may allow it to escape}}
116+
_ = self[]({ 0 }, fn) // expected-error {{converting non-escaping parameter 'fn' to generic parameter 'T' may allow it to escape}}
114117
}
115118
}
116119

0 commit comments

Comments
 (0)