Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4732,9 +4732,21 @@ namespace {
KeyPathExpr::Component::forOptionalChain(objectTy, loc));
break;
}
case KeyPathExpr::Component::Kind::OptionalForce:
buildKeyPathOptionalForceComponent(resolvedComponents);
case KeyPathExpr::Component::Kind::OptionalForce: {
// Handle force optional when it is the first component e.g.
// \String?.!.count
if (resolvedComponents.empty()) {
auto loc = origComponent.getLoc();
auto objectTy = componentTy->getOptionalObjectType();
assert(objectTy);

resolvedComponents.push_back(
KeyPathExpr::Component::forOptionalForce(objectTy, loc));
} else {
buildKeyPathOptionalForceComponent(resolvedComponents);
}
break;
}
case KeyPathExpr::Component::Kind::Invalid: {
auto component = origComponent;
component.setComponentType(leafTy);
Expand Down
8 changes: 8 additions & 0 deletions test/Constraints/keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,11 @@ func key_path_root_mismatch<T>(_ base: KeyPathBase?, subBase: KeyPathBaseSubtype
let _ : T = subBase[keyPath: kpa] // expected-error {{key path with root type 'AnotherBase' cannot be applied to a base of type 'KeyPathBaseSubtype?'}}

}

// SR-13442
func SR13442<T>(_ x: KeyPath<String?, T>) -> T { "1"[keyPath: x] }

func testSR13442() {
_ = SR13442(\.!.count) // OK
_ = SR13442(\String?.!.count) // OK
}