Skip to content

[CodeCompletion] Suggest typealias on protocols #38054

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
Jul 2, 2021
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
7 changes: 7 additions & 0 deletions lib/Sema/IDETypeCheckingRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ static bool isMemberDeclAppliedInternal(const DeclContext *DC, Type BaseTy,
BaseTy->hasUnresolvedType() || BaseTy->hasError())
return true;

if (isa<TypeAliasDecl>(VD) && BaseTy->is<ProtocolType>()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do this before line 153, before getting the generic signature?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, makes sense as it’s cheaper to check. Moved it up

// The protocol doesn't satisfy its own generic signature (static members
// of the protocol are not visible on the protocol itself) but we can still
// access typealias declarations on it.
return true;
}

const GenericContext *genericDecl = VD->getAsGenericContext();
if (!genericDecl)
return true;
Expand Down
58 changes: 58 additions & 0 deletions test/IDE/complete_protocol_typealias.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t

protocol MyProto {
typealias Content = Int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a typealias with generic arguments? e.g. typealias Storage<T> = Array<T>. I don't this your change affects this case, but still.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test case. Seems to not be an issue.

}
func testSimpleInTypeCompletion() -> MyProto.#^SIMPLE_IN_TYPE_COMPLETION^# {}
// SIMPLE_IN_TYPE_COMPLETION: Begin completions, 3 items
// SIMPLE_IN_TYPE_COMPLETION-DAG: Decl[TypeAlias]/CurrNominal: Content[#Int#];
// SIMPLE_IN_TYPE_COMPLETION-DAG: Keyword/None: Protocol[#MyProto.Protocol#];
// SIMPLE_IN_TYPE_COMPLETION-DAG: Keyword/None: Type[#MyProto.Type#];
// SIMPLE_IN_TYPE_COMPLETION: End completions

func testUnconstrainedUnresolvedMember() {
let _: MyProto = .#^UNCONSTRAINED_UNRESOLVED_MEMBER^#
// UNCONSTRAINED_UNRESOLVED_MEMBER: Begin completions, 1 item
// UNCONSTRAINED_UNRESOLVED_MEMBER-DAG: Decl[TypeAlias]/CurrNominal: Content[#Int#];
// UNCONSTRAINED_UNRESOLVED_MEMBER: End completions
}

protocol MyOtherProto {
associatedtype MyAssocType
}
extension MyOtherProto where MyAssocType == String {
typealias Content = Int
}

// `Content` is actually accessible on `MyOtherProto` here, but that seems more like a bug of the language than a feature, so we don't want to promote it in code completion.
func testConstrainedInTypeCompletion() -> MyOtherProto.#^CONSTRAINED_IN_TYPE_COMPLETION^# {}
// CONSTRAINED_IN_TYPE_COMPLETION: Begin completions, 3 items
// CONSTRAINED_IN_TYPE_COMPLETION-DAG: Decl[AssociatedType]/CurrNominal: MyAssocType;
// CONSTRAINED_IN_TYPE_COMPLETION-DAG: Keyword/None: Protocol[#MyOtherProto.Protocol#];
// CONSTRAINED_IN_TYPE_COMPLETION-DAG: Keyword/None: Type[#MyOtherProto.Type#];
// CONSTRAINED_IN_TYPE_COMPLETION: End completions

func testConstrainedUnresolvedMember() {
let _: MyOtherProto = .#^CONSTRAINED_UNRESOLVED_MEMBER^#
// CONSTRAINED_UNRESOLVED_MEMBER: Begin completions, 1 item
// CONSTRAINED_UNRESOLVED_MEMBER-DAG: Decl[AssociatedType]/CurrNominal: MyAssocType;
// CONSTRAINED_UNRESOLVED_MEMBER: End completions
}

protocol ProtoWithGenericTypealias {
typealias Storage<T> = Array<T>
}
func testGenericInTypeCompletion() -> ProtoWithGenericTypealias.#^GENERIC_IN_TYPE_COMPLETION^# {}
// GENERIC_IN_TYPE_COMPLETION: Begin completions, 3 items
// GENERIC_IN_TYPE_COMPLETION-DAG: Decl[TypeAlias]/CurrNominal: Storage[#Array<T>#];
// GENERIC_IN_TYPE_COMPLETION-DAG: Keyword/None: Protocol[#ProtoWithGenericTypealias.Protocol#];
// GENERIC_IN_TYPE_COMPLETION-DAG: Keyword/None: Type[#ProtoWithGenericTypealias.Type#];
// GENERIC_IN_TYPE_COMPLETION: End completions

func testGenericUnresolvedMember() {
let _: ProtoWithGenericTypealias = .#^GENERIC_UNRESOLVED_MEMBER^#
// GENERIC_UNRESOLVED_MEMBER: Begin completions, 1 item
// GENERIC_UNRESOLVED_MEMBER-DAG: Decl[TypeAlias]/CurrNominal: Storage[#Array<T>#];
// GENERIC_UNRESOLVED_MEMBER: End completions
}
5 changes: 4 additions & 1 deletion test/IDE/complete_unresolved_members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,10 @@ func testSubType() {
func testMemberTypealias() {
var _: MyProtocol = .#^SUBTYPE_2^#
}
// SUBTYPE_2-NOT: Begin completions
// SUBTYPE_2: Begin completions, 2 items
// SUBTYPE_2-DAG: Decl[TypeAlias]/CurrNominal/TypeRelation[Convertible]: Concrete1[#BaseClass#];
// SUBTYPE_2-DAG: Decl[TypeAlias]/CurrNominal/TypeRelation[Convertible]: Concrete2[#AnotherTy#];
// SUBTYPE_2: End completions

enum Generic<T> {
case contains(content: T)
Expand Down