Skip to content

Commit f97d68f

Browse files
authored
Merge pull request #33290 from apple/revert-33245-index-key-paths
Revert "[Sema/Index] Resolve #keyPath components so they get handled by indexing, semantic highlighting, etc."
2 parents 78f0259 + 00e4a76 commit f97d68f

16 files changed

+25
-160
lines changed

include/swift/AST/Expr.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5259,7 +5259,6 @@ class KeyPathExpr : public Expr {
52595259
OptionalWrap,
52605260
Identity,
52615261
TupleElement,
5262-
DictionaryKey,
52635262
};
52645263

52655264
private:
@@ -5368,16 +5367,6 @@ class KeyPathExpr : public Expr {
53685367
propertyType,
53695368
loc);
53705369
}
5371-
5372-
/// Create a component for a dictionary key (#keyPath only).
5373-
static Component forDictionaryKey(DeclNameRef UnresolvedName,
5374-
Type valueType,
5375-
SourceLoc loc) {
5376-
return Component(nullptr, UnresolvedName, nullptr, {}, {},
5377-
Kind::DictionaryKey,
5378-
valueType,
5379-
loc);
5380-
}
53815370

53825371
/// Create a component for a subscript.
53835372
static Component forSubscript(ASTContext &ctx,
@@ -5468,7 +5457,6 @@ class KeyPathExpr : public Expr {
54685457
case Kind::Property:
54695458
case Kind::Identity:
54705459
case Kind::TupleElement:
5471-
case Kind::DictionaryKey:
54725460
return true;
54735461

54745462
case Kind::UnresolvedSubscript:
@@ -5493,7 +5481,6 @@ class KeyPathExpr : public Expr {
54935481
case Kind::Property:
54945482
case Kind::Identity:
54955483
case Kind::TupleElement:
5496-
case Kind::DictionaryKey:
54975484
return nullptr;
54985485
}
54995486
llvm_unreachable("unhandled kind");
@@ -5513,7 +5500,6 @@ class KeyPathExpr : public Expr {
55135500
case Kind::Property:
55145501
case Kind::Identity:
55155502
case Kind::TupleElement:
5516-
case Kind::DictionaryKey:
55175503
llvm_unreachable("no subscript labels for this kind");
55185504
}
55195505
llvm_unreachable("unhandled kind");
@@ -5536,7 +5522,6 @@ class KeyPathExpr : public Expr {
55365522
case Kind::Property:
55375523
case Kind::Identity:
55385524
case Kind::TupleElement:
5539-
case Kind::DictionaryKey:
55405525
return {};
55415526
}
55425527
llvm_unreachable("unhandled kind");
@@ -5548,7 +5533,6 @@ class KeyPathExpr : public Expr {
55485533
DeclNameRef getUnresolvedDeclName() const {
55495534
switch (getKind()) {
55505535
case Kind::UnresolvedProperty:
5551-
case Kind::DictionaryKey:
55525536
return Decl.UnresolvedName;
55535537

55545538
case Kind::Invalid:
@@ -5579,7 +5563,6 @@ class KeyPathExpr : public Expr {
55795563
case Kind::OptionalForce:
55805564
case Kind::Identity:
55815565
case Kind::TupleElement:
5582-
case Kind::DictionaryKey:
55835566
llvm_unreachable("no decl ref for this kind");
55845567
}
55855568
llvm_unreachable("unhandled kind");
@@ -5599,7 +5582,6 @@ class KeyPathExpr : public Expr {
55995582
case Kind::Identity:
56005583
case Kind::Property:
56015584
case Kind::Subscript:
5602-
case Kind::DictionaryKey:
56035585
llvm_unreachable("no field number for this kind");
56045586
}
56055587
llvm_unreachable("unhandled kind");

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,11 +2829,6 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
28292829
PrintWithColorRAII(OS, DiscriminatorColor)
28302830
<< "#" << component.getTupleIndex();
28312831
break;
2832-
case KeyPathExpr::Component::Kind::DictionaryKey:
2833-
PrintWithColorRAII(OS, ASTNodeColor) << "dict_key";
2834-
PrintWithColorRAII(OS, IdentifierColor)
2835-
<< " key='" << component.getUnresolvedDeclName() << "'";
2836-
break;
28372832
}
28382833
PrintWithColorRAII(OS, TypeColor)
28392834
<< " type='" << GetTypeOfKeyPathComponent(E, i) << "'";

lib/AST/ASTWalker.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
11331133
case KeyPathExpr::Component::Kind::Invalid:
11341134
case KeyPathExpr::Component::Kind::Identity:
11351135
case KeyPathExpr::Component::Kind::TupleElement:
1136-
case KeyPathExpr::Component::Kind::DictionaryKey:
11371136
// No subexpr to visit.
11381137
break;
11391138
}

lib/AST/Expr.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2383,7 +2383,6 @@ void KeyPathExpr::Component::setSubscriptIndexHashableConformances(
23832383
case Kind::Property:
23842384
case Kind::Identity:
23852385
case Kind::TupleElement:
2386-
case Kind::DictionaryKey:
23872386
llvm_unreachable("no hashable conformances for this kind");
23882387
}
23892388
}

lib/IDE/SourceEntityWalker.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
412412
case KeyPathExpr::Component::Kind::OptionalWrap:
413413
case KeyPathExpr::Component::Kind::OptionalForce:
414414
case KeyPathExpr::Component::Kind::Identity:
415-
case KeyPathExpr::Component::Kind::DictionaryKey:
416415
break;
417416
}
418417
}

