Skip to content

[CodeCompletion] Don't suggest initializers on existential types #59265

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
11 changes: 8 additions & 3 deletions lib/IDE/CompletionLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,10 @@ void CompletionLookup::addConstructorCallsForType(
if (!Sink.addInitsToTopLevel)
return;

// Existential types cannot be instantiated. e.g. 'MyProtocol()'.
if (type->isExistentialType())
return;

// 'AnyObject' is not initializable.
// FIXME: Should we do this in 'AnyObjectLookupRequest'?
if (type->isAnyObject())
Expand Down Expand Up @@ -2017,9 +2021,10 @@ void CompletionLookup::foundDecl(ValueDecl *D, DeclVisibilityKind Reason,

if (auto *GP = dyn_cast<GenericTypeParamDecl>(D)) {
addGenericTypeParamRef(GP, Reason, dynamicLookupInfo);
for (auto *protocol : GP->getConformingProtocols())
addConstructorCallsForType(protocol->getDeclaredInterfaceType(),
GP->getName(), Reason, dynamicLookupInfo);
auto type =
CurrDeclContext->mapTypeIntoContext(GP->getDeclaredInterfaceType());
addConstructorCallsForType(type, GP->getName(), Reason,
dynamicLookupInfo);
Comment on lines -2020 to +2027
Copy link
Member Author

@rintaro rintaro Jun 8, 2022

Choose a reason for hiding this comment

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

@bnbarham Could you re-review? I added this to fix an issue for generic parameters:

func foo<MyGeneric: MyProtocol>(_: MyGeneric) {
  <HERE>
}

In this case, since this is a GenericTypeParamDecl, the type passed to addConstructorCallsForType() was a protocol type which is an existential type. But in this case we do want initializers for MyGeneric.

return;
}

Expand Down
12 changes: 9 additions & 3 deletions lib/IDE/ExprContextAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,15 @@ static void collectPossibleCalleesByQualifiedLookup(
if (!baseInstanceTy->mayHaveMembers())
return;

// 'AnyObject' is not initializable.
if (baseInstanceTy->isAnyObject() && name == DeclNameRef::createConstructor())
return;
if (name == DeclNameRef::createConstructor()) {
// Existential types cannot be instantiated. e.g. 'MyProtocol()'.
if (baseInstanceTy->isExistentialType())
return;

// 'AnyObject' is not initializable.
if (baseInstanceTy->isAnyObject())
return;
}

// Make sure we've resolved implicit members.
namelookup::installSemanticMembersIfNeeded(baseInstanceTy, name);
Expand Down
8 changes: 4 additions & 4 deletions lib/Sema/LookupVisibleDecls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ static void lookupVisibleDeclsImpl(VisibleDeclConsumer &Consumer,
if (!isa<PatternBindingInitializer>(DC) ||
!cast<PatternBindingInitializer>(DC)->getInitializedLazyVar())
LS = LS.withOnMetatype();
DC = DC->getParent();
DC = DC->getParentForLookup();
}

// We don't look for generic parameters if we are in the context of a
Expand All @@ -1210,7 +1210,7 @@ static void lookupVisibleDeclsImpl(VisibleDeclConsumer &Consumer,

if (auto *SE = dyn_cast<SubscriptDecl>(DC)) {
ExtendedType = SE->getDeclContext()->getSelfTypeInContext();
DC = DC->getParent();
DC = DC->getParentForLookup();
if (SE->isStatic())
LS = LS.withOnMetatype();
} else if (auto *AFD = dyn_cast<AbstractFunctionDecl>(DC)) {
Expand Down Expand Up @@ -1238,7 +1238,7 @@ static void lookupVisibleDeclsImpl(VisibleDeclConsumer &Consumer,

if (AFD->getDeclContext()->isTypeContext()) {
ExtendedType = AFD->getDeclContext()->getSelfTypeInContext();
DC = DC->getParent();
DC = DC->getParentForLookup();

if (auto *FD = dyn_cast<FuncDecl>(AFD))
if (FD->isStatic())
Expand Down Expand Up @@ -1288,7 +1288,7 @@ static void lookupVisibleDeclsImpl(VisibleDeclConsumer &Consumer,
MemberReason = DeclVisibilityKind::MemberOfOutsideNominal;
}

DC = DC->getParent();
DC = DC->getParentForLookup();
}

if (auto SF = dyn_cast<SourceFile>(DC)) {
Expand Down
5 changes: 2 additions & 3 deletions test/IDE/complete_init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ func testTopLevel() {
// TOP_LEVEL_0-DAG: Decl[Constructor]/CurrModule: E({#x: A#})[#E#]{{; name=.+}}
// TOP_LEVEL_0-DAG: Decl[Constructor]/CurrModule: F({#x: A#})[#F#]{{; name=.+}}
// TOP_LEVEL_0-DAG: Decl[Constructor]/CurrModule: F()[#F#]{{; name=.+}}
// TOP_LEVEL_0-DAG: Decl[Constructor]/CurrModule: G({#x: A#})[#G#]{{; name=.+}}
// TOP_LEVEL_0-DAG: Decl[Constructor]/CurrModule: H({#x: A#})[#H#]{{; name=.+}}
// TOP_LEVEL_0-DAG: Decl[Constructor]/CurrModule: I({#x: A#})[#I#]{{; name=.+}}
// TOP_LEVEL_0-DAG: Decl[Constructor]/CurrModule: I({#y: A#})[#I#]{{; name=.+}}
// TOP_LEVEL_0-DAG: Decl[Constructor]/CurrModule: J()[#A#]{{; name=.+}}
// TOP_LEVEL_0: End completions
// NEGATIVE_TOP_LEVEL_0-NOT: Decl[Constructor]/CurrModule: E()
// NEGATIVE_TOP_LEVEL_0-NOT: Decl[Constructor]/CurrModule: G(
// NEGATIVE_TOP_LEVEL_0-NOT: Decl[Constructor]/CurrModule: I(

func testQualified0() {
K.#^K_QUALIFIED_0^#
Expand Down
45 changes: 45 additions & 0 deletions test/IDE/complete_rdar94369218.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-ide-test -batch-code-completion -code-complete-inits-in-postfix-expr -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t

protocol MyProto {
init(value: String)
}

extension MyProto where Self == MyStruct {
init(arg: String) { self = Self(value: arg) }
}

struct MyStruct: MyProto {
init(value: String) {}
}

func test1() {
#^GLOBALEXPR^#
// GLOBALEXPR: Begin completions
// GLOBALEXPR-NOT: name=MyProto(
// GLOBALEXPR-DAG: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
// GLOBALEXPR-DAG: Decl[Constructor]/CurrModule: MyStruct({#value: String#})[#MyStruct#]; name=MyStruct(value:)
// GLOBALEXPR-DAG: Decl[Constructor]/CurrModule: MyStruct({#arg: String#})[#MyStruct#]; name=MyStruct(arg:)
// GLOBALEXPR-DAG: Decl[Protocol]/CurrModule/Flair[RareType]: MyProto[#MyProto#]; name=MyProto
// GLOBALEXPR-NOT: name=MyProto(
// GLOBALEXPR: End completions
}

func test2() {
_ = MyProto(#^PROTOCOL_AFTER_PAREN^#
// PROTOCOL_AFTER_PAREN: Begin completions
// PROTOCOL_AFTER_PAREN-NOT: name=arg:
// PROTOCOL_AFTER_PAREN-NOT: name=value:
// PROTOCOL_AFTER_PAREN: End completions
}

func test3<MyGeneric: MyProto>() {
class Inner {
func test() {
#^GENERICEXPR^#
// GENERICEXPR: Decl[GenericTypeParam]/Local: MyGeneric[#MyGeneric#]; name=MyGeneric
// GENERICEXPR: Decl[Constructor]/Local: MyGeneric({#value: String#})[#MyProto#]; name=MyGeneric(value:)
// GENERICEXPR: Decl[Constructor]/Local: MyGeneric({#arg: String#})[#MyStruct#]; name=MyGeneric(arg:)
}
}
}