Skip to content

Fix solution application for keypath function subtype conversions #71737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
25 changes: 18 additions & 7 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5117,7 +5117,7 @@ namespace {
!componentTy->getWithoutSpecifierType()->isEqual(leafTy)) {
auto component = KeyPathExpr::Component::forOptionalWrap(leafTy);
resolvedComponents.push_back(component);
componentTy = leafTy;
componentTy = OptionalType::get(componentTy);
}

// Set the resolved components, and cache their types.
Expand All @@ -5133,13 +5133,23 @@ namespace {

// If we've gotten here, the user has used key path literal syntax to form
// a closure. The type checker has given E a function type to indicate
// this; we're going to change E's type to KeyPath<baseTy, leafTy> and
// then wrap it in a larger closure expression with the appropriate type.
// this.
//
// Since functions support more conversions than generic types, we may
// have ended up with a type of (baseTy) -> leafTy, where the actual type
// of the key path is some subclass of KeyPath<baseTy, componentTy>, and
// with componentTy: leafTy.
//
// We're going to change E's type to KeyPath<baseTy, componentTy> and
// then wrap it in a larger closure expression which we will convert to
// appropriate type.

auto kpResultTy = componentTy->getWithoutSpecifierType();

// Compute KeyPath<baseTy, leafTy> and set E's type back to it.
auto kpDecl = cs.getASTContext().getKeyPathDecl();
auto keyPathTy =
BoundGenericType::get(kpDecl, nullptr, { baseTy, leafTy });
BoundGenericType::get(kpDecl, nullptr, { baseTy, kpResultTy });
E->setType(keyPathTy);
cs.cacheType(E);

Expand All @@ -5154,9 +5164,10 @@ namespace {

FunctionType::ExtInfo closureInfo;
auto closureTy =
FunctionType::get({FunctionType::Param(baseTy)}, leafTy, closureInfo);
FunctionType::get({FunctionType::Param(baseTy)}, kpResultTy,
closureInfo);
auto closure = new (ctx)
AutoClosureExpr(/*set body later*/nullptr, leafTy, dc);
AutoClosureExpr(/*set body later*/nullptr, kpResultTy, dc);

auto param = new (ctx) ParamDecl(
SourceLoc(),
Expand Down Expand Up @@ -5208,7 +5219,7 @@ namespace {
auto *application = new (ctx)
KeyPathApplicationExpr(paramRef,
E->getStartLoc(), outerParamRef, E->getEndLoc(),
leafTy, /*implicit=*/true);
kpResultTy, /*implicit=*/true);
cs.cacheType(application);

// Finish up the inner closure.
Expand Down
15 changes: 15 additions & 0 deletions test/SILGen/keypaths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,18 @@ struct TestKeyPathWithSomeType : DefineSomeType {

}
}

// apple/swift#71423
protocol CodingKey {}

struct URICoderCodingKey : CodingKey {}

struct CodingStackEntry {
var key: URICoderCodingKey
}

struct Test {
var codingStack: [CodingStackEntry]
var codingPath: [any CodingKey] { codingStack.map(\.key) }
// CHECK: keypath $KeyPath<CodingStackEntry, URICoderCodingKey>, (root $CodingStackEntry; stored_property #CodingStackEntry.key : $URICoderCodingKey)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a test-case with optional promotion as well for completeness.

}