lib/SILGen/SILGenExpr.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3734,11 +3734,6 @@ RValue RValueEmitter::visitKeyPathExpr(KeyPathExpr *E, SGFContext C) {
37343734
case KeyPathExpr::Component::Kind::UnresolvedProperty:
37353735
case KeyPathExpr::Component::Kind::UnresolvedSubscript:
37363736
llvm_unreachable("not resolved");
3737-
break;
3738-
3739-
case KeyPathExpr::Component::Kind::DictionaryKey:
3740-
llvm_unreachable("DictionaryKey only valid in #keyPath");
3741-
break;
37423737
}
37433738
}
37443739

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,6 @@ static bool buildObjCKeyPathString(KeyPathExpr *E,
237237
// Don't bother building the key path string if the key path didn't even
238238
// resolve.
239239
return false;
240-
case KeyPathExpr::Component::Kind::DictionaryKey:
241-
llvm_unreachable("DictionaryKey only valid in #keyPath expressions.");
242-
return false;
243240
}
244241
}
245242

@@ -4693,10 +4690,6 @@ namespace {
46934690
case KeyPathExpr::Component::Kind::OptionalWrap:
46944691
case KeyPathExpr::Component::Kind::TupleElement:
46954692
llvm_unreachable("already resolved");
4696-
break;
4697-
case KeyPathExpr::Component::Kind::DictionaryKey:
4698-
llvm_unreachable("DictionaryKey only valid in #keyPath");
4699-
break;
47004693
}
47014694

47024695
// Update "componentTy" with the result type of the last component.
@@ -7752,8 +7745,9 @@ namespace {
77527745
componentType = solution.simplifyType(cs.getType(kp, i));
77537746
assert(!componentType->hasTypeVariable() &&
77547747
"Should not write type variable into key-path component");
7755-
kp->getMutableComponents()[i].setComponentType(componentType);
77567748
}
7749+
7750+
kp->getMutableComponents()[i].setComponentType(componentType);
77577751
}
77587752
}
77597753

lib/Sema/CSGen.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,9 +3485,6 @@ namespace {
34853485
}
34863486
case KeyPathExpr::Component::Kind::Identity:
34873487
continue;
3488-
case KeyPathExpr::Component::Kind::DictionaryKey:
3489-
llvm_unreachable("DictionaryKey only valid in #keyPath");
3490-
break;
34913488
}
34923489

34933490
// By now, `base` is the result type of this component. Set it in the

lib/Sema/CSSimplify.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8236,9 +8236,6 @@ ConstraintSystem::simplifyKeyPathConstraint(
82368236
case KeyPathExpr::Component::Kind::TupleElement:
82378237
llvm_unreachable("not implemented");
82388238
break;
8239-
case KeyPathExpr::Component::Kind::DictionaryKey:
8240-
llvm_unreachable("DictionaryKey only valid in #keyPath");
8241-
break;
82428239
}
82438240
}
82448241

