Skip to content

Commit e4b35f9

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 e4b35f9

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-typecheck-verify-swift -solver-expression-time-threshold=2
2+
// REQUIRES: tools-release,no_asan
3+
4+
// The time here fluctuates around from 700 ms. to 1.1 secs.
5+
6+
func f(n: Int, a: [Int]) {
7+
let _ = [(0 ..< n + a.count).map { Int8($0) }] +
8+
[(0 ..< n + a.count).map { Int8($0) }.reversed()] // Ok
9+
}

0 commit comments

Comments
 (0)