Skip to content

Commit 11df457

Browse files
committed
[CodeComplete] Complete parameter labels in self.init call
The `init` of `self.init` needs to be looked up on the Metatype of `self`, not on the type of `self` itself. We were already doing a similar special-casing for `super.init`, which I extened to also cover `self.init`. Resolves rdar://36521732
1 parent 3d2fd56 commit 11df457

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,20 @@ static void collectPossibleCalleesByQualifiedLookup(
551551
}
552552
baseTy = baseTy->getWithoutSpecifierType();
553553

554-
// Use metatype for lookup 'super.init' if it's inside constructors.
555-
if (isa<SuperRefExpr>(baseExpr) && isa<ConstructorDecl>(DC) &&
556-
name == DeclNameRef::createConstructor())
557-
baseTy = MetatypeType::get(baseTy);
554+
// Use metatype for lookup 'super.init' and 'self.init' if it's inside
555+
// constructors.
556+
if (name == DeclNameRef::createConstructor() && isa<ConstructorDecl>(DC)) {
557+
bool isSuperCall = isa<SuperRefExpr>(baseExpr);
558+
bool isSelfCall = false;
559+
if (auto declRef = dyn_cast<DeclRefExpr>(baseExpr)) {
560+
if (declRef->getDecl()->getName() == DC.getASTContext().Id_self) {
561+
isSelfCall = true;
562+
}
563+
}
564+
if (isSuperCall || isSelfCall) {
565+
baseTy = MetatypeType::get(baseTy);
566+
}
567+
}
558568

559569
collectPossibleCalleesByQualifiedLookup(DC, baseTy, name, candidates);
560570

test/IDE/complete_call_arg.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=VARIADIC_2 | %FileCheck %s -check-prefix=VARIADIC_2
115115
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=VARIADIC_3 | %FileCheck %s -check-prefix=VARIADIC_2
116116

117+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=LABEL_IN_SELF_DOT_INIT | %FileCheck %s -check-prefix=LABEL_IN_SELF_DOT_INIT
118+
117119
var i1 = 1
118120
var i2 = 2
119121
var oi1 : Int?
@@ -909,3 +911,15 @@ func testVariadic(_ arg: Any..., option1: Int = 0, option2: String = 1) {
909911
testVariadic(1, 2, #^VARIADIC_3^#)
910912
// Same as VARIADIC_2.
911913
}
914+
915+
func testLabelsInSelfDotInit() {
916+
class Foo {
917+
init(a: Int, b: Int) {}
918+
convenience init() {
919+
self.init(a: 1, #^LABEL_IN_SELF_DOT_INIT^#)
920+
// LABEL_IN_SELF_DOT_INIT: Begin completions, 1 item
921+
// LABEL_IN_SELF_DOT_INIT-DAG: Pattern/ExprSpecific: {#b: Int#}[#Int#]
922+
// LABEL_IN_SELF_DOT_INIT: End completions
923+
}
924+
}
925+
}

0 commit comments

Comments
 (0)