lib/Sema/ConstraintSystem.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,6 @@ ConstraintLocator *ConstraintSystem::getCalleeLocator(
493493
case ComponentKind::OptionalChain:
494494
case ComponentKind::OptionalWrap:
495495
case ComponentKind::Identity:
496-
case ComponentKind::DictionaryKey:
497496
// These components don't have any callee associated, so just continue.
498497
break;
499498
}

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2522,7 +2522,6 @@ class AvailabilityWalker : public ASTWalker {
25222522
case KeyPathExpr::Component::Kind::OptionalWrap:
25232523
case KeyPathExpr::Component::Kind::OptionalForce:
25242524
case KeyPathExpr::Component::Kind::Identity:
2525-
case KeyPathExpr::Component::Kind::DictionaryKey:
25262525
break;
25272526
}
25282527
}

lib/Sema/TypeCheckCodeCompletion.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -621,21 +621,9 @@ static Optional<Type> getTypeOfCompletionContextExpr(
621621

622622
case CompletionTypeCheckKind::KeyPath:
623623
referencedDecl = nullptr;
624-
if (auto keyPath = dyn_cast<KeyPathExpr>(parsedExpr)) {
625-
auto components = keyPath->getComponents();
626-
if (!components.empty()) {
627-
auto &last = components.back();
628-
if (last.isResolved()) {
629-
if (last.getKind() == KeyPathExpr::Component::Kind::Property)
630-
referencedDecl = last.getDeclRef();
631-
Type lookupTy = last.getComponentType();
632-
ASTContext &Ctx = DC->getASTContext();
633-
if (auto bridgedClass = Ctx.getBridgedToObjC(DC, lookupTy))
634-
return bridgedClass;
635-
return lookupTy;
636-
}
637-
}
638-
}
624+
if (auto keyPath = dyn_cast<KeyPathExpr>(parsedExpr))
625+
return TypeChecker::checkObjCKeyPathExpr(DC, keyPath,
626+
/*requireResultType=*/true);
639627

640628
return None;
641629
}

lib/Sema/TypeCheckExprObjC.cpp

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
221221
case KeyPathExpr::Component::Kind::OptionalWrap:
222222
case KeyPathExpr::Component::Kind::Property:
223223
case KeyPathExpr::Component::Kind::Subscript:
224-
case KeyPathExpr::Component::Kind::DictionaryKey:
225224
llvm_unreachable("already resolved!");
226225
}
227226

@@ -242,9 +241,6 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
242241
// From here, we're resolving a property. Use the current type.
243242
updateState(/*isProperty=*/true, currentType);
244243

245-
auto resolved = KeyPathExpr::Component::
246-
forDictionaryKey(componentName, currentType, componentNameLoc);
247-
resolvedComponents.push_back(resolved);
248244
continue;
249245
}
250246

@@ -325,14 +321,10 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
325321
if (auto var = dyn_cast<VarDecl>(found)) {
326322
// Resolve this component to the variable we found.
327323
auto varRef = ConcreteDeclRef(var);
328-
Type varTy = var->getInterfaceType();
329-
330-
// Updates currentType
331-
updateState(/*isProperty=*/true, varTy);
332-
333-
auto resolved = KeyPathExpr::Component::forProperty(varRef, currentType,
334-
componentNameLoc);
324+
auto resolved =
325+
KeyPathExpr::Component::forProperty(varRef, Type(), componentNameLoc);
335326
resolvedComponents.push_back(resolved);
327+
updateState(/*isProperty=*/true, var->getInterfaceType());
336328

337329
// Check that the property is @objc.
338330
if (!var->isObjC()) {
@@ -398,15 +390,7 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
398390
break;
399391
}
400392

401-
// Updates currentType based on newType.
402393
updateState(/*isProperty=*/false, newType);
403-
404-
// Resolve this component to the type we found.
405-
auto typeRef = ConcreteDeclRef(type);
406-
auto resolved = KeyPathExpr::Component::forProperty(typeRef, currentType,
407-
componentNameLoc);
408-
resolvedComponents.push_back(resolved);
409-
410394
continue;
411395
}
412396

test/IDE/complete_pound_keypath.swift

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@
66

77
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -code-completion -source-filename %s -code-completion-token=IN_KEYPATH_2 | %FileCheck -check-prefix=CHECK-IN_KEYPATH %s
88

