Skip to content

Commit

Permalink
[CSBindings] Unwrap optionals from contextual function type used as a…
Browse files Browse the repository at this point in the history
… key path type

The key path is going to be implicitly wrapped into a contextual
optional type.

Resolves: rdar://72864716
Resolves: apple#56393
  • Loading branch information
xedin committed Dec 8, 2023
1 parent 28e6369 commit 9cd9619
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/Sema/CSBindings.cpp
Expand Up @@ -634,8 +634,9 @@ void BindingSet::finalize(
assert(isKnownKeyPathType(bindingTy) || bindingTy->is<FunctionType>());

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

// Note that even though key path literal maybe be invalid it's
Expand Down
14 changes: 14 additions & 0 deletions test/Constraints/keypath.swift
Expand Up @@ -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<R: RangeExpression>(
_ 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
}

0 comments on commit 9cd9619

Please sign in to comment.