Skip to content

Commit 0224d40

Browse files
committed
[index] Add indexing of references in keypath member lookup expressions
Currently includes both the implicit references to the subscript(dynamicMember:) and the explicit references to the underlying property/subscript declarations. rdar://49028783
1 parent 983146e commit 0224d40

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

Diff for: lib/IDE/SourceEntityWalker.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
265265
!isa<MakeTemporarilyEscapableExpr>(E) &&
266266
!isa<CollectionUpcastConversionExpr>(E) &&
267267
!isa<OpaqueValueExpr>(E) &&
268+
!isa<KeyPathExpr>(E) &&
268269
E->isImplicit())
269270
return { true, E };
270271

Diff for: test/Index/index_keypath_member_lookup.swift

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s | %FileCheck %s
2+
3+
struct Point {
4+
// CHECK: [[@LINE-1]]:8 | struct/Swift | Point | {{.*}} | Def | rel: 0
5+
var x: Int
6+
// CHECK: [[@LINE-1]]:7 | instance-property/Swift | x | [[PX_USR:s:.*]] | Def,RelChild | rel: 1
7+
// CHECK: [[@LINE-2]]:7 | instance-method/acc-get/Swift | getter:x | [[PX_GET_USR:s:.*]] | Def,Impl,RelChild,RelAcc | rel: 1
8+
// CHECK: [[@LINE-3]]:7 | instance-method/acc-set/Swift | setter:x | [[PX_SET_USR:s:.*]] | Def,Impl,RelChild,RelAcc | rel: 1
9+
var y: Int
10+
// CHECK: [[@LINE-1]]:7 | instance-property/Swift | y | [[PY_USR:s:.*]] | Def,RelChild | rel: 1
11+
// CHECK: [[@LINE-2]]:7 | instance-method/acc-get/Swift | getter:y | [[PY_GET_USR:s:.*]] | Def,Impl,RelChild,RelAcc | rel: 1
12+
// CHECK: [[@LINE-3]]:7 | instance-method/acc-set/Swift | setter:y | [[PY_SET_USR:s:.*]] | Def,Impl,RelChild,RelAcc | rel: 1
13+
}
14+
15+
struct Rectangle {
16+
// CHECK: [[@LINE-1]]:8 | struct/Swift | Rectangle | {{.*}} | Def | rel: 0
17+
var topLeft: Point
18+
// CHECK: [[@LINE-1]]:7 | instance-property/Swift | topLeft | [[TL_USR:s:.*]] | Def,RelChild | rel: 1
19+
// CHECK: [[@LINE-2]]:7 | instance-method/acc-get/Swift | getter:topLeft | [[TL_GET_USR:s:.*]] | Def,Impl,RelChild,RelAcc | rel: 1
20+
// CHECK: [[@LINE-3]]:7 | instance-method/acc-set/Swift | setter:topLeft | [[TL_SET_USR:s:.*]] | Def,Impl,RelChild,RelAcc | rel: 1
21+
var bottomRight: Point
22+
// CHECK: [[@LINE-1]]:7 | instance-property/Swift | bottomRight | [[BR_USR:s:.*]] | Def,RelChild | rel: 1
23+
// CHECK: [[@LINE-2]]:7 | instance-method/acc-get/Swift | getter:bottomRight | [[BR_GET_USR:s:.*]] | Def,Impl,RelChild,RelAcc | rel: 1
24+
// CHECK: [[@LINE-3]]:7 | instance-method/acc-set/Swift | setter:bottomRight | [[BR_SET_USR:s:.*]] | Def,Impl,RelChild,RelAcc | rel: 1
25+
}
26+
27+
@dynamicMemberLookup
28+
struct Lens<T> {
29+
// CHECK: [[@LINE-1]]:8 | struct/Swift | Lens | {{.*}} | Def | rel: 0
30+
var obj: T
31+
32+
init(_ obj: T) {
33+
self.obj = obj
34+
}
35+
36+
subscript<U>(dynamicMember member: WritableKeyPath<T, U>) -> Lens<U> {
37+
// CHECK: [[@LINE-1]]:3 | instance-property/subscript/Swift | subscript(dynamicMember:) | [[SUB_USR:s:.*]] | Def,RelChild | rel: 1
38+
get { return Lens<U>(obj[keyPath: member]) }
39+
// CHECK: [[@LINE-1]]:5 | instance-method/acc-get/Swift | getter:subscript(dynamicMember:) | [[SUB_GET_USR:s:.*]] | Def,RelChild,RelAcc | rel: 1
40+
set { obj[keyPath: member] = newValue.obj }
41+
// CHECK: [[@LINE-1]]:5 | instance-method/acc-set/Swift | setter:subscript(dynamicMember:) | [[SUB_SET_USR:s:.*]] | Def,RelChild,RelAcc | rel: 1
42+
}
43+
}
44+
45+
46+
func testRead1(r: Lens<Rectangle>, a: Lens<[Int]>) {
47+
_ = r.topLeft
48+
// CHECK: [[TL_LINE:[0-9]+]]:7 | param/Swift | r
49+
50+
// => implicit dynamicMember subscript (topLeft)
51+
// CHECK: [[TL_LINE]]:9 | instance-property/subscript/Swift | subscript(dynamicMember:) | [[SUB_USR]] | Ref,Read,RelCont | rel: 1
52+
// CHECK: [[TL_LINE]]:9 | instance-method/acc-get/Swift | getter:subscript(dynamicMember:) | [[SUB_GET_USR]] | Ref,Call,Impl,RelRec,RelCall,RelCont | rel: 2
53+
54+
// => property topLeft
55+
// CHECK: [[TL_LINE]]:9 | instance-property/Swift | topLeft | [[TL_USR]] | Ref,Read,RelCont | rel: 1
56+
// CHECK: [[TL_LINE]]:9 | instance-method/acc-get/Swift | getter:topLeft | [[TL_GET_USR]] | Ref,Call,Impl,RelCall,RelCont | rel: 1
57+
58+
_ = r.bottomRight.y
59+
// CHECK: [[BR_LINE:[0-9]+]]:7 | param/Swift | r
60+
61+
// => implicit dynamicMember subscript (bottomRight)
62+
// CHECK: [[BR_LINE]]:9 | instance-property/subscript/Swift | subscript(dynamicMember:) | [[SUB_USR]] | Ref,Read,RelCont | rel: 1
63+
// CHECK: [[BR_LINE]]:9 | instance-method/acc-get/Swift | getter:subscript(dynamicMember:) | [[SUB_GET_USR]] | Ref,Call,Impl,RelRec,RelCall,RelCont | rel: 2
64+
65+
// => property bottomRight
66+
// CHECK: [[BR_LINE]]:9 | instance-property/Swift | bottomRight | [[BR_USR]] | Ref,Read,RelCont | rel: 1
67+
// CHECK: [[BR_LINE]]:9 | instance-method/acc-get/Swift | getter:bottomRight | [[BR_GET_USR]] | Ref,Call,Impl,RelCall,RelCont | rel: 1
68+
69+
// => implicit dynamicMember subscript (y)
70+
// CHECK: [[BR_LINE]]:21 | instance-property/subscript/Swift | subscript(dynamicMember:) | [[SUB_USR]] | Ref,Read,RelCont | rel: 1
71+
// CHECK: [[BR_LINE]]:21 | instance-method/acc-get/Swift | getter:subscript(dynamicMember:) | [[SUB_GET_USR]] | Ref,Call,Impl,RelRec,RelCall,RelCont | rel: 2
72+
73+
// => property y
74+
// CHECK: [[BR_LINE]]:21 | instance-property/Swift | y | [[PY_USR]] | Ref,Read,RelCont | rel: 1
75+
// CHECK: [[BR_LINE]]:21 | instance-method/acc-get/Swift | getter:y | [[PY_GET_USR]] | Ref,Call,Impl,RelCall,RelCont | rel: 1
76+
77+
_ = a[0]
78+
// CHECK: [[A_LINE:[0-9]+]]:7 | param/Swift | a
79+
80+
// => implicit dynamicMember subscript
81+
// CHECK: [[A_LINE]]:8 | instance-property/subscript/Swift | subscript(dynamicMember:) | [[SUB_USR]] | Ref,Read,RelCont | rel: 1
82+
// CHECK: [[A_LINE]]:8 | instance-method/acc-get/Swift | getter:subscript(dynamicMember:) | [[SUB_GET_USR]] | Ref,Call,Impl,RelRec,RelCall,RelCont | rel: 2
83+
84+
// => subscript [Int]
85+
// CHECK: [[A_LINE]]:8 | instance-property/subscript/Swift | subscript(_:) | s:SayxSicip | Ref,Read,RelCont | rel: 1
86+
// CHECK: [[A_LINE]]:8 | instance-method/acc-get/Swift | getter:subscript(_:) | s:SayxSicig | Ref,Call,Impl,RelCall,RelCont | rel: 1
87+
}
88+
89+
90+
func testWrite1(r: inout Lens<Rectangle>, p: Lens<Point>, a: inout Lens<[Int]>) {
91+
r.topLeft = p
92+
// CHECK: [[WTL_LINE:[0-9]+]]:3 | param/Swift | r
93+
94+
// => implicit dynamicMember subscript (topLeft)
95+
// CHECK: [[WTL_LINE]]:5 | instance-property/subscript/Swift | subscript(dynamicMember:) | [[SUB_USR]] | Ref,Writ,RelCont | rel: 1
96+
// CHECK: [[WTL_LINE]]:5 | instance-method/acc-set/Swift | setter:subscript(dynamicMember:) | [[SUB_SET_USR]] | Ref,Call,Impl,RelCall,RelCont | rel: 1
97+
98+
// => property topLeft
99+
// CHECK: [[WTL_LINE]]:5 | instance-property/Swift | topLeft | [[TL_USR]] | Ref,Writ,RelCont | rel: 1
100+
// CHECK: [[WTL_LINE]]:5 | instance-method/acc-set/Swift | setter:topLeft | [[TL_SET_USR]] | Ref,Call,Impl,RelCall,RelCont | rel: 1
101+
102+
r.bottomRight.y = Lens(3)
103+
// CHECK: [[WBR_LINE:[0-9]+]]:3 | param/Swift | r
104+
105+
// => implicit dynamicMember subscript (bottomRight)
106+
// CHECK: [[WBR_LINE:[0-9]+]]:5 | instance-property/subscript/Swift | subscript(dynamicMember:) | [[SUB_USR]] | Ref,Read,Writ,RelCont | rel: 1
107+
// CHECK: [[WBR_LINE:[0-9]+]]:5 | instance-method/acc-get/Swift | getter:subscript(dynamicMember:) | [[SUB_GET_USR]] | Ref,Call,Impl,RelCall,RelCont | rel: 1
108+
// CHECK: [[WBR_LINE:[0-9]+]]:5 | instance-method/acc-set/Swift | setter:subscript(dynamicMember:) | [[SUB_SET_USR]] | Ref,Call,Impl,RelCall,RelCont | rel: 1
109+
110+
// => property bottomRight
111+
// CHECK: [[WBR_LINE:[0-9]+]]:5 | instance-property/Swift | bottomRight | [[BR_USR]] | Ref,Read,Writ,RelCont | rel: 1
112+
// CHECK: [[WBR_LINE:[0-9]+]]:5 | instance-method/acc-get/Swift | getter:bottomRight | [[BR_GET_USR]] | Ref,Call,Impl,RelCall,RelCont | rel: 1
113+
// CHECK: [[WBR_LINE:[0-9]+]]:5 | instance-method/acc-set/Swift | setter:bottomRight | [[BR_SET_USR]] | Ref,Call,Impl,RelCall,RelCont | rel: 1
114+
115+
// => implicit dynamicMember subscript (y)
116+
// CHECK: [[WBR_LINE:[0-9]+]]:17 | instance-property/subscript/Swift | subscript(dynamicMember:) | [[SUB_USR]] | Ref,Writ,RelCont | rel: 1
117+
// CHECK: [[WBR_LINE:[0-9]+]]:17 | instance-method/acc-set/Swift | setter:subscript(dynamicMember:) | [[SUB_SET_USR]] | Ref,Call,Impl,RelCall,RelCont | rel: 1
118+
119+
// => property y
120+
// CHECK: [[WBR_LINE:[0-9]+]]:17 | instance-property/Swift | y | [[PY_USR]] | Ref,Writ,RelCont | rel: 1
121+
// CHECK: [[WBR_LINE:[0-9]+]]:17 | instance-method/acc-set/Swift | setter:y | [[PY_SET_USR]] | Ref,Call,Impl,RelCall,RelCont | rel: 1
122+
123+
// FIXME: crashes typechecker rdar://problem/49533404
124+
// a[0] = Lens(1)
125+
}

0 commit comments

Comments
 (0)