Skip to content

Commit aff4974

Browse files
committed
[CSSolver] Prioritize bindings with fewer default types
With all else equal prioritize bindings with fewer defaultable types, which always gets us closer to solution since defaultable bindings mostly come from the collections and could be checked at the end. This also makes sure that solver not as aggresive at assigning bindings to trailing closures and allows solver to consider types which come from inside the closure. Resolves: rdar://problem/37290898
1 parent 4734920 commit aff4974

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/Sema/ConstraintSystem.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2758,6 +2758,9 @@ class ConstraintSystem {
27582758
if (!x.hasNonDefaultableBindings())
27592759
return false;
27602760

2761+
if (x.FullyBound || x.SubtypeOfExistentialType)
2762+
return false;
2763+
27612764
llvm::SmallPtrSet<Constraint *, 8> intersection(x.Sources);
27622765
llvm::set_intersect(intersection, y.Sources);
27632766

@@ -2774,7 +2777,9 @@ class ConstraintSystem {
27742777
return x.TypeVar == typeVar;
27752778
}
27762779

2777-
return false;
2780+
// If the only difference is default types,
2781+
// prioritize bindings with fewer of them.
2782+
return x.NumDefaultableBindings < y.NumDefaultableBindings;
27782783
}
27792784

27802785
void foundLiteralBinding(ProtocolDecl *proto) {

test/Constraints/operator.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,10 @@ struct S_35740653 {
194194
func rdar35740653(val: S_35740653) {
195195
let _ = 0...Int(val / .value(1.0 / 42.0)) // Ok
196196
}
197+
198+
protocol P_37290898 {}
199+
struct S_37290898: P_37290898 {}
200+
201+
func rdar37290898(_ arr: inout [P_37290898], _ element: S_37290898?) {
202+
arr += [element].compactMap { $0 } // Ok
203+
}

0 commit comments

Comments
 (0)