From 9cd9619a57f757ecb8c917faaa49341d7ac252c2 Mon Sep 17 00:00:00 2001 From: Pavel Yaskevich Date: Thu, 7 Dec 2023 18:00:55 -0800 Subject: [PATCH] [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: https://github.com/apple/swift/issues/56393 --- lib/Sema/CSBindings.cpp | 5 +++-- test/Constraints/keypath.swift | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/Sema/CSBindings.cpp b/lib/Sema/CSBindings.cpp index 3974ef89fcc3e..80d0fe1ddfd78 100644 --- a/lib/Sema/CSBindings.cpp +++ b/lib/Sema/CSBindings.cpp @@ -634,8 +634,9 @@ void BindingSet::finalize( assert(isKnownKeyPathType(bindingTy) || bindingTy->is()); // Functions don't have capability so we can simply add them. - if (bindingTy->is()) - updatedBindings.insert(binding); + if (auto *fnType = bindingTy->getAs()) { + updatedBindings.insert(binding.withType(fnType)); + } } // Note that even though key path literal maybe be invalid it's diff --git a/test/Constraints/keypath.swift b/test/Constraints/keypath.swift index dd5e7c811b3ca..6f80de5250ba2 100644 --- a/test/Constraints/keypath.swift +++ b/test/Constraints/keypath.swift @@ -304,3 +304,17 @@ func test_invalid_argument_to_keypath_subscript() { // expected-error@-1 {{cannot use value of type 'A' as a key path subscript index; argument must be a key path}} } } + +extension Collection { + func prefix( + _ range: R, + while predicate: ((Element) -> Bool)? = nil + ) -> SubSequence where R.Bound == Self.Index { + fatalError() + } +} + +// https://github.com/apple/swift/issues/56393 +func keypathToFunctionWithOptional() { + _ = Array("").prefix(1...4, while: \.isNumber) // Ok +}