Skip to content

Commit d0fae40

Browse files
committed
[CSSimplify] Increase score if conformance constraint finds missing conformances
Although the solver is allowed to use types with missing synthesizable conformances we need to note their presence in the score in order to rank overloads without such types higher.
1 parent b13c219 commit d0fae40

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8412,6 +8412,16 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
84128412
if (isConformanceUnavailable(conformance, loc))
84138413
increaseScore(SK_Unavailable, locator);
84148414

8415+
unsigned numMissing = 0;
8416+
conformance.forEachMissingConformance(DC->getParentModule(),
8417+
[&numMissing](auto *missing) {
8418+
++numMissing;
8419+
return false;
8420+
});
8421+
8422+
if (numMissing > 0)
8423+
increaseScore(SK_MissingSynthesizableConformance, locator, numMissing);
8424+
84158425
// This conformance may be conditional, in which case we need to consider
84168426
// those requirements as constraints too.
84178427
if (conformance.isConcrete()) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %target-swift-frontend %s -emit-sil -o /dev/null -verify -strict-concurrency=complete
2+
3+
// REQUIRES: concurrency
4+
// REQUIRES: asserts
5+
// REQUIRES: OS=macosx
6+
7+
do {
8+
struct Test {
9+
init<T: Sendable>(value: T) {}
10+
// expected-note@-1 {{found this candidate}}
11+
init<T>(value: T) {}
12+
// expected-note@-1 {{found this candidate}}
13+
}
14+
15+
struct SendableOnly {
16+
init<T: Sendable>(value: T) {}
17+
}
18+
19+
func testNonSendable<T>(v: T) { // expected-note {{consider making generic parameter 'T' conform to the 'Sendable' protocol}}
20+
_ = Test(value: v) // Ok (non-Sendable overload)
21+
_ = SendableOnly(value: v)
22+
// expected-warning@-1 {{type 'T' does not conform to the 'Sendable' protocol}}
23+
}
24+
25+
func testSendable<T: Sendable>(v: T) {
26+
_ = Test(value: v)
27+
// expected-error@-1 {{ambiguous use of 'init(value:)'}}
28+
_ = SendableOnly(value: v) // Ok
29+
}
30+
}

0 commit comments

Comments
 (0)