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
3 changes: 2 additions & 1 deletion lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5116,7 +5116,8 @@ namespace {
!componentTy->getWithoutSpecifierType()->isEqual(leafTy)) {
auto component = KeyPathExpr::Component::forOptionalWrap(leafTy);
resolvedComponents.push_back(component);
componentTy = OptionalType::get(componentTy);
// Optional chaining forces the component to be r-value.
componentTy = OptionalType::get(componentTy->getWithoutSpecifierType());
}

// Set the resolved components, and cache their types.
Expand Down
20 changes: 20 additions & 0 deletions test/SILGen/keypaths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,23 @@ struct Test {
var codingPath: [any CodingKey] { codingStack.map(\.key) }
// CHECK: keypath $KeyPath<CodingStackEntry, URICoderCodingKey>, (root $CodingStackEntry; stored_property #CodingStackEntry.key : $URICoderCodingKey)
}

// rdar://123638701 - Make sure that optional chaining forces loads.
func test_optional_chaining_with_function_conversion() {
class Storage {}

class Elements {
var db: Storage = Storage()
}

class Source {
var elements: Elements? = nil
}

func test(data: [Source]) {
// CHECK: {{.*}} = keypath $KeyPath<Source, Optional<Storage>>
_ = data.compactMap(\.elements?.db)
// CHECK: {{.*}} = keypath $KeyPath<Source, Storage>
_ = data.compactMap(\.elements!.db)
}
}