9-
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -code-completion -source-filename %s -code-completion-token=IN_KEYPATH_3 | %FileCheck -check-prefix=CHECK-IN_KEYPATH_BRIDGED_STRING %s
10-
11-
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -code-completion -source-filename %s -code-completion-token=IN_KEYPATH_4 | %FileCheck -check-prefix=CHECK-IN_KEYPATH_BRIDGED_STRING %s
12-
13-
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -code-completion -source-filename %s -code-completion-token=IN_KEYPATH_5 | %FileCheck -check-prefixes=CHECK-IN_KEYPATH,CHECK-IN_KEYPATH_OPT %s
14-
15-
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -code-completion -source-filename %s -code-completion-token=IN_KEYPATH_6 | %FileCheck -check-prefixes=CHECK-IN_KEYPATH,CHECK-IN_KEYPATH_OPT %s
16-
17-
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -code-completion -source-filename %s -code-completion-token=IN_KEYPATH_7 | %FileCheck -check-prefixes=CHECK-IN_KEYPATH_BRIDGED_STRING %s
18-
199

2010
// REQUIRES: objc_interop
2111

@@ -31,11 +21,9 @@ func selectorArg1(obj: NSObject) {
3121
acceptKeyPath(#^KEYPATH_ARG^#
3222
}
3323

34-
@objcMembers class ObjCClass : NSObject {
24+
class ObjCClass : NSObject {
3525
var prop1: String = ""
3626
var prop2: ObjCClass?
37-
var prop3: [ObjCClass]? = []
38-
var prop4: [String: String] = [:]
3927

4028
func completeInKeyPath1() {
4129
_ = #keyPath(#^IN_KEYPATH_1^#
@@ -46,42 +34,12 @@ func completeInKeyPath2() {
4634
_ = #keyPath(ObjCClass.#^IN_KEYPATH_2^#
4735
}
4836

49-
func completeInKeyPath3() {
50-
_ = #keyPath(ObjCClass.prop1.#^IN_KEYPATH_3^#
51-
}
52-
func completeInKeyPath3() {
53-
_ = #keyPath(String.#^IN_KEYPATH_4^#
54-
}
55-
56-
func completeInKeyPath4() {
57-
_ = #keyPath(ObjCClass.prop2.#^IN_KEYPATH_5^#
58-
}
59-
60-
func completeInKeyPath5() {
61-
_ = #keyPath(ObjCClass.prop3.#^IN_KEYPATH_6^#
62-
}
63-
64-
func completeInKeyPath6() {
65-
_ = #keyPath(ObjCClass.prop4.anythingHere.#^IN_KEYPATH_7^#
66-
}
67-
6837
// CHECK-AFTER_POUND-NOT: keyPath
6938

7039
// CHECK-KEYPATH_ARG: Keyword/None/TypeRelation[Identical]: #keyPath({#@objc property sequence#})[#String#]; name=#keyPath(@objc property sequence)
7140

7241
// CHECK-IN_KEYPATH: Decl[InstanceVar]/CurrNominal: prop1[#String#]; name=prop1
7342
// CHECK-IN_KEYPATH: Decl[InstanceVar]/CurrNominal: prop2[#ObjCClass?#]; name=prop2
74-
// CHECK-IN_KEYPATH: Decl[InstanceVar]/CurrNominal: prop3[#[ObjCClass]?#]; name=prop3
7543
// CHECK-IN_KEYPATH: Decl[InstanceVar]/Super: hashValue[#Int#]; name=hashValue
7644

77-
// Make sure we unwrap optionals (members of Optional itself are invalid in this context)
78-
//
79-
// CHECK-IN_KEYPATH_OPT-NOT: name=map
80-
81-
// Make sure we handle bridged types (i.e. show NSString members rather than String members)
82-
//
83-
// CHECK-IN_KEYPATH_BRIDGED_STRING: Decl[InstanceVar]/CurrNominal/IsSystem: urlsInText[#[URL]#]; name=urlsInText
84-
// CHECK-IN_KEYPATH_BRIDGED_STRING: Decl[InstanceVar]/CurrNominal/IsSystem: uppercased[#String!#]; name=uppercased
85-
// CHECK-IN_KEYPATH_BRIDGED_STRING-NOT: name=count
86-
8745

0 commit comments

Comments
 (0)