Skip to content

[CodeCompletion] Completion support for static subscript #24180

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
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: 2 additions & 2 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2594,8 +2594,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
}

void addSubscriptCall(const SubscriptDecl *SD, DeclVisibilityKind Reason) {
// Don't add subscript call to meta types.
if (!ExprType || ExprType->is<AnyMetatypeType>())
// Don't add subscript call to unqualified completion.
if (!ExprType)
return;

// Subscript after '.' is valid only after type part of Swift keypath
Expand Down
13 changes: 8 additions & 5 deletions lib/IDE/ExprContextAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ class ExprParentFinder : public ASTWalker {
static void collectPossibleCalleesByQualifiedLookup(
DeclContext &DC, Type baseTy, DeclBaseName name,
SmallVectorImpl<FunctionTypeAndDecl> &candidates) {
bool isOnMetaType = baseTy->is<AnyMetatypeType>();

SmallVector<ValueDecl *, 2> decls;
auto resolver = DC.getASTContext().getLazyResolver();
Expand All @@ -287,16 +288,18 @@ static void collectPossibleCalleesByQualifiedLookup(
continue;
Type declaredMemberType = VD->getInterfaceType();
if (VD->getDeclContext()->isTypeContext()) {
if (auto *FD = dyn_cast<FuncDecl>(VD)) {
if (!baseTy->is<AnyMetatypeType>())
if (isa<FuncDecl>(VD)) {
if (!isOnMetaType)
declaredMemberType =
declaredMemberType->castTo<AnyFunctionType>()->getResult();
}
if (auto *CD = dyn_cast<ConstructorDecl>(VD)) {
if (!baseTy->is<AnyMetatypeType>())
} else if (isa<ConstructorDecl>(VD)) {
if (!isOnMetaType)
continue;
declaredMemberType =
declaredMemberType->castTo<AnyFunctionType>()->getResult();
} else if (isa<SubscriptDecl>(VD)) {
if (isOnMetaType != VD->isStatic())
continue;
}
}

Expand Down
11 changes: 11 additions & 0 deletions lib/Sema/LookupVisibleDecls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ static bool isDeclVisibleInLookupMode(ValueDecl *Member, LookupState LS,
// Otherwise, either call a function or curry it.
return true;
}
if (auto *SD = dyn_cast<SubscriptDecl>(Member)) {
// Cannot use static subscripts on non-metatypes.
if (!LS.isOnMetatype() && SD->isStatic())
return false;

// Cannot use instance subscript on metatypes.
if (LS.isOnMetatype() && !SD->isStatic() && !LS.isIncludingInstanceMembers())
return false;

return true;
}
if (auto *VD = dyn_cast<VarDecl>(Member)) {
// Cannot use static properties on non-metatypes.
if (!(LS.isQualified() && LS.isOnMetatype()) && VD->isStatic())
Expand Down
67 changes: 67 additions & 0 deletions test/IDE/complete_subscript.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=METATYPE_UNRESOLVED | %FileCheck %s -check-prefix=METATYPE_UNRESOLVED
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=METATYPE_INT | %FileCheck %s -check-prefix=METATYPE_INT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INSTANCE_INT | %FileCheck %s -check-prefix=INSTANCE_INT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=METATYPE_ARCHETYPE | %FileCheck %s -check-prefix=METATYPE_ARCHETYPE
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INSTANCE_ARCHETYPE | %FileCheck %s -check-prefix=INSTANCE_ARCHETYPE
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=METATYPE_LABEL | %FileCheck %s -check-prefix=METATYPE_LABEL
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INSTANCE_LABEL | %FileCheck %s -check-prefix=INSTANCE_LABEL

struct MyStruct<T> {
static subscript(x: Int, static defValue: T) -> MyStruct<T> {
fatalError()
}
subscript(x: Int, instance defValue: T) -> Int {
fatalError()
}
}

func test1() {
let _ = MyStruct #^METATYPE_UNRESOLVED^#
// METATYPE_UNRESOLVED: Begin completions, 4 items
// METATYPE_UNRESOLVED-DAG: Decl[Subscript]/CurrNominal: [{#(x): Int#}, {#static: _#}][#MyStruct<_>#];
// METATYPE_UNRESOLVED-DAG: Decl[Constructor]/CurrNominal: ()[#MyStruct<_>#];
// METATYPE_UNRESOLVED-DAG: Keyword[self]/CurrNominal: .self[#MyStruct<_>.Type#];
// METATYPE_UNRESOLVED-DAG: Keyword/CurrNominal: .Type[#MyStruct<_>.Type#];
// METATYPE_UNRESOLVED: End completions


let _ = MyStruct<Int> #^METATYPE_INT^#
// METATYPE_INT: Begin completions, 4 items
// METATYPE_INT-DAG: Decl[Subscript]/CurrNominal: [{#(x): Int#}, {#static: Int#}][#MyStruct<Int>#];
// METATYPE_INT-DAG: Decl[Constructor]/CurrNominal: ()[#MyStruct<Int>#];
// METATYPE_INT-DAG: Keyword[self]/CurrNominal: .self[#MyStruct<Int>.Type#];
// METATYPE_INT-DAG: Keyword/CurrNominal: .Type[#MyStruct<Int>.Type#];
// METATYPE_INT: End completions

let _ = MyStruct<Int>()#^INSTANCE_INT^#
// INSTANCE_INT: Begin completions, 2 items
// INSTANCE_INT-DAG: Decl[Subscript]/CurrNominal: [{#(x): Int#}, {#instance: Int#}][#Int#];
// INSTANCE_INT-DAG: Keyword[self]/CurrNominal: .self[#MyStruct<Int>#];
// INSTANCE_INT: End completions

}
func test2<U>(value: MyStruct<U>) {
let _ = MyStruct<U>#^METATYPE_ARCHETYPE^#
// METATYPE_ARCHETYPE: Begin completions, 4 items
// METATYPE_ARCHETYPE-DAG: Decl[Subscript]/CurrNominal: [{#(x): Int#}, {#static: U#}][#MyStruct<U>#];
// METATYPE_ARCHETYPE-DAG: Decl[Constructor]/CurrNominal: ()[#MyStruct<U>#];
// METATYPE_ARCHETYPE-DAG: Keyword[self]/CurrNominal: .self[#MyStruct<U>.Type#];
// METATYPE_ARCHETYPE-DAG: Keyword/CurrNominal: .Type[#MyStruct<U>.Type#];
// METATYPE_ARCHETYPE: End completions

let _ = value #^INSTANCE_ARCHETYPE^#
// INSTANCE_ARCHETYPE: Begin completions, 2 items
// INSTANCE_ARCHETYPE-DAG: Decl[Subscript]/CurrNominal: [{#(x): Int#}, {#instance: U#}][#Int#];
// INSTANCE_ARCHETYPE-DAG: Keyword[self]/CurrNominal: .self[#MyStruct<U>#];
// INSTANCE_ARCHETYPE: End completions

let _ = MyStruct<U>[42, #^METATYPE_LABEL^#
// METATYPE_LABEL: Begin completions, 1 items
// METATYPE_LABEL-DAG: Keyword/ExprSpecific: static: [#Argument name#];
// METATYPE_LABEL: End completions

let _ = value[42, #^INSTANCE_LABEL^#
// INSTANCE_LABEL: Begin completions, 1 items
// INSTANCE_LABEL-DAG: Keyword/ExprSpecific: instance: [#Argument name#];
// INSTANCE_LABEL: End completions
}