Skip to content

Commit 72bb74a

Browse files
committed
[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: #69936
1 parent 23effda commit 72bb74a

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,14 +641,15 @@ void BindingSet::finalize(
641641
updatedBindings.insert(
642642
{keyPathTy, AllowedBindingKind::Exact, keyPathLoc});
643643
} else if (CS.shouldAttemptFixes()) {
644+
auto fixedRootTy = CS.getFixedType(rootTy);
644645
// If key path is structurally correct and has a resolved root
645646
// type, let's promote the fallback type into a binding because
646647
// root would have been inferred from explicit type already and
647648
// it's benefitial for diagnostics to assign a non-placeholder
648649
// type to key path literal to propagate root/value to the context.
649650
if (!keyPath->hasSingleInvalidComponent() &&
650651
(keyPath->getParsedRoot() ||
651-
!CS.getFixedType(rootTy)->isTypeVariableOrMember())) {
652+
(fixedRootTy && !fixedRootTy->isTypeVariableOrMember()))) {
652653
auto fallback = llvm::find_if(Defaults, [](const auto &entry) {
653654
return entry.second->getKind() == ConstraintKind::FallbackType;
654655
});

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7469,8 +7469,7 @@ ConstraintSystem::inferKeyPathLiteralCapability(KeyPathExpr *keyPath) {
74697469
break;
74707470

74717471
case KeyPathExpr::Component::Kind::CodeCompletion: {
7472-
capability = KeyPathCapability::ReadOnly;
7473-
break;
7472+
return fail();
74747473
}
74757474
case KeyPathExpr::Component::Kind::Property:
74767475
case KeyPathExpr::Component::Kind::Subscript:

test/IDE/complete_issue-69936.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %batch-code-completion
2+
3+
struct Test {
4+
var currentHeight: Int
5+
6+
func subscribeToKeyboardEvents() {
7+
objectAndKeyPath(object: self, keyPath: \.#^COMPLETE^#)
8+
}
9+
}
10+
11+
func objectAndKeyPath<Root, Input>(object: Root, keyPath: WritableKeyPath<Root, Input>) {}
12+
13+
// COMPLETE: Decl[InstanceVar]/CurrNominal: currentHeight[#Int#]; name=currentHeight

0 commit comments

Comments
 (0)