Skip to content

Commit 5e7da0e

Browse files
authored
Merge pull request #18329 from gregomni/7339
[Sema] Better key path failure diagnosis for unresolved roots
2 parents 501a5ef + d52d46d commit 5e7da0e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7102,6 +7102,19 @@ static bool diagnoseKeyPathComponents(ConstraintSystem &CS, KeyPathExpr *KPE,
71027102
Type rootType) {
71037103
auto &TC = CS.TC;
71047104

7105+
// The constraint system may have been unable to resolve the actual root
7106+
// type. The generic interface type of the root produces better
7107+
// diagnostics in this case.
7108+
if (rootType->hasUnresolvedType() && !KPE->isObjC() && KPE->getRootType()) {
7109+
if (auto ident = dyn_cast<ComponentIdentTypeRepr>(KPE->getRootType())) {
7110+
if (auto decl = ident->getBoundDecl()) {
7111+
if (auto metaType = decl->getInterfaceType()->castTo<MetatypeType>()) {
7112+
rootType = metaType->getInstanceType();
7113+
}
7114+
}
7115+
}
7116+
}
7117+
71057118
// The key path string we're forming.
71067119
SmallString<32> keyPathScratch;
71077120
llvm::raw_svector_ostream keyPathOS(keyPathScratch);

test/Constraints/keypath.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,18 @@ func test() {
1919

2020
let _ = C()[keyPath: \.i] // no warning for a read
2121
}
22+
23+
// SR-7339
24+
class Some<T, V> {
25+
init(keyPath: KeyPath<T, ((V) -> Void)?>) {
26+
}
27+
}
28+
29+
class Demo {
30+
var here: (() -> Void)?
31+
}
32+
33+
// FIXME: This error is better than it was, but the diagnosis should break it down more specifically to 'here's type.
34+
let some = Some(keyPath: \Demo.here)
35+
// expected-error@-1 {{cannot convert value of type 'ReferenceWritableKeyPath<Demo, (() -> Void)?>' to expected argument type 'KeyPath<_, ((_) -> Void)?>'}}
36+

0 commit comments

Comments
 (0)