Skip to content

Commit 1737303

Browse files
committed
[ConstraintSystem] Increase the score when attempting overload choices
for unapplied references when the choice is a function declaration. This will allow the solver to prune those overload choices when it has already found a solultion with a property (all else equal in the score). This is already done as an ambiguity tie-breaker in solution ranking, but adding this bit to the score will prune a lot of search space within the solver.
1 parent 131a081 commit 1737303

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,8 +768,18 @@ enum ScoreKind {
768768
SK_ValueToPointerConversion,
769769
/// A closure/function conversion to an autoclosure parameter.
770770
SK_FunctionToAutoClosureConversion,
771-
772-
SK_LastScoreKind = SK_FunctionToAutoClosureConversion,
771+
/// An unapplied reference to a function. The purpose of this
772+
/// score bit is to prune overload choices that are functions
773+
/// when a solution has already been found using property.
774+
///
775+
/// \Note The solver only prefers variables over functions
776+
/// to resolve ambiguities, so please be sure that any score
777+
/// kind added after this is truly less impactful. Only other
778+
/// ambiguity tie-breakers should go after this; anything else
779+
/// should be added above.
780+
SK_UnappliedFunction,
781+
782+
SK_LastScoreKind = SK_UnappliedFunction,
773783
};
774784

775785
/// The number of score kinds.

lib/Sema/CSRanking.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ static StringRef getScoreKindName(ScoreKind kind) {
9090

9191
case SK_ImplicitValueConversion:
9292
return "value-to-value conversion";
93+
94+
case SK_UnappliedFunction:
95+
return "overloaded unapplied function";
9396
}
9497
}
9598

lib/Sema/ConstraintSystem.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,6 +2947,12 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
29472947
}
29482948
}
29492949

2950+
if (isa<AbstractFunctionDecl>(decl) || isa<TypeDecl>(decl)) {
2951+
if (choice.getFunctionRefKind() == FunctionRefKind::Unapplied) {
2952+
increaseScore(SK_UnappliedFunction);
2953+
}
2954+
}
2955+
29502956
if (auto *afd = dyn_cast<AbstractFunctionDecl>(decl)) {
29512957
// Check whether applying this overload would result in invalid
29522958
// partial function application e.g. partial application of

0 commit comments

Comments
 (0)