Skip to content

Commit 08612ff

Browse files
Merge pull request #34406 from AnthonyLatsis/ide-misc
CodeCompletion: Annotate archetypes, generic parameters and dependent members
2 parents c10064a + 4ce7a2d commit 08612ff

16 files changed

+296
-137
lines changed

include/swift/AST/Types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5219,6 +5219,10 @@ class PrimaryArchetypeType final : public ArchetypeType,
52195219
GenericEnvironment *getGenericEnvironment() const {
52205220
return Environment;
52215221
}
5222+
5223+
GenericTypeParamType *getInterfaceType() const {
5224+
return cast<GenericTypeParamType>(InterfaceType.getPointer());
5225+
}
52225226

52235227
static bool classof(const TypeBase *T) {
52245228
return T->getKind() == TypeKind::PrimaryArchetype;

lib/AST/ASTPrinter.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4539,7 +4539,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
45394539
visit(T->getOpenedExistentialType());
45404540
}
45414541

4542-
void printArchetypeCommon(ArchetypeType *T) {
4542+
void printArchetypeCommon(ArchetypeType *T,
4543+
const AbstractTypeParamDecl *Decl) {
45434544
if (Options.AlternativeTypeNames) {
45444545
auto found = Options.AlternativeTypeNames->find(T->getCanonicalType());
45454546
if (found != Options.AlternativeTypeNames->end()) {
@@ -4548,24 +4549,23 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
45484549
}
45494550
}
45504551

4551-
auto Name = T->getName();
4552-
if (Name.empty())
4552+
const auto Name = T->getName();
4553+
if (Name.empty()) {
45534554
Printer << "<anonymous>";
4554-
else {
4555-
PrintNameContext context = PrintNameContext::Normal;
4556-
if (Name == T->getASTContext().Id_Self)
4557-
context = PrintNameContext::GenericParameter;
4558-
Printer.printName(Name, context);
4555+
} else if (Decl) {
4556+
Printer.printTypeRef(T, Decl, Name);
4557+
} else {
4558+
Printer.printName(Name);
45594559
}
45604560
}
4561-
4561+
45624562
void visitNestedArchetypeType(NestedArchetypeType *T) {
45634563
visitParentType(T->getParent());
4564-
printArchetypeCommon(T);
4564+
printArchetypeCommon(T, T->getAssocType());
45654565
}
45664566

45674567
void visitPrimaryArchetypeType(PrimaryArchetypeType *T) {
4568-
printArchetypeCommon(T);
4568+
printArchetypeCommon(T, T->getInterfaceType()->getDecl());
45694569
}
45704570

45714571
void visitOpaqueTypeArchetypeType(OpaqueTypeArchetypeType *T) {
@@ -4632,26 +4632,23 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
46324632
T = Options.GenericSig->getSugaredType(T);
46334633
}
46344634

4635-
auto Name = T->getName();
4636-
if (Name.empty())
4635+
const auto Name = T->getName();
4636+
if (Name.empty()) {
46374637
Printer << "<anonymous>";
4638-
else {
4639-
if (T->getDecl() &&
4640-
T->getDecl()->getDeclContext()->getSelfProtocolDecl()) {
4641-
Printer.printTypeRef(T, T->getDecl(), Name);
4642-
return;
4643-
}
4644-
4645-
PrintNameContext context = PrintNameContext::Normal;
4646-
if (Name == T->getASTContext().Id_Self)
4647-
context = PrintNameContext::GenericParameter;
4648-
Printer.printName(Name, context);
4638+
} else if (auto *Decl = T->getDecl()) {
4639+
Printer.printTypeRef(T, Decl, Name);
4640+
} else {
4641+
Printer.printName(Name);
46494642
}
46504643
}
46514644

46524645
void visitDependentMemberType(DependentMemberType *T) {
46534646
visitParentType(T->getBase());
4654-
Printer.printName(T->getName());
4647+
if (auto *const Assoc = T->getAssocType()) {
4648+
Printer.printTypeRef(T, Assoc, T->getName());
4649+
} else {
4650+
Printer.printName(T->getName());
4651+
}
46554652
}
46564653

46574654
#define REF_STORAGE(Name, name, ...) \

test/IDE/complete_annotation.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// RUN: %swift-ide-test -code-completion -code-completion-annotate-results -source-filename %s -code-completion-token=EXPR_POSTFIX | %FileCheck %s --check-prefix=EXPR_POSTFIX
77
// RUN: %swift-ide-test -code-completion -code-completion-annotate-results -source-filename %s -code-completion-token=EXPR_IMPLICITMEMBER | %FileCheck %s --check-prefix=EXPR_IMPLICITMEMBER
88
// RUN: %swift-ide-test -code-completion -code-completion-annotate-results -source-filename %s -code-completion-token=CALLARG | %FileCheck %s --check-prefix=CALLARG
9+
// RUN: %swift-ide-test -code-completion -code-completion-annotate-results -source-filename %s -code-completion-token=GENERIC | %FileCheck %s --check-prefix=GENERIC
10+
// RUN: %swift-ide-test -code-completion -code-completion-annotate-results -source-filename %s -code-completion-token=WHERE | %FileCheck %s --check-prefix=WHERE
911

1012
struct MyStruct {
1113
init(x: Int) {}
@@ -113,3 +115,24 @@ func testArgument() -> MyStruct {
113115
// CALLARG-DAG: Pattern/ExprSpecific: <callarg><callarg.label>y</callarg.label>: <callarg.type><typeid.sys>Int</typeid.sys></callarg.type></callarg>; typename=<typeid.sys>Int</typeid.sys>
114116
// CALLARG: End completions
115117

118+
struct TestArchetypeAnnotations<T> {
119+
func foo1<U>(u: U, t: T) {}
120+
func foo2<S: Sequence>(s: S, elt: S.Element) {}
121+
}
122+
123+
func testArchetypeAnnotations<T>(arg: TestArchetypeAnnotations<T>) {
124+
arg.#^GENERIC^#
125+
}
126+
// GENERIC: Begin completions, 3 items
127+
// GENERIC-DAG: Keyword[self]/CurrNominal: <keyword>self</keyword>; typename=<typeid.user>TestArchetypeAnnotations</typeid.user>&lt;<typeid.user>T</typeid.user>&gt;; name=self
128+
// GENERIC-DAG: Decl[InstanceMethod]/CurrNominal: <name>foo1</name>(<callarg><callarg.label>u</callarg.label>: <callarg.type><typeid.user>U</typeid.user></callarg.type></callarg>, <callarg><callarg.label>t</callarg.label>: <callarg.type><typeid.user>T</typeid.user></callarg.type></callarg>); typename=<typeid.sys>Void</typeid.sys>; name=foo1(u: U, t: T)
129+
// GENERIC-DAG: Decl[InstanceMethod]/CurrNominal: <name>foo2</name>(<callarg><callarg.label>s</callarg.label>: <callarg.type><typeid.sys>Sequence</typeid.sys></callarg.type></callarg>, <callarg><callarg.label>elt</callarg.label>: <callarg.type><typeid.sys>Sequence</typeid.sys>.<typeid.sys>Element</typeid.sys></callarg.type></callarg>); typename=<typeid.sys>Void</typeid.sys>; name=foo2(s: Sequence, elt: Sequence.Element)
130+
// GENERIC: End completions
131+
132+
struct TestGenericParamAnnotations<T> {
133+
func foo1<U>(u: U) where #^WHERE^#
134+
}
135+
// WHERE: Begin completions, 2 items
136+
// WHERE-NEXT: Decl[GenericTypeParam]/Local: <name>T</name>; typename=<typeid.user>T</typeid.user>; name=T
137+
// WHERE-NEXT: Decl[GenericTypeParam]/Local: <name>U</name>; typename=<typeid.user>U</typeid.user>; name=U
138+
// WHERE: End completions

test/IDE/print_synthesized_extensions.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,21 +237,21 @@ extension S13 : P5 {
237237
public func foo1() {}
238238
}
239239

240-
// CHECK1: <synthesized>extension <ref:Struct>S1</ref> where T : <ref:Protocol>P2</ref> {
240+
// CHECK1: <synthesized>extension <ref:Struct>S1</ref> where <ref:GenericTypeParam>T</ref> : <ref:Protocol>P2</ref> {
241241
// CHECK1-NEXT: <decl:Func>public func <loc>p2member()</loc></decl>
242-
// CHECK1-NEXT: <decl:Func>public func <loc>ef1(<decl:Param>t: T</decl>)</loc></decl>
242+
// CHECK1-NEXT: <decl:Func>public func <loc>ef1(<decl:Param>t: <ref:GenericTypeParam>T</ref></decl>)</loc></decl>
243243
// CHECK1-NEXT: <decl:Func>public func <loc>ef2(<decl:Param>t: <ref:Struct>S2</ref></decl>)</loc></decl>
244244
// CHECK1-NEXT: }</synthesized>
245245

246-
// CHECK2: <synthesized>extension <ref:Struct>S1</ref> where T : <ref:Protocol>P3</ref> {
246+
// CHECK2: <synthesized>extension <ref:Struct>S1</ref> where <ref:GenericTypeParam>T</ref> : <ref:Protocol>P3</ref> {
247247
// CHECK2-NEXT: <decl:Func>public func <loc>p3Func(<decl:Param>i: <ref:Struct>Int</ref></decl>)</loc> -> <ref:Struct>Int</ref></decl>
248248
// CHECK2-NEXT: }</synthesized>
249249

250-
// CHECK3: <synthesized>extension <ref:Struct>S1</ref> where T == <ref:Struct>Int</ref> {
250+
// CHECK3: <synthesized>extension <ref:Struct>S1</ref> where <ref:GenericTypeParam>T</ref> == <ref:Struct>Int</ref> {
251251
// CHECK3-NEXT: <decl:Func>public func <loc>p1IntFunc(<decl:Param>i: <ref:Struct>Int</ref></decl>)</loc> -> <ref:Struct>Int</ref></decl>
252252
// CHECK3-NEXT: }</synthesized>
253253

254-
// CHECK4: <synthesized>extension <ref:Struct>S1</ref> where T == <ref:Struct>S9</ref><<ref:Struct>Int</ref>> {
254+
// CHECK4: <synthesized>extension <ref:Struct>S1</ref> where <ref:GenericTypeParam>T</ref> == <ref:Struct>S9</ref><<ref:Struct>Int</ref>> {
255255
// CHECK4-NEXT: <decl:Func>public func <loc>S9IntFunc()</loc></decl>
256256
// CHECK4-NEXT: }</synthesized>
257257

@@ -278,17 +278,17 @@ extension S13 : P5 {
278278
// CHECK8: <decl:Struct>public struct <loc>S4<<decl:GenericTypeParam>T</decl>></loc> : <ref:module>print_synthesized_extensions</ref>.<ref:Protocol>P1</ref> {
279279
// CHECK8-NEXT: <decl:TypeAlias>public typealias <loc>T1</loc> = <ref:Struct>Int</ref></decl>
280280
// CHECK8-NEXT: <decl:TypeAlias>public typealias <loc>T2</loc> = <ref:Struct>Int</ref></decl>
281-
// CHECK8-NEXT: <decl:Func>public func <loc>f1(<decl:Param>t: <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S4</ref><T>.<ref:TypeAlias>T1</ref></decl>)</loc> -> <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S4</ref><T>.<ref:TypeAlias>T1</ref></decl>
282-
// CHECK8-NEXT: <decl:Func>public func <loc>f2(<decl:Param>t: <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S4</ref><T>.<ref:TypeAlias>T2</ref></decl>)</loc> -> <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S4</ref><T>.<ref:TypeAlias>T2</ref></decl></decl>
281+
// CHECK8-NEXT: <decl:Func>public func <loc>f1(<decl:Param>t: <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S4</ref><<ref:GenericTypeParam>T</ref>>.<ref:TypeAlias>T1</ref></decl>)</loc> -> <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S4</ref><<ref:GenericTypeParam>T</ref>>.<ref:TypeAlias>T1</ref></decl>
282+
// CHECK8-NEXT: <decl:Func>public func <loc>f2(<decl:Param>t: <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S4</ref><<ref:GenericTypeParam>T</ref>>.<ref:TypeAlias>T2</ref></decl>)</loc> -> <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S4</ref><<ref:GenericTypeParam>T</ref>>.<ref:TypeAlias>T2</ref></decl></decl>
283283
// CHECK8-NEXT: <decl:Func>public func <loc>p1IntFunc(<decl:Param>i: <ref:Struct>Int</ref></decl>)</loc> -> <ref:Struct>Int</ref></decl>
284284
// CHECK8-NEXT: }</synthesized>
285285

286286
// CHECK9: <decl:Struct>public struct <loc>S6<<decl:GenericTypeParam>T</decl>></loc> : <ref:module>print_synthesized_extensions</ref>.<ref:Protocol>P1</ref> {
287287
// CHECK9-NEXT: <decl:TypeAlias>public typealias <loc>T1</loc> = <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S5</ref></decl>
288288
// CHECK9-NEXT: <decl:TypeAlias>public typealias <loc>T2</loc> = <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S5</ref></decl>
289-
// CHECK9-NEXT: <decl:Func>public func <loc>f1(<decl:Param>t: <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S6</ref><T>.<ref:TypeAlias>T1</ref></decl>)</loc> -> <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S6</ref><T>.<ref:TypeAlias>T1</ref></decl>
289+
// CHECK9-NEXT: <decl:Func>public func <loc>f1(<decl:Param>t: <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S6</ref><<ref:GenericTypeParam>T</ref>>.<ref:TypeAlias>T1</ref></decl>)</loc> -> <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S6</ref><<ref:GenericTypeParam>T</ref>>.<ref:TypeAlias>T1</ref></decl>
290290

291-
// CHECK9-NEXT: <decl:Func>public func <loc>f2(<decl:Param>t: <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S6</ref><T>.<ref:TypeAlias>T2</ref></decl>)</loc> -> <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S6</ref><T>.<ref:TypeAlias>T2</ref></decl></decl>
291+
// CHECK9-NEXT: <decl:Func>public func <loc>f2(<decl:Param>t: <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S6</ref><<ref:GenericTypeParam>T</ref>>.<ref:TypeAlias>T2</ref></decl>)</loc> -> <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S6</ref><<ref:GenericTypeParam>T</ref>>.<ref:TypeAlias>T2</ref></decl></decl>
292292
// CHECK9-NEXT: <decl:Extension><decl:Func>public func <loc>f3()</loc></decl></decl>
293293
// CHECK9-NEXT: <decl:Extension><decl:Func>public func <loc>fromActualExtension()</loc></decl></decl>
294294
// CHECK9-NEXT: <decl:Func>public func <loc>p3Func(<decl:Param>i: <ref:Struct>Int</ref></decl>)</loc> -> <ref:Struct>Int</ref></decl>
@@ -321,12 +321,12 @@ extension S13 : P5 {
321321

322322
// CHECK13: <decl:Protocol>public protocol <loc>P7</loc> {
323323
// CHECK13-NEXT: <decl:AssociatedType>associatedtype <loc>T1</loc></decl>
324-
// CHECK13-NEXT: <decl:Func(HasDefault)>func <loc>f1(<decl:Param>t: <ref:GenericTypeParam>Self</ref>.T1</decl>)</loc></decl>
324+
// CHECK13-NEXT: <decl:Func(HasDefault)>func <loc>f1(<decl:Param>t: <ref:GenericTypeParam>Self</ref>.<ref:AssociatedType>T1</ref></decl>)</loc></decl>
325325
// CHECK13-NEXT: }</decl>
326326

327327
// CHECK13: <decl:Extension>extension <loc><ref:Protocol>P7</ref></loc> {
328-
// CHECK13-NEXT: <decl:Func>public func <loc>nomergeFunc(<decl:Param>t: <ref:GenericTypeParam>Self</ref>.T1</decl>)</loc> -> <ref:GenericTypeParam>Self</ref>.T1</decl>
329-
// CHECK13-NEXT: <decl:Func>public func <loc>f1(<decl:Param>t: <ref:GenericTypeParam>Self</ref>.T1</decl>)</loc> -> <ref:GenericTypeParam>Self</ref>.T1</decl>
328+
// CHECK13-NEXT: <decl:Func>public func <loc>nomergeFunc(<decl:Param>t: <ref:GenericTypeParam>Self</ref>.<ref:AssociatedType>T1</ref></decl>)</loc> -> <ref:GenericTypeParam>Self</ref>.<ref:AssociatedType>T1</ref></decl>
329+
// CHECK13-NEXT: <decl:Func>public func <loc>f1(<decl:Param>t: <ref:GenericTypeParam>Self</ref>.<ref:AssociatedType>T1</ref></decl>)</loc> -> <ref:GenericTypeParam>Self</ref>.<ref:AssociatedType>T1</ref></decl>
330330
// CHECK13-NEXT: }</decl>
331331

332332
// CHECK14: <decl:Struct>public struct <loc>S13</loc> {</decl>

0 commit comments

Comments
 (0)