Skip to content

Commit e684fe1

Browse files
committed
[CSOptimizer] Favor choices that don't require application
When disjunction is not applied, don't only bump its score but also favor all of the choices that don't require application because selection algorithm uses that for comparison. This is important for situation when property is overload with a method i.e. `Array.count`.
1 parent 55189ba commit e684fe1

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/Sema/CSOptimizer.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,19 @@ static void determineBestChoicesInContext(
298298
case ExprKind::Binary:
299299
case ExprKind::PrefixUnary:
300300
case ExprKind::PostfixUnary:
301-
case ExprKind::UnresolvedDot:
302-
recordResult(disjunction, {/*score=*/1.0});
301+
case ExprKind::UnresolvedDot: {
302+
llvm::SmallVector<Constraint *, 2> favoredChoices;
303+
// Favor choices that don't require application.
304+
llvm::copy_if(
305+
disjunction->getNestedConstraints(),
306+
std::back_inserter(favoredChoices), [](Constraint *choice) {
307+
auto *decl = getOverloadChoiceDecl(choice);
308+
return decl &&
309+
!decl->getInterfaceType()->is<AnyFunctionType>();
310+
});
311+
recordResult(disjunction, {/*score=*/1.0, favoredChoices});
303312
continue;
313+
}
304314

305315
default:
306316
break;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-typecheck-verify-swift -solver-expression-time-threshold=1
2+
// REQUIRES: tools-release,no_asan
3+
4+
func f(n: Int, a: [Int]) {
5+
let _ = [(0 ..< n + a.count).map { Int8($0) }] +
6+
[(0 ..< n + a.count).map { Int8($0) }.reversed()] // Ok
7+
}

0 commit comments

Comments
 (0)