Skip to content

Commit a0a4a5b

Browse files
hamishknightxedin
authored andcommitted
[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. (cherry picked from commit 40d2f51)
1 parent 8da7501 commit a0a4a5b

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
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: 4 additions & 1 deletion
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-
init(fn: () -> Int) {
112+
subscript<T>() -> (T, T) -> Void { { _, _ in } }
113+
114+
init(fn: () -> Int) { // expected-note {{parameter 'fn' is implicitly non-escaping}}
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 {{passing non-escaping parameter 'fn' to function expecting an @escaping closure}}
114117
}
115118
}
116119

0 commit comments

Comments
 (0)