Skip to content

Commit 9bc76a7

Browse files
authored
Merge pull request #15903 from rudkx/keypath-fixes
Opportunistic fix to allow \Derived.property to be inferred as \Base.…
2 parents 02aef12 + 9a7497a commit 9bc76a7

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/Sema/CSGen.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2828,8 +2828,10 @@ namespace {
28282828
if (!rootObjectTy || rootObjectTy->hasError())
28292829
return Type();
28302830
rootObjectTy = CS.openUnboundGenericType(rootObjectTy, locator);
2831-
CS.addConstraint(ConstraintKind::Bind, root, rootObjectTy,
2832-
locator);
2831+
// Allow \Derived.property to be inferred as \Base.property to
2832+
// simulate a sort of covariant conversion from
2833+
// KeyPath<Derived, T> to KeyPath<Base, T>.
2834+
CS.addConstraint(ConstraintKind::Subtype, rootObjectTy, root, locator);
28332835
}
28342836

28352837
bool didOptionalChain = false;

test/expr/unary/keypath/keypath.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,18 @@ struct VisibilityTesting2 {
545545
}
546546
}
547547

548+
protocol PP {}
549+
class Base : PP { var i: Int = 0 }
550+
class Derived : Base {}
551+
552+
func testSubtypeKeypathClass(_ keyPath: ReferenceWritableKeyPath<Base, Int>) {
553+
testSubtypeKeypathClass(\Derived.i)
554+
}
555+
556+
func testSubtypeKeypathProtocol(_ keyPath: ReferenceWritableKeyPath<PP, Int>) {
557+
testSubtypeKeypathProtocol(\Base.i) // expected-error {{type 'PP' has no member 'i'}}
558+
}
559+
548560
func testSyntaxErrors() { // expected-note{{}}
549561
_ = \. ; // expected-error{{expected member name following '.'}}
550562
_ = \.a ;

0 commit comments

Comments
 (0)