Skip to content

Commit fa1e2d3

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. This only affects type variable that appears one the right-hand side of the `bind param` constraint and represents result type of the closure body, because left-hand side gets types from overload choices. Resolves: rdar://problem/45659733
1 parent 4539dbf commit fa1e2d3

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,20 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
424424
case ConstraintKind::ArgumentConversion:
425425
case ConstraintKind::OperatorArgumentConversion:
426426
case ConstraintKind::OptionalObject: {
427+
// If there is a `bind param` constraint associated with
428+
// current type variable, result should be aware of that
429+
// fact. Binding set might be incomplete until
430+
// this constraint is resolved, because we currently don't
431+
// look-through constraints expect to `subtype` to try and
432+
// find related bindings.
433+
// This only affects type variable that appears one the
434+
// right-hand side of the `bind param` constraint and
435+
// represents result type of the closure body, because
436+
// left-hand side gets types from overload choices.
437+
if (constraint->getKind() == ConstraintKind::BindParam &&
438+
constraint->getSecondType()->isEqual(typeVar))
439+
result.PotentiallyIncomplete = true;
440+
427441
auto binding = getPotentialBindingForRelationalConstraint(
428442
result, constraint, hasDependentMemberRelationalConstraints,
429443
hasNonDependentMemberRelationalConstraints,

lib/Sema/ConstraintSystem.h

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

2834+
/// Whether the bindings represent (potentially) incomplete set,
2835+
/// there is no way to say with absolute certainty if that's the
2836+
/// case, but that could happen when certain constraints like
2837+
/// `bind param` are present in the system.
2838+
bool PotentiallyIncomplete = false;
2839+
28342840
/// Whether this type variable has literal bindings.
28352841
LiteralBindingKind LiteralBinding = LiteralBindingKind::None;
28362842

@@ -2875,9 +2881,14 @@ class ConstraintSystem {
28752881
if (formBindingScore(y) < formBindingScore(x))
28762882
return false;
28772883

2878-
// If the only difference is default types,
2884+
// If there is a difference in number of default types,
28792885
// prioritize bindings with fewer of them.
2880-
return x.NumDefaultableBindings < y.NumDefaultableBindings;
2886+
if (x.NumDefaultableBindings != y.NumDefaultableBindings)
2887+
return x.NumDefaultableBindings < y.NumDefaultableBindings;
2888+
2889+
// As a last resort, let's check if the bindings are
2890+
// potentially incomplete, and if so, let's de-prioritize them.
2891+
return x.PotentiallyIncomplete < y.PotentiallyIncomplete;
28812892
}
28822893

28832894
void foundLiteralBinding(ProtocolDecl *proto) {
@@ -2910,6 +2921,8 @@ class ConstraintSystem {
29102921
void dump(llvm::raw_ostream &out,
29112922
unsigned indent = 0) const LLVM_ATTRIBUTE_USED {
29122923
out.indent(indent);
2924+
if (PotentiallyIncomplete)
2925+
out << "potentially_incomplete ";
29132926
if (FullyBound)
29142927
out << "fully_bound ";
29152928
if (SubtypeOfExistentialType)

test/Constraints/closures.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,29 @@ func rdar_40537960() {
823823
_ = A(arr, fn: { L($0.v) }) // expected-error {{cannot convert value of type 'L' to closure result type 'R<T>'}}
824824
}
825825

826+
// rdar://problem/45659733
827+
func rdar_45659733() {
828+
func foo<T : BinaryInteger>(_: AnyHashable, _: T) {}
829+
func bar(_ a: Int, _ b: Int) {
830+
_ = (a ..< b).map { i in foo(i, i) } // Ok
831+
}
832+
833+
struct S<V> {
834+
func map<T>(
835+
get: @escaping (V) -> T,
836+
set: @escaping (inout V, T) -> Void
837+
) -> S<T> {
838+
fatalError()
839+
}
840+
841+
subscript<T>(
842+
keyPath: WritableKeyPath<V, T?>,
843+
default defaultValue: T
844+
) -> S<T> {
845+
return map(
846+
get: { $0[keyPath: keyPath] ?? defaultValue },
847+
set: { $0[keyPath: keyPath] = $1 }
848+
) // Ok, make sure that we deduce result to be S<T>
849+
}
850+
}
851+
}

0 commit comments

Comments
 (0)