Skip to content

Commit 7ef24b1

Browse files
committed
[SR-7789][Additions]
restore accidentally removed constructors from 'self' replace illegal direct call completions on dynamic metatypes with .init, except archetypes.
1 parent 927598f commit 7ef24b1

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,10 +2606,15 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
26062606
assert(isa<ConstructorDecl>(CurrDeclContext) &&
26072607
"can call super.init only inside a constructor");
26082608
needInit = true;
2609-
} else if (addName.empty() && HaveDot) {
2610-
if (Reason == DeclVisibilityKind::MemberOfCurrentNominal ||
2611-
Reason == DeclVisibilityKind::MemberOfSuper ||
2612-
Reason == DeclVisibilityKind::MemberOfProtocolImplementedByCurrentNominal) {
2609+
} else if (addName.empty()) {
2610+
auto ReasonIsValid =
2611+
Reason == DeclVisibilityKind::MemberOfCurrentNominal ||
2612+
Reason == DeclVisibilityKind::MemberOfSuper ||
2613+
Reason == DeclVisibilityKind::MemberOfProtocolImplementedByCurrentNominal;
2614+
auto instTy = ExprType->castTo<AnyMetatypeType>()->getInstanceType();
2615+
2616+
if (ReasonIsValid && (HaveDot ||
2617+
!(instTy->is<ArchetypeType>() || IsStaticMetatype))) {
26132618
// This case is querying the init function as member
26142619
needInit = true;
26152620
}
@@ -2641,7 +2646,6 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
26412646
addTypeAnnotation(Builder, MemberType);
26422647
return;
26432648
}
2644-
assert(ConstructorType);
26452649

26462650
if (!HaveLParen)
26472651
Builder.addLeftParen();
@@ -2959,8 +2963,11 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
29592963
CD->getResultInterfaceType().getPointer()) {
29602964
Result = Ty;
29612965
}
2962-
if (IsStaticMetatype || CD->isRequired() || !Ty->is<ClassType>())
2966+
if (IsStaticMetatype || CD->isRequired() || IsSelfRefExpr ||
2967+
Ty->is<ArchetypeType>() ||
2968+
!(Ty->is<ClassType>() || HaveLParen))
29632969
addConstructorCall(CD, Reason, None, Result);
2970+
return;
29642971
}
29652972
if (IsSuperRefExpr || IsSelfRefExpr) {
29662973
if (!isa<ConstructorDecl>(CurrDeclContext))

test/IDE/complete_after_self.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ class ThisDerived1 : ThisBase1 {
214214
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .derivedFunc0({#self: ThisDerived1#})[#() -> Void#]
215215
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticVar]/CurrNominal: .derivedStaticVar[#Int#]
216216
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: .derivedStaticFunc0()[#Void#]
217+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Constructor]/CurrNominal: .init()[#ThisDerived1#]
218+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Constructor]/CurrNominal: .init({#a: Int#})[#ThisDerived1#]
217219
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .test1({#self: ThisDerived1#})[#() -> Void#]
218220
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .test2({#self: ThisDerived1#})[#() -> Void#]
219221
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: .staticTest1()[#Void#]
@@ -224,6 +226,7 @@ class ThisDerived1 : ThisBase1 {
224226
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Class]/CurrNominal: .DerivedExtNestedClass[#ThisDerived1.DerivedExtNestedClass#]
225227
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Enum]/CurrNominal: .DerivedExtNestedEnum[#ThisDerived1.DerivedExtNestedEnum#]
226228
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[TypeAlias]/CurrNominal: .DerivedExtNestedTypealias[#Int#]
229+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Constructor]/CurrNominal: .init({#someExtensionArg: Int#})[#ThisDerived1#]
227230
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/Super: .baseFunc0({#self: ThisBase1#})[#() -> Void#]
228231
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/Super: .baseFunc1({#self: ThisBase1#})[#(Int) -> Void#]
229232
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticVar]/Super: .baseStaticVar[#Int#]
@@ -245,6 +248,8 @@ class ThisDerived1 : ThisBase1 {
245248
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: derivedFunc0({#self: ThisDerived1#})[#() -> Void#]
246249
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticVar]/CurrNominal: derivedStaticVar[#Int#]
247250
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: derivedStaticFunc0()[#Void#]
251+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Constructor]/CurrNominal: init()[#ThisDerived1#]
252+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Constructor]/CurrNominal: init({#a: Int#})[#ThisDerived1#]
248253
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: test1({#self: ThisDerived1#})[#() -> Void#]
249254
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: test2({#self: ThisDerived1#})[#() -> Void#]
250255
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: staticTest1()[#Void#]
@@ -255,6 +260,7 @@ class ThisDerived1 : ThisBase1 {
255260
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Class]/CurrNominal: DerivedExtNestedClass[#ThisDerived1.DerivedExtNestedClass#]
256261
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Enum]/CurrNominal: DerivedExtNestedEnum[#ThisDerived1.DerivedExtNestedEnum#]
257262
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[TypeAlias]/CurrNominal: DerivedExtNestedTypealias[#Int#]
263+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Constructor]/CurrNominal: init({#someExtensionArg: Int#})[#ThisDerived1#]
258264
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/Super: baseFunc0({#self: ThisBase1#})[#() -> Void#]
259265
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/Super: baseFunc1({#self: ThisBase1#})[#(Int) -> Void#]
260266
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticVar]/Super: baseStaticVar[#Int#]

test/IDE/complete_constructor.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=EXPLICIT_CONSTRUCTORS_SELECTOR_1 | %FileCheck %s -check-prefix=EXPLICIT_CONSTRUCTORS_SELECTOR_1
1515

16+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=EXPLICIT_CONSTRUCTORS_VAL_META_1 | %FileCheck %s -check-prefix=EXPLICIT_CONSTRUCTORS_VAL_META_1
17+
18+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=EXPLICIT_CONSTRUCTORS_VAL_META_2 | %FileCheck %s -check-prefix=EXPLICIT_CONSTRUCTORS_VAL_META_2
19+
1620
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=EXPLICIT_CONSTRUCTORS_BASE_DERIVED_1 | %FileCheck %s -check-prefix=EXPLICIT_CONSTRUCTORS_BASE_DERIVED_1
1721

1822
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_INIT_FROM_PROT_NODOT | %FileCheck %s -check-prefix=DEFAULT_INIT_FROM_PROT
@@ -149,6 +153,17 @@ func testExplicitConstructorsSelector1() {
149153
// EXPLICIT_CONSTRUCTORS_SELECTOR_1: End completions
150154
}
151155

156+
func testExplicitConstructorsValueMetatype() {
157+
var SS = ExplicitConstructorsSelector1.self
158+
SS#^EXPLICIT_CONSTRUCTORS_VAL_META_1^#
159+
SS(#^EXPLICIT_CONSTRUCTORS_VAL_META_2^#
160+
161+
// EXPLICIT_CONSTRUCTORS_VAL_META_1: Decl[Constructor]/CurrNominal: .init({#int: Int#})[#ExplicitConstructorsSelector1#]
162+
// EXPLICIT_CONSTRUCTORS_VAL_META_1: Decl[Constructor]/CurrNominal: .init({#int: Int#}, {#andFloat: Float#})[#ExplicitConstructorsSelector1#]
163+
164+
// EXPLICIT_CONSTRUCTORS_VAL_META_2-NOT: Decl[Constructor]
165+
}
166+
152167
struct ExplicitConstructorsSelector2 {
153168
init(noArgs _ : ()) {}
154169
init(_ a : Int) {}

0 commit comments

Comments
 (0)