Skip to content

Commit 45c9038

Browse files
committed
corrections and review amendments
1 parent 7ef24b1 commit 45c9038

File tree

5 files changed

+99
-46
lines changed

5 files changed

+99
-46
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,7 +2591,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
25912591

25922592
void addConstructorCall(const ConstructorDecl *CD, DeclVisibilityKind Reason,
25932593
Optional<Type> BaseType, Optional<Type> Result,
2594-
bool IsOnMetatype = true,
2594+
bool IsOnType = true,
25952595
Identifier addName = Identifier()) {
25962596
foundFunction(CD);
25972597
Type MemberType = getTypeOfMember(CD, BaseType);
@@ -2601,23 +2601,11 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
26012601
->castTo<AnyFunctionType>();
26022602

26032603
bool needInit = false;
2604-
if (!IsOnMetatype) {
2604+
if (!IsOnType) {
26052605
assert(addName.empty());
2606-
assert(isa<ConstructorDecl>(CurrDeclContext) &&
2607-
"can call super.init only inside a constructor");
26082606
needInit = true;
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))) {
2618-
// This case is querying the init function as member
2619-
needInit = true;
2620-
}
2607+
} else if (addName.empty() && HaveDot) {
2608+
needInit = true;
26212609
}
26222610

26232611
// If we won't be able to provide a result, bail out.
@@ -2699,7 +2687,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
26992687
if (shouldHideDeclFromCompletionResults(init))
27002688
continue;
27012689
addConstructorCall(cast<ConstructorDecl>(init), Reason, type, None,
2702-
/*IsOnMetatype=*/true, name);
2690+
/*IsOnType=*/true, name);
27032691
}
27042692
}
27052693
}
@@ -2963,16 +2951,27 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
29632951
CD->getResultInterfaceType().getPointer()) {
29642952
Result = Ty;
29652953
}
2966-
if (IsStaticMetatype || CD->isRequired() || IsSelfRefExpr ||
2967-
Ty->is<ArchetypeType>() ||
2968-
!(Ty->is<ClassType>() || HaveLParen))
2969-
addConstructorCall(CD, Reason, None, Result);
2954+
// If the expression type is not a static metatype or an archetype, the base
2955+
// is not a type. Direct call syntax is illegal on values, so we only add
2956+
// initializer completions if we do not have a left parenthesis and either
2957+
// the initializer is required, the base type's instance type is not a class,
2958+
// or this is a 'self' or 'super' reference.
2959+
if (IsStaticMetatype || Ty->is<ArchetypeType>())
2960+
addConstructorCall(CD, Reason, None, Result, /*isOnType*/true);
2961+
else if ((IsSelfRefExpr || IsSuperRefExpr || !Ty->is<ClassType>() ||
2962+
CD->isRequired()) && !HaveLParen)
2963+
addConstructorCall(CD, Reason, None, Result, /*isOnType*/false);
29702964
return;
29712965
}
2972-
if (IsSuperRefExpr || IsSelfRefExpr) {
2973-
if (!isa<ConstructorDecl>(CurrDeclContext))
2966+
if (!HaveLParen) {
2967+
auto CDC = dyn_cast<ConstructorDecl>(CurrDeclContext);
2968+
if (!CDC)
29742969
return;
2975-
addConstructorCall(CD, Reason, None, None, /*IsOnMetatype=*/false);
2970+
// We do not want 'init' completions for 'self' in non-convenience
2971+
// initializers and for 'super' in convenience initializers.
2972+
if ((IsSelfRefExpr && CDC->isConvenienceInit()) ||
2973+
((IsSuperRefExpr && !CDC->isConvenienceInit())))
2974+
addConstructorCall(CD, Reason, None, None, /*IsOnType=*/false);
29762975
}
29772976
return;
29782977
}

test/IDE/complete_after_self.swift

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
// RUN: %FileCheck %s -check-prefix=COMMON_SELF_NO_DOT_1 < %t.self.txt
3030
// RUN: %FileCheck %s -check-prefix=NO_INIT < %t.self.txt
3131

