Skip to content

Commit 2fa4bf8

Browse files
authored
Merge pull request #58778 from xedin/rdar-92912878
[CSSimplify] Avoid filtering `init` overloads of a callable type
2 parents d74d00e + d81007c commit 2fa4bf8

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10946,6 +10946,24 @@ bool ConstraintSystem::simplifyAppliedOverloadsImpl(
1094610946

1094710947
auto *argList = getArgumentList(getConstraintLocator(locator));
1094810948

10949+
// If argument list has trailing closures and this is `init` call to
10950+
// a callable type, let's not filter anything since there is a possibility
10951+
// that it needs an implicit `.callAsFunction` to work.
10952+
if (argList && argList->hasAnyTrailingClosures()) {
10953+
if (disjunction->getLocator()
10954+
->isLastElement<LocatorPathElt::ConstructorMember>()) {
10955+
auto choice = disjunction->getNestedConstraints()[0]->getOverloadChoice();
10956+
if (auto *decl = choice.getDeclOrNull()) {
10957+
auto *dc = decl->getDeclContext();
10958+
if (auto *parent = dc->getSelfNominalTypeDecl()) {
10959+
auto type = parent->getDeclaredInterfaceType();
10960+
if (type->isCallableNominalType(DC))
10961+
return false;
10962+
}
10963+
}
10964+
}
10965+
}
10966+
1094910967
// Consider each of the constraints in the disjunction.
1095010968
retry_after_fail:
1095110969
bool hasUnhandledConstraints = false;

test/Constraints/callAsFunction.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,25 @@ struct Test {
2929
}
3030
}
3131
}
32+
33+
// rdar://92912878 - filtering prevents disambiguation of `.callAsFunction`
34+
func test_no_filtering_of_overloads() {
35+
struct S {
36+
init() {}
37+
init(_: String) {}
38+
39+
func callAsFunction<T>(_ fn: () -> T) -> T {
40+
fn()
41+
}
42+
}
43+
44+
func test(_: () -> Void) {
45+
}
46+
47+
test {
48+
_ = S() { // Ok
49+
_ = 42
50+
print("Hello")
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)