Skip to content

CodeCompletion: Annotate archetypes, generic parameters and dependent… #34406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -5219,6 +5219,10 @@ class PrimaryArchetypeType final : public ArchetypeType,
GenericEnvironment *getGenericEnvironment() const {
return Environment;
}

GenericTypeParamType *getInterfaceType() const {
return cast<GenericTypeParamType>(InterfaceType.getPointer());
}

static bool classof(const TypeBase *T) {
return T->getKind() == TypeKind::PrimaryArchetype;
Expand Down
47 changes: 22 additions & 25 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4539,7 +4539,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
visit(T->getOpenedExistentialType());
}

void printArchetypeCommon(ArchetypeType *T) {
void printArchetypeCommon(ArchetypeType *T,
const AbstractTypeParamDecl *Decl) {
if (Options.AlternativeTypeNames) {
auto found = Options.AlternativeTypeNames->find(T->getCanonicalType());
if (found != Options.AlternativeTypeNames->end()) {
Expand All @@ -4548,24 +4549,23 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
}
}

auto Name = T->getName();
if (Name.empty())
const auto Name = T->getName();
if (Name.empty()) {
Printer << "<anonymous>";
else {
PrintNameContext context = PrintNameContext::Normal;
if (Name == T->getASTContext().Id_Self)
context = PrintNameContext::GenericParameter;
Printer.printName(Name, context);
} else if (Decl) {
Printer.printTypeRef(T, Decl, Name);
} else {
Printer.printName(Name);
}
}

void visitNestedArchetypeType(NestedArchetypeType *T) {
visitParentType(T->getParent());
printArchetypeCommon(T);
printArchetypeCommon(T, T->getAssocType());
}

void visitPrimaryArchetypeType(PrimaryArchetypeType *T) {
printArchetypeCommon(T);
printArchetypeCommon(T, T->getInterfaceType()->getDecl());
}

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

auto Name = T->getName();
if (Name.empty())
const auto Name = T->getName();
if (Name.empty()) {
Printer << "<anonymous>";
else {
if (T->getDecl() &&
T->getDecl()->getDeclContext()->getSelfProtocolDecl()) {
Printer.printTypeRef(T, T->getDecl(), Name);
return;
}

PrintNameContext context = PrintNameContext::Normal;
if (Name == T->getASTContext().Id_Self)
context = PrintNameContext::GenericParameter;
Printer.printName(Name, context);
} else if (auto *Decl = T->getDecl()) {
Printer.printTypeRef(T, Decl, Name);
} else {
Printer.printName(Name);
}
}

void visitDependentMemberType(DependentMemberType *T) {
visitParentType(T->getBase());
Printer.printName(T->getName());
if (auto *const Assoc = T->getAssocType()) {
Printer.printTypeRef(T, Assoc, T->getName());
} else {
Printer.printName(T->getName());
}
}

#define REF_STORAGE(Name, name, ...) \
Expand Down
23 changes: 23 additions & 0 deletions test/IDE/complete_annotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// RUN: %swift-ide-test -code-completion -code-completion-annotate-results -source-filename %s -code-completion-token=EXPR_POSTFIX | %FileCheck %s --check-prefix=EXPR_POSTFIX
// RUN: %swift-ide-test -code-completion -code-completion-annotate-results -source-filename %s -code-completion-token=EXPR_IMPLICITMEMBER | %FileCheck %s --check-prefix=EXPR_IMPLICITMEMBER
// RUN: %swift-ide-test -code-completion -code-completion-annotate-results -source-filename %s -code-completion-token=CALLARG | %FileCheck %s --check-prefix=CALLARG
// RUN: %swift-ide-test -code-completion -code-completion-annotate-results -source-filename %s -code-completion-token=GENERIC | %FileCheck %s --check-prefix=GENERIC
// RUN: %swift-ide-test -code-completion -code-completion-annotate-results -source-filename %s -code-completion-token=WHERE | %FileCheck %s --check-prefix=WHERE

struct MyStruct {
init(x: Int) {}
Expand Down Expand Up @@ -113,3 +115,24 @@ func testArgument() -> MyStruct {
// 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>
// CALLARG: End completions

struct TestArchetypeAnnotations<T> {
func foo1<U>(u: U, t: T) {}
func foo2<S: Sequence>(s: S, elt: S.Element) {}
}

func testArchetypeAnnotations<T>(arg: TestArchetypeAnnotations<T>) {
arg.#^GENERIC^#
}
// GENERIC: Begin completions, 3 items
// GENERIC-DAG: Keyword[self]/CurrNominal: <keyword>self</keyword>; typename=<typeid.user>TestArchetypeAnnotations</typeid.user>&lt;<typeid.user>T</typeid.user>&gt;; name=self
// 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)
// 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)
// GENERIC: End completions

struct TestGenericParamAnnotations<T> {
func foo1<U>(u: U) where #^WHERE^#
}
// WHERE: Begin completions, 2 items
// WHERE-NEXT: Decl[GenericTypeParam]/Local: <name>T</name>; typename=<typeid.user>T</typeid.user>; name=T
// WHERE-NEXT: Decl[GenericTypeParam]/Local: <name>U</name>; typename=<typeid.user>U</typeid.user>; name=U
// WHERE: End completions
24 changes: 12 additions & 12 deletions test/IDE/print_synthesized_extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,21 @@ extension S13 : P5 {
public func foo1() {}
}

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

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

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

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

Expand All @@ -278,17 +278,17 @@ extension S13 : P5 {
// CHECK8: <decl:Struct>public struct <loc>S4<<decl:GenericTypeParam>T</decl>></loc> : <ref:module>print_synthesized_extensions</ref>.<ref:Protocol>P1</ref> {
// CHECK8-NEXT: <decl:TypeAlias>public typealias <loc>T1</loc> = <ref:Struct>Int</ref></decl>
// CHECK8-NEXT: <decl:TypeAlias>public typealias <loc>T2</loc> = <ref:Struct>Int</ref></decl>
// 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>
// 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>
// 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>
// 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>
// CHECK8-NEXT: <decl:Func>public func <loc>p1IntFunc(<decl:Param>i: <ref:Struct>Int</ref></decl>)</loc> -> <ref:Struct>Int</ref></decl>
// CHECK8-NEXT: }</synthesized>

// CHECK9: <decl:Struct>public struct <loc>S6<<decl:GenericTypeParam>T</decl>></loc> : <ref:module>print_synthesized_extensions</ref>.<ref:Protocol>P1</ref> {
// CHECK9-NEXT: <decl:TypeAlias>public typealias <loc>T1</loc> = <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S5</ref></decl>
// CHECK9-NEXT: <decl:TypeAlias>public typealias <loc>T2</loc> = <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S5</ref></decl>
// 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>
// 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>

// 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>
// 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>
// CHECK9-NEXT: <decl:Extension><decl:Func>public func <loc>f3()</loc></decl></decl>
// CHECK9-NEXT: <decl:Extension><decl:Func>public func <loc>fromActualExtension()</loc></decl></decl>
// CHECK9-NEXT: <decl:Func>public func <loc>p3Func(<decl:Param>i: <ref:Struct>Int</ref></decl>)</loc> -> <ref:Struct>Int</ref></decl>
Expand Down Expand Up @@ -321,12 +321,12 @@ extension S13 : P5 {

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

// CHECK13: <decl:Extension>extension <loc><ref:Protocol>P7</ref></loc> {
// CHECK13-NEXT: <decl:Func>public func <loc>nomergeFunc(<decl:Param>t: <ref:GenericTypeParam>Self</ref>.T1</decl>)</loc> -> <ref:GenericTypeParam>Self</ref>.T1</decl>
// CHECK13-NEXT: <decl:Func>public func <loc>f1(<decl:Param>t: <ref:GenericTypeParam>Self</ref>.T1</decl>)</loc> -> <ref:GenericTypeParam>Self</ref>.T1</decl>
// 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>
// 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>
// CHECK13-NEXT: }</decl>

// CHECK14: <decl:Struct>public struct <loc>S13</loc> {</decl>
Expand Down
Loading