32+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=STATIC_SELF_PAREN > %t.self.txt
33+
// RUN: %FileCheck %s -check-prefix=STATIC_SELF_PAREN < %t.self.txt
34+
3235
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=FUNC_SELF_DOT_1 > %t.self.txt
3336
// RUN: %FileCheck %s -check-prefix=FUNC_SELF_DOT_1 < %t.self.txt
3437
// RUN: %FileCheck %s -check-prefix=COMMON_SELF_DOT_1 < %t.self.txt
@@ -166,9 +169,8 @@ class ThisDerived1 : ThisBase1 {
166169

167170
init() {
168171
self#^CONSTRUCTOR_SELF_NO_DOT_1^#
169-
// CONSTRUCTOR_SELF_NO_DOT_1: Begin completions, 24 items
170-
// CONSTRUCTOR_SELF_NO_DOT_1-DAG: Decl[Constructor]/CurrNominal: .init()[#ThisDerived1#];
171-
// CONSTRUCTOR_SELF_NO_DOT_1-DAG: Decl[Constructor]/CurrNominal: .init({#a: Int#})[#ThisDerived1#];
172+
// CONSTRUCTOR_SELF_NO_DOT_1: Begin completions, 21 items
173+
// CONSTRUCTOR_SELF_NO_DOT_1-NOT: Decl[Constructor]
172174
// CONSTRUCTOR_SELF_NO_DOT_1: End completions
173175
let d: ThisDerived1
174176
d#^CONSTRUCTOR_NONSELF_NO_DOT_1^#
@@ -177,9 +179,8 @@ class ThisDerived1 : ThisBase1 {
177179

178180
init(a : Int) {
179181
self.#^CONSTRUCTOR_SELF_DOT_1^#
180-
// CONSTRUCTOR_SELF_DOT_1: Begin completions, 19 items
181-
// CONSTRUCTOR_SELF_DOT_1-DAG: Decl[Constructor]/CurrNominal: init()[#ThisDerived1#];
182-
// CONSTRUCTOR_SELF_DOT_1-DAG: Decl[Constructor]/CurrNominal: init({#a: Int#})[#ThisDerived1#];
182+
// CONSTRUCTOR_SELF_DOT_1: Begin completions, 16 items
183+
// CONSTRUCTOR_SELF_DOT_1-NOT: Decl[Constructor]
183184

184185
// CONSTRUCTOR_SELF_DOT_1: End completions
185186
let d: ThisDerived1
@@ -275,6 +276,11 @@ class ThisDerived1 : ThisBase1 {
275276
}
276277
}
277278

279+
class func staticTest3() {
280+
self(#^STATIC_SELF_PAREN^#
281+
// STATIC_SELF_PAREN-NOT: Decl[Constructor]
282+
}
283+
278284
extension ThisDerived1 {
279285
var derivedExtProp : Int {
280286
get {
@@ -313,10 +319,9 @@ struct S1 {
313319
init() {}
314320
init(x: Int) {
315321
self.#^STRUCT_CONSTRUCTOR_SELF_DOT_1^#
316-
// STRUCT_CONSTRUCTOR_SELF_DOT_1: Begin completions, 4 items
322+
// STRUCT_CONSTRUCTOR_SELF_DOT_1: Begin completions, 2 items
317323
// STRUCT_CONSTRUCTOR_SELF_DOT_1-DAG: Keyword[self]/CurrNominal: self[#S1#]; name=self
318-
// STRUCT_CONSTRUCTOR_SELF_DOT_1-DAG: Decl[Constructor]/CurrNominal: init()[#S1#];
319-
// STRUCT_CONSTRUCTOR_SELF_DOT_1-DAG: Decl[Constructor]/CurrNominal: init({#x: Int#})[#S1#];
324+
// STRUCT_CONSTRUCTOR_SELF_DOT_1-NOT: Decl[Constructor]
320325
// STRUCT_CONSTRUCTOR_SELF_DOT_1-DAG: Decl[InstanceMethod]/CurrNominal: f()[#Void#];
321326
// STRUCT_CONSTRUCTOR_SELF_DOT_1: End completions
322327
let s: S1

test/IDE/complete_after_super.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@
99
// RUN: %FileCheck %s -check-prefix=COMMON_BASE_A_DOT < %t.super.txt
1010
// RUN: %FileCheck %s -check-prefix=CONSTRUCTOR_SUPER_DOT_1 < %t.super.txt
1111

12+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CONV_CONSTRUCTOR_SUPER_DOT > %t.super.txt
13+
// RUN: %FileCheck %s -check-prefix=CONSTRUCTOR_SUPER_NOINIT < %t.super.txt
14+
15+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CONV_CONSTRUCTOR_SUPER_NO_DOT > %t.super.txt
16+
// RUN: %FileCheck %s -check-prefix=CONSTRUCTOR_SUPER_NOINIT < %t.super.txt
17+
18+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CONV_CONSTRUCTOR_SUPER_PAREN > %t.super.txt
19+
// RUN: %FileCheck %s -check-prefix=CONSTRUCTOR_SUPER_NOINIT < %t.super.txt
20+
21+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CONSTRUCTOR_SUPER_PAREN > %t.super.txt
22+
// RUN: %FileCheck %s -check-prefix=CONSTRUCTOR_SUPER_NOINIT < %t.super.txt
23+
24+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CLASS_FUNC_SUPER_PAREN > %t.super.txt
25+
// RUN: %FileCheck %s -check-prefix=CONSTRUCTOR_SUPER_NOINIT < %t.super.txt
26+
1227
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CONSTRUCTOR_SUPER_INIT_1 > %t.super.txt
1328
// RUN: %FileCheck %s -check-prefix=CONSTRUCTOR_SUPER_INIT_1 < %t.super.txt
1429

@@ -64,6 +79,9 @@
6479
// RUN: %FileCheck %s -check-prefix=FUNC_SUPER_DOT_2 < %t.super.txt
6580
// RUN: %FileCheck %s -check-prefix=NO_CONSTRUCTORS < %t.super.txt
6681

82+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CLASS_FUNC_SUPER_NODOT > %t.super.txt
83+
// RUN: %FileCheck %s -check-prefix=CLASS_FUNC_SUPER_NODOT < %t.super.txt
84+
6785

6886
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SEMANTIC_CONTEXT_OVERRIDDEN_DECL_1 > %t.super.txt
6987
// RUN: %FileCheck %s -check-prefix=SEMANTIC_CONTEXT_OVERRIDDEN_DECL_1 < %t.super.txt
@@ -221,11 +239,19 @@ class SuperDerivedA : SuperBaseA {
221239

222240
init(a: Int) {
223241
super.#^CONSTRUCTOR_SUPER_DOT_1^#
242+
super(#^CONSTRUCTOR_SUPER_PAREN^#
224243
// CONSTRUCTOR_SUPER_DOT_1: Begin completions, 7 items
225244
// CONSTRUCTOR_SUPER_DOT_1-DAG: Decl[Constructor]/CurrNominal: init()[#SuperBaseA#]{{; name=.+$}}
226245
// CONSTRUCTOR_SUPER_DOT_1: End completions
227246
}
228247

248+
convenience init(foo1: Int) {
249+
super.#^CONV_CONSTRUCTOR_SUPER_DOT^#
250+
super#^CONV_CONSTRUCTOR_SUPER_NO_DOT^#
251+
super(#^CONV_CONSTRUCTOR_SUPER_PAREN^#
252+
// CONSTRUCTOR_SUPER_NOINIT-NOT: Decl[Constructor]
253+
}
254+
229255
init (a: Float) {
230256
super.init#^CONSTRUCTOR_SUPER_INIT_1^#
231257
// CONSTRUCTOR_SUPER_INIT_1: Begin completions
@@ -411,6 +437,15 @@ class SuperDerivedB : SuperBaseB {
411437
// FUNC_SUPER_DOT_2: Begin completions, 6 items
412438
// FUNC_SUPER_DOT_2: End completions
413439
}
440+
441+
class func test3() {
442+
super#^CLASS_FUNC_SUPER_NODOT^#
443+
super(#^CLASS_FUNC_SUPER_PAREN^#
444+
445+
// CLASS_FUNC_SUPER_NODOT: Decl[Constructor]/CurrNominal: .init()[#SuperBaseB#]
446+
// CLASS_FUNC_SUPER_NODOT: Decl[Constructor]/CurrNominal: .init({#a: Double#})[#SuperBaseB#]
447+
// CLASS_FUNC_SUPER_NODOT: Decl[Constructor]/CurrNominal: .init({#int: Int#})[#SuperBaseB#]
448+
}
414449
}
415450

416451
//===--- Check that we assign a special semantic context to the overridden decl.

test/IDE/complete_constructor.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_INIT_FROM_PROT_PAREN | %FileCheck %s -check-prefix=DEFAULT_INIT_FROM_PROT
2727

2828
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE1 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE1
29-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE2_1 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE2
30-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE2_2 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE2
31-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE2_3 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE2
29+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE2_1 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE2_NOINIT
30+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE2_2 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE2_NOINIT
31+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE2_3 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE2_NOINIT
32+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE2_4 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE2_SHOW_INIT
33+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE2_5 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE2_SHOW_INIT
34+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE2_6 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE2_NOINIT
3235
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE3 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE3
3336
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE4 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE4
3437
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FROM_METATYPE5 | %FileCheck %s -check-prefix=INIT_FROM_METATYPE4
@@ -220,13 +223,20 @@ func testGetInitFromMetatype1() {
220223
// INIT_FROM_METATYPE1-NEXT: End completions
221224

222225
func testGetInitFromMetatype2() {
223-
var SS = ExplicitConstructorsBase1.self
224-
SS.#^INIT_FROM_METATYPE2_1^#
225-
SS#^INIT_FROM_METATYPE2_2^#
226-
SS(#^INIT_FROM_METATYPE2_3^#
227-
}
228-
229-
// INIT_FROM_METATYPE2-NOT: Decl[Constructor]
226+
var S1 = ExplicitConstructorsBase1.self
227+
var S2 = ExplicitConstructorsDerived2.self
228+
S1.#^INIT_FROM_METATYPE2_1^#
229+
S1#^INIT_FROM_METATYPE2_2^#
230+
S1(#^INIT_FROM_METATYPE2_3^#
231+
S2.#^INIT_FROM_METATYPE2_4^#
232+
S2#^INIT_FROM_METATYPE2_5^#
233+
S2(#^INIT_FROM_METATYPE2_6^#
234+
}
235+
// INIT_FROM_METATYPE2_NOINIT-NOT: Decl[Constructor]
236+
237+
// INIT_FROM_METATYPE2_SHOW_INIT-NOT: Decl[Constructor]
238+
// INIT_FROM_METATYPE2_SHOW_INIT: Decl[Constructor]/CurrNominal: {{init|.init}}({#a: Int#})[#ExplicitConstructorsDerived2#]
239+
// INIT_FROM_METATYPE2_SHOW_INIT-NOT: Decl[Constructor]
230240

231241
func testGetInitFromMetatype3() {
232242
var SS = ExplicitConstructorsBase1.self

test/IDE/complete_value_expr.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@
134134
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PROTOCOL_EXT_TA_2 | %FileCheck %s -check-prefix=PROTOCOL_EXT_TA
135135
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PROTOCOL_EXT_INIT_1 | %FileCheck %s -check-prefix=PROTOCOL_EXT_INIT_1
136136
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PROTOCOL_EXT_INIT_2 | %FileCheck %s -check-prefix=PROTOCOL_EXT_INIT_2
137+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PROTOCOL_EXT_INIT_3 | %FileCheck %s -check-prefix=PROTOCOL_EXT_INIT_3
138+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PROTOCOL_EXT_INIT_4 | %FileCheck %s -check-prefix=PROTOCOL_EXT_INIT_4
137139
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PROTOCOL_EXT_P4_DOT_1 | %FileCheck %s -check-prefix=PROTOCOL_EXT_P4_DOT
138140
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PROTOCOL_EXT_P4_DOT_2 | %FileCheck %s -check-prefix=PROTOCOL_EXT_P4_DOT
139141
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PROTOCOL_EXT_P4_T_DOT_1 | %FileCheck %s -check-prefix=PROTOCOL_EXT_P4_T_DOT_1
@@ -1637,11 +1639,13 @@ func testProtExtInit1() {
16371639
16381640
func testProtExtInit2<S: P4 where S.T : P1>() {
16391641
S(#^PROTOCOL_EXT_INIT_2^#
1642+
S.#^PROTOCOL_EXT_INIT_3^#
1643+
S#^PROTOCOL_EXT_INIT_4^#
16401644
}
16411645
1642-
// PROTOCOL_EXT_INIT_2: Begin completions
1643-
// PROTOCOL_EXT_INIT_2: Decl[Constructor]/Super: ['(']{#x: Int#}[')'][#P4#]{{; name=.+$}}
1644-
// PROTOCOL_EXT_INIT_2: End completions
1646+
// PROTOCOL_EXT_INIT_2: Decl[Constructor]/Super: ['(']{#x: Int#}[')'][#P4#]{{; name=.+$}}
1647+
// PROTOCOL_EXT_INIT_3: Decl[Constructor]/Super: init({#x: Int#})[#P4#]{{; name=.+$}}
1648+
// PROTOCOL_EXT_INIT_4: Decl[Constructor]/Super: ({#x: Int#})[#P4#]{{; name=.+$}}
16451649
16461650
extension P4 where Self.T == OnlyMe {
16471651
final func test1() {

0 commit comments

Comments
 (0)