[ConstraintSystem] Key path literals with completion tokens should be marked as invalid

Capability couldn't be determined for expressions like that which
means that inference should be delayed until root becomes available.

Resolves: https://github.com/apple/swift/issues/69936
This commit is contained in:
Pavel Yaskevich
2023-11-16 15:46:51 -08:00
parent 23effda44d
commit 72bb74aa4f
3 changed files with 16 additions and 3 deletions

View File

@@ -641,6 +641,7 @@ void BindingSet::finalize(
updatedBindings.insert(
{keyPathTy, AllowedBindingKind::Exact, keyPathLoc});
} else if (CS.shouldAttemptFixes()) {
auto fixedRootTy = CS.getFixedType(rootTy);
// If key path is structurally correct and has a resolved root
// type, let's promote the fallback type into a binding because
// root would have been inferred from explicit type already and
@@ -648,7 +649,7 @@ void BindingSet::finalize(
// type to key path literal to propagate root/value to the context.
if (!keyPath->hasSingleInvalidComponent() &&
(keyPath->getParsedRoot() ||
!CS.getFixedType(rootTy)->isTypeVariableOrMember())) {
(fixedRootTy && !fixedRootTy->isTypeVariableOrMember()))) {
auto fallback = llvm::find_if(Defaults, [](const auto &entry) {
return entry.second->getKind() == ConstraintKind::FallbackType;
});

View File

@@ -7469,8 +7469,7 @@ ConstraintSystem::inferKeyPathLiteralCapability(KeyPathExpr *keyPath) {
break;
case KeyPathExpr::Component::Kind::CodeCompletion: {
capability = KeyPathCapability::ReadOnly;
break;
return fail();
}
case KeyPathExpr::Component::Kind::Property:
case KeyPathExpr::Component::Kind::Subscript:

View File

@@ -0,0 +1,13 @@
// RUN: %batch-code-completion
struct Test {
var currentHeight: Int
func subscribeToKeyboardEvents() {
objectAndKeyPath(object: self, keyPath: \.#^COMPLETE^#)
}
}
func objectAndKeyPath<Root, Input>(object: Root, keyPath: WritableKeyPath<Root, Input>) {}
// COMPLETE: Decl[InstanceVar]/CurrNominal: currentHeight[#Int#]; name=currentHeight