Skip to content

[Sema] Better key path failure diagnosis for unresolved roots #18329

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 2 commits into from
Aug 1, 2018
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
13 changes: 13 additions & 0 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7098,6 +7098,19 @@ static bool diagnoseKeyPathComponents(ConstraintSystem &CS, KeyPathExpr *KPE,
Type rootType) {
auto &TC = CS.TC;

// The constraint system may have been unable to resolve the actual root
// type. The generic interface type of the root produces better
// diagnostics in this case.
if (rootType->hasUnresolvedType() && !KPE->isObjC() && KPE->getRootType()) {
if (auto ident = dyn_cast<ComponentIdentTypeRepr>(KPE->getRootType())) {
if (auto decl = ident->getBoundDecl()) {
if (auto metaType = decl->getInterfaceType()->castTo<MetatypeType>()) {
rootType = metaType->getInstanceType();
}
}
}
}

// The key path string we're forming.
SmallString<32> keyPathScratch;
llvm::raw_svector_ostream keyPathOS(keyPathScratch);
Expand Down
15 changes: 15 additions & 0 deletions test/Constraints/keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ func test() {

let _ = C()[keyPath: \.i] // no warning for a read
}

// SR-7339
class Some<T, V> {
init(keyPath: KeyPath<T, ((V) -> Void)?>) {
}
}

class Demo {
var here: (() -> Void)?
}

// FIXME: This error is better than it was, but the diagnosis should break it down more specifically to 'here's type.
let some = Some(keyPath: \Demo.here)
// expected-error@-1 {{cannot convert value of type 'ReferenceWritableKeyPath<Demo, (() -> Void)?>' to expected argument type 'KeyPath<_, ((_) -> Void)?>'}}