Skip to content

Commit 78c1e1b

Browse files
committed
[CSBindings] Transitive key path root inference should make note of adjacent vars
Binding inference through a contextual root variable should include marking contextual root and all of its adjacent variables as adjacent to a key path root variable as well otherwise transitively inferred bindings are ranked incorrectly.
1 parent 835fa02 commit 78c1e1b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,16 @@ void BindingSet::inferTransitiveBindings(
471471
if (bindings.isDelayed())
472472
continue;
473473

474+
// Copy the bindings over to the root.
474475
for (const auto &binding : bindings.Bindings)
475476
addBinding(binding);
477+
478+
// Make a note that the key path root is transitively adjacent
479+
// to contextual root type variable and all of its variables.
480+
// This is important for ranking.
481+
AdjacentVars.insert(contextualRootVar);
482+
AdjacentVars.insert(bindings.AdjacentVars.begin(),
483+
bindings.AdjacentVars.end());
476484
}
477485
} else {
478486
addBinding(

test/Sema/keypath_bidirectional_inference.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,23 @@ class FilteringTest {
6262
.assign(to: \.innerController, on: viewController) // Ok
6363
}
6464
}
65+
66+
extension Sequence {
67+
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>) -> [Element] {
68+
[]
69+
}
70+
}
71+
72+
func testCollectionUpcastWithTupleLabelErasure() {
73+
struct Item {}
74+
75+
enum Info : Int, Hashable {
76+
case one = 1
77+
}
78+
79+
80+
func test(data: [Info: [Item]]) -> [(Info, [Item])] {
81+
data.map { $0 }
82+
.sorted(by: \.key.rawValue) // Ok
83+
}
84+
}

0 commit comments

Comments
 (0)