Skip to content

Commit 604852d

Browse files
authored
Merge pull request #26712 from gregomni/sr-11234
[CSGen] Coerce key path index expr to handle upcast correctly.
2 parents af47110 + cf04729 commit 604852d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/Sema/CSApply.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,10 @@ namespace {
12851285
} else {
12861286
auto keyPathBGT = keyPathTy->castTo<BoundGenericType>();
12871287
baseTy = keyPathBGT->getGenericArgs()[0];
1288-
1288+
1289+
// Coerce the index to the key path's type
1290+
indexKP = coerceToType(indexKP, keyPathTy, locator);
1291+
12891292
// Coerce the base to the key path's expected base type.
12901293
if (!baseTy->isEqual(cs.getType(base)->getRValueType()))
12911294
base = coerceToType(base, baseTy, locator);

test/Constraints/keypath.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ func testFunc() {
5151
let f = \S.i
5252
let _: (S) -> Int = f // expected-error {{cannot convert value of type 'KeyPath<S, Int>' to specified type '(S) -> Int'}}
5353
}
54+
55+
// SR-11234
56+
public extension Array {
57+
func sorted<C: Comparable, K: KeyPath<Element, C>>(by keyPath: K) -> Array<Element> {
58+
let sortedA = self.sorted(by: { $0[keyPath: keyPath] < $1[keyPath: keyPath] })
59+
return sortedA
60+
}
61+
}

0 commit comments

Comments
 (0)