Skip to content

Commit 59172c9

Browse files
committed
[SourceKit] Update for primary associated types
* InterfaceGen reports a primary associated type as a reference to the 'associatedtype' declaration * CursorInfo on a primary associated type returns information of the 'associatedtype' declaration rdar://93275458
1 parent 42655e1 commit 59172c9

File tree

6 files changed

+184
-11
lines changed

6 files changed

+184
-11
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3529,8 +3529,9 @@ void PrintAST::printPrimaryAssociatedTypes(ProtocolDecl *decl) {
35293529
[&](AssociatedTypeDecl *assocType) {
35303530
Printer.callPrintStructurePre(PrintStructureKind::GenericParameter,
35313531
assocType);
3532-
Printer.printName(assocType->getName(),
3533-
PrintNameContext::GenericParameter);
3532+
Printer.printTypeRef(assocType->getDeclaredInterfaceType(), assocType,
3533+
assocType->getName(),
3534+
PrintNameContext::GenericParameter);
35343535
Printer.printStructurePost(PrintStructureKind::GenericParameter,
35353536
assocType);
35363537
},

lib/IDE/SourceEntityWalker.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,21 @@ bool SemaAnnotator::walkToDeclPreProper(Decl *D) {
180180
if (!ReportParamList(ParamList))
181181
return false;
182182
}
183+
184+
if (auto proto = dyn_cast<ProtocolDecl>(VD)) {
185+
// Report a primary associated type as a references to the associated type
186+
// declaration.
187+
for (auto parsedName : proto->getPrimaryAssociatedTypeNames()) {
188+
Identifier name = parsedName.first;
189+
SourceLoc loc = parsedName.second;
190+
if (auto assocTypeDecl = proto->getAssociatedType(name)) {
191+
passReference(assocTypeDecl,
192+
assocTypeDecl->getDeclaredInterfaceType(),
193+
DeclNameLoc(loc),
194+
ReferenceMetaData(SemaReferenceKind::TypeRef, None));
195+
}
196+
}
197+
}
183198
} else if (auto *ED = dyn_cast<ExtensionDecl>(D)) {
184199
SourceRange SR = SourceRange();
185200
if (auto *repr = ED->getExtendedTypeRepr())

test/SourceKit/CursorInfo/cursor_generics.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public struct HostRecord<IPType: IP> {
3030
}
3131
}
3232

33+
public protocol Proto<Assoc> {
34+
associatedtype Assoc
35+
}
36+
3337
// RUN: %sourcekitd-test -req=cursor -pos=1:10 %s -- %s | %FileCheck -check-prefix=CHECK1 %s
3438
// CHECK1: <Declaration>func testGenerics&lt;T&gt;(x: <Type usr="s:15cursor_generics12testGenerics1xyx_tlF1TL_xmfp">T</Type>)</Declaration>
3539

@@ -52,3 +56,13 @@ public struct HostRecord<IPType: IP> {
5256
// RUN: %sourcekitd-test -req=cursor -pos=29:22 %s -- %s | %FileCheck -check-prefix=CHECK_IP_TYPE %s
5357
// CHECK_IP_TYPE: source.lang.swift.ref.generic_type_param
5458
// CHECK_IP_TYPE: <Declaration>IPType : <Type usr="s:15cursor_generics2IPP">IP</Type></Declaration>
59+
60+
// RUN: %sourcekitd-test -req=cursor -pos=33:23 %s -- %s | %FileCheck --check-prefixes=CHECK_ASSOC_COMMON,CHECK_ASSOC_PRIMARY %s
61+
// RUN: %sourcekitd-test -req=cursor -pos=34:18 %s -- %s | %FileCheck --check-prefixes=CHECK_ASSOC_COMMON,CHECK_ASSOC_DECL %s
62+
// CHECK_ASSOC_PRIMARY: source.lang.swift.ref.associatedtype
63+
// CHECK_ASSOC_DECL: source.lang.swift.decl.associatedtype
64+
// CHECK_ASSOC_COMMON: Assoc
65+
// CHECK_ASSOC_COMMON: Self.Assoc.Type
66+
// CHECK_ASSOC_COMMON: <Declaration>associatedtype Assoc</Declaration>
67+
// CHECK_ASSOC_COMMON: <decl.associatedtype><syntaxtype.keyword>associatedtype</syntaxtype.keyword> <decl.name>Assoc</decl.name></decl.associatedtype>
68+

test/SourceKit/InterfaceGen/Inputs/swift_mod.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ public func pub_function() {}
99
internal func int_function() {}
1010
fileprivate func fp_function() {}
1111
private func priv_function() {}
12+
13+
public protocol MyProto<Assoc> {
14+
associatedtype Assoc
15+
}

test/SourceKit/InterfaceGen/gen_swift_module.swift.from_swiftinterface.response

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ public class MyClass {
44
public func pub_method()
55
}
66

7+
public protocol MyProto<Assoc> {
8+
9+
associatedtype Assoc
10+
}
11+
712
public func pub_function()
813

914

@@ -46,15 +51,51 @@ public func pub_function()
4651
{
4752
key.kind: source.lang.swift.syntaxtype.keyword,
4853
key.offset: 64,
54+
key.length: 8
55+
},
56+
{
57+
key.kind: source.lang.swift.syntaxtype.identifier,
58+
key.offset: 73,
59+
key.length: 7
60+
},
61+
{
62+
key.kind: source.lang.swift.syntaxtype.identifier,
63+
key.offset: 81,
64+
key.length: 5
65+
},
66+
{
67+
key.kind: source.lang.swift.syntaxtype.keyword,
68+
key.offset: 95,
69+
key.length: 14
70+
},
71+
{
72+
key.kind: source.lang.swift.syntaxtype.identifier,
73+
key.offset: 110,
74+
key.length: 5
75+
},
76+
{
77+
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
78+
key.offset: 119,
79+
key.length: 6
80+
},
81+
{
82+
key.kind: source.lang.swift.syntaxtype.keyword,
83+
key.offset: 126,
4984
key.length: 4
5085
},
5186
{
5287
key.kind: source.lang.swift.syntaxtype.identifier,
53-
key.offset: 69,
88+
key.offset: 131,
5489
key.length: 12
5590
}
5691
]
57-
<<NULL>>
92+
[
93+
{
94+
key.kind: source.lang.swift.ref.associatedtype,
95+
key.offset: 81,
96+
key.length: 5
97+
}
98+
]
5899
[
59100
{
60101
key.kind: source.lang.swift.decl.class,
@@ -92,17 +133,46 @@ public func pub_function()
92133
}
93134
]
94135
},
136+
{
137+
key.kind: source.lang.swift.decl.protocol,
138+
key.accessibility: source.lang.swift.accessibility.public,
139+
key.name: "MyProto",
140+
key.offset: 64,
141+
key.length: 53,
142+
key.nameoffset: 73,
143+
key.namelength: 7,
144+
key.bodyoffset: 89,
145+
key.bodylength: 27,
146+
key.attributes: [
147+
{
148+
key.offset: 57,
149+
key.length: 6,
150+
key.attribute: source.decl.attribute.public
151+
}
152+
],
153+
key.substructure: [
154+
{
155+
key.kind: source.lang.swift.decl.associatedtype,
156+
key.accessibility: source.lang.swift.accessibility.public,
157+
key.name: "Assoc",
158+
key.offset: 95,
159+
key.length: 20,
160+
key.nameoffset: 110,
161+
key.namelength: 5
162+
}
163+
]
164+
},
95165
{
96166
key.kind: source.lang.swift.decl.function.free,
97167
key.accessibility: source.lang.swift.accessibility.public,
98168
key.name: "pub_function()",
99-
key.offset: 64,
169+
key.offset: 126,
100170
key.length: 19,
101-
key.nameoffset: 69,
171+
key.nameoffset: 131,
102172
key.namelength: 14,
103173
key.attributes: [
104174
{
105-
key.offset: 57,
175+
key.offset: 119,
106176
key.length: 6,
107177
key.attribute: source.decl.attribute.public
108178
}

test/SourceKit/InterfaceGen/gen_swift_module.swift.response

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ public class MyClass {
55
public func pub_method()
66
}
77

8+
public protocol MyProto<Assoc> {
9+
10+
associatedtype Assoc
11+
}
12+
813
public func pub_function()
914

1015

@@ -57,11 +62,41 @@ public func pub_function()
5762
{
5863
key.kind: source.lang.swift.syntaxtype.keyword,
5964
key.offset: 89,
65+
key.length: 8
66+
},
67+
{
68+
key.kind: source.lang.swift.syntaxtype.identifier,
69+
key.offset: 98,
70+
key.length: 7
71+
},
72+
{
73+
key.kind: source.lang.swift.syntaxtype.identifier,
74+
key.offset: 106,
75+
key.length: 5
76+
},
77+
{
78+
key.kind: source.lang.swift.syntaxtype.keyword,
79+
key.offset: 120,
80+
key.length: 14
81+
},
82+
{
83+
key.kind: source.lang.swift.syntaxtype.identifier,
84+
key.offset: 135,
85+
key.length: 5
86+
},
87+
{
88+
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
89+
key.offset: 144,
90+
key.length: 6
91+
},
92+
{
93+
key.kind: source.lang.swift.syntaxtype.keyword,
94+
key.offset: 151,
6095
key.length: 4
6196
},
6297
{
6398
key.kind: source.lang.swift.syntaxtype.identifier,
64-
key.offset: 94,
99+
key.offset: 156,
65100
key.length: 12
66101
}
67102
]
@@ -71,6 +106,11 @@ public func pub_function()
71106
key.offset: 7,
72107
key.length: 17,
73108
key.is_system: 1
109+
},
110+
{
111+
key.kind: source.lang.swift.ref.associatedtype,
112+
key.offset: 106,
113+
key.length: 5
74114
}
75115
]
76116
[
@@ -110,17 +150,46 @@ public func pub_function()
110150
}
111151
]
112152
},
153+
{
154+
key.kind: source.lang.swift.decl.protocol,
155+
key.accessibility: source.lang.swift.accessibility.public,
156+
key.name: "MyProto",
157+
key.offset: 89,
158+
key.length: 53,
159+
key.nameoffset: 98,
160+
key.namelength: 7,
161+
key.bodyoffset: 114,
162+
key.bodylength: 27,
163+
key.attributes: [
164+
{
165+
key.offset: 82,
166+
key.length: 6,
167+
key.attribute: source.decl.attribute.public
168+
}
169+
],
170+
key.substructure: [
171+
{
172+
key.kind: source.lang.swift.decl.associatedtype,
173+
key.accessibility: source.lang.swift.accessibility.public,
174+
key.name: "Assoc",
175+
key.offset: 120,
176+
key.length: 20,
177+
key.nameoffset: 135,
178+
key.namelength: 5
179+
}
180+
]
181+
},
113182
{
114183
key.kind: source.lang.swift.decl.function.free,
115184
key.accessibility: source.lang.swift.accessibility.public,
116185
key.name: "pub_function()",
117-
key.offset: 89,
186+
key.offset: 151,
118187
key.length: 19,
119-
key.nameoffset: 94,
188+
key.nameoffset: 156,
120189
key.namelength: 14,
121190
key.attributes: [
122191
{
123-
key.offset: 82,
192+
key.offset: 144,
124193
key.length: 6,
125194
key.attribute: source.decl.attribute.public
126195
}

0 commit comments

Comments
 (0)