Skip to content

Commit 9cd9619

Browse files
committed
[CSBindings] Unwrap optionals from contextual function type used as a key path type
The key path is going to be implicitly wrapped into a contextual optional type. Resolves: rdar://72864716 Resolves: #56393
1 parent 28e6369 commit 9cd9619

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,9 @@ void BindingSet::finalize(
634634
assert(isKnownKeyPathType(bindingTy) || bindingTy->is<FunctionType>());
635635

636636
// Functions don't have capability so we can simply add them.
637-
if (bindingTy->is<FunctionType>())
638-
updatedBindings.insert(binding);
637+
if (auto *fnType = bindingTy->getAs<FunctionType>()) {
638+
updatedBindings.insert(binding.withType(fnType));
639+
}
639640
}
640641

641642
// Note that even though key path literal maybe be invalid it's

test/Constraints/keypath.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,17 @@ func test_invalid_argument_to_keypath_subscript() {
304304
// expected-error@-1 {{cannot use value of type 'A' as a key path subscript index; argument must be a key path}}
305305
}
306306
}
307+
308+
extension Collection {
309+
func prefix<R: RangeExpression>(
310+
_ range: R,
311+
while predicate: ((Element) -> Bool)? = nil
312+
) -> SubSequence where R.Bound == Self.Index {
313+
fatalError()
314+
}
315+
}
316+
317+
// https://github.com/apple/swift/issues/56393
318+
func keypathToFunctionWithOptional() {
319+
_ = Array("").prefix(1...4, while: \.isNumber) // Ok
320+
}

0 commit comments

Comments
 (0)