Skip to content

Commit 4f70889

Browse files
committed
[CSBindings] Detect situations when type variable bindings could be incomplete
When `bind param` constraint is associated with a given type variable a set of bindings collected for it is potentially incomplete, because binding gathering doesn't look through related type variables. Which means that such type variable has to be de-prioritized until `bind param` constraint is resolved or there is just nothing else to try, otherwise there is a risk that solver would skip some of the valid solutions. Resolves: rdar://problem/45659733
1 parent b5aa1b3 commit 4f70889

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,15 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
423423
case ConstraintKind::ArgumentConversion:
424424
case ConstraintKind::OperatorArgumentConversion:
425425
case ConstraintKind::OptionalObject: {
426+
// If there is a `bind param` constraint associated with
427+
// current type variable, result should be aware of that
428+
// fact. Binding set might be incomplete until
429+
// this constraint is resolved, because we currently don't
430+
// look-through constraints expect to `subtype` to try and
431+
// find related bindings.
432+
if (constraint->getKind() == ConstraintKind::BindParam)
433+
result.PotentiallyIncomplete = true;
434+
426435
auto binding = getPotentialBindingForRelationalConstraint(
427436
result, constraint, hasDependentMemberRelationalConstraints,
428437
hasNonDependentMemberRelationalConstraints,

lib/Sema/ConstraintSystem.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2806,6 +2806,12 @@ class ConstraintSystem {
28062806
/// Whether the bindings of this type involve other type variables.
28072807
bool InvolvesTypeVariables = false;
28082808

2809+
/// Whether the bindings represent (potentially) incomplete set,
2810+
/// there is no way to say with absolute certainty if that's the
2811+
/// case, but that could happen when certain constraints like
2812+
/// `bind param` are present in the system.
2813+
bool PotentiallyIncomplete = false;
2814+
28092815
/// Whether this type variable has literal bindings.
28102816
LiteralBindingKind LiteralBinding = LiteralBindingKind::None;
28112817

@@ -2850,9 +2856,14 @@ class ConstraintSystem {
28502856
if (formBindingScore(y) < formBindingScore(x))
28512857
return false;
28522858

2853-
// If the only difference is default types,
2859+
// If there is a difference in number of default types,
28542860
// prioritize bindings with fewer of them.
2855-
return x.NumDefaultableBindings < y.NumDefaultableBindings;
2861+
if (x.NumDefaultableBindings != y.NumDefaultableBindings)
2862+
return x.NumDefaultableBindings < y.NumDefaultableBindings;
2863+
2864+
// As a last resort, let's check if the bindings are
2865+
// potentially incomplete, and if so, let's de-prioritize them.
2866+
return x.PotentiallyIncomplete < y.PotentiallyIncomplete;
28562867
}
28572868

28582869
void foundLiteralBinding(ProtocolDecl *proto) {
@@ -2885,6 +2896,8 @@ class ConstraintSystem {
28852896
void dump(llvm::raw_ostream &out,
28862897
unsigned indent = 0) const LLVM_ATTRIBUTE_USED {
28872898
out.indent(indent);
2899+
if (PotentiallyIncomplete)
2900+
out << "potentially_incomplete ";
28882901
if (FullyBound)
28892902
out << "fully_bound ";
28902903
if (SubtypeOfExistentialType)

test/Constraints/closures.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,3 +797,11 @@ func test<Instances : Collection>(
797797
) { fatalError() }
798798

799799
test([1]) { _, _ in fatalError(); () }
800+
801+
// rdar://problem/45659733
802+
func rdar_45659733() {
803+
func foo<T : BinaryInteger>(_: AnyHashable, _: T) {}
804+
func bar(_ a: Int, _ b: Int) {
805+
_ = (a ..< b).map { i in foo(i, i) } // Ok
806+
}
807+
}

0 commit comments

Comments
 (0)