[CodeCompletion] Don't suggest initializers on 'AnyObject'

'AnyObject' cannot be instantiated, we shouldn't be providing any
initializers on 'AnyObject' type. 'AnyObject(<HERE>' and global
completions with initializers (i.e. `addinitstotoplevel`) showed all
initializers from objc classes.

Note that, 'lookupVisibleMemberDecls()' on 'AnyObject' (i.e.
'AnyObject.<HERE>') doesn't return initializers even before this change.
But 'QualifiedLookup' did.

rdar://93059166
This commit is contained in:
Rintaro Ishizaki
2022-05-12 16:26:33 -07:00
parent 0a133e8df7
commit d32b9c885c
3 changed files with 25 additions and 0 deletions

View File

@@ -1555,6 +1555,11 @@ void CompletionLookup::addConstructorCallsForType(
if (!Sink.addInitsToTopLevel)
return;
// 'AnyObject' is not initializable.
// FIXME: Should we do this in 'AnyObjectLookupRequest'?
if (type->isAnyObject())
return;
assert(CurrDeclContext);
auto results =

View File

@@ -365,6 +365,10 @@ static void collectPossibleCalleesByQualifiedLookup(
if (!baseInstanceTy->mayHaveMembers())
return;
// 'AnyObject' is not initializable.
if (baseInstanceTy->isAnyObject() && name == DeclNameRef::createConstructor())
return;
// Make sure we've resolved implicit members.
namelookup::installSemanticMembersIfNeeded(baseInstanceTy, name);

View File

@@ -56,6 +56,9 @@
// RUN: %FileCheck %s -check-prefix=DL_CLASS_DOT < %t.dl.txt
// RUN: %FileCheck %s -check-prefix=GLOBAL_NEGATIVE < %t.dl.txt
// RUN: %target-swift-ide-test -code-completion -source-filename %s -I %t -disable-objc-attr-requires-foundation-module -code-completion-token=INITIALIZE_PAREN | %FileCheck %s -check-prefix=INITIALIZE_PAREN
// RUN: %target-swift-ide-test -code-completion -source-filename %s -I %t -disable-objc-attr-requires-foundation-module -code-completion-token=GLOBAL_WITHINIT -code-complete-inits-in-postfix-expr | %FileCheck %s -check-prefix=GLOBAL_WITHINIT
// REQUIRES: objc_interop
import foo_swift_module
@@ -491,3 +494,16 @@ func testAnyObjectClassMethods1(_ dl: AnyObject) {
func testAnyObjectClassMethods2(_ dl: AnyObject) {
type(of: dl).#^DL_CLASS_DOT_1^#
}
func testAnyObjectInitialize() {
AnyObject(#^INITIALIZE_PAREN^#)
// INITIALIZE_PAREN-NOT: Flair[ArgLabels]
// INITIALIZE_PAREN-NOT: name=int:
}
func testGlobalInitializer() {
#^GLOBAL_WITHINIT^#
// GLOBAL_WITHINIT-NOT: name=AnyObject(
// GLOBAL_WITHINIT-DAG: Decl[TypeAlias]/OtherModule[Swift]/IsSystem: AnyObject[#Builtin.AnyObject#]; name=AnyObject
// GLOBAL_WITHINIT-NOT: name=AnyObject(
}