Skip to content

Commit 7b7c320

Browse files
committed
[CodeCompletion] Allow ErrorType in constructor
Even if the constructor has `ErrorType` we can suggest it as long as it's `FunctionType`. rdar://problem/49480808
1 parent 2587df2 commit 7b7c320

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

lib/IDE/CodeCompletion.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -2559,7 +2559,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
25592559
}
25602560

25612561
// If we won't be able to provide a result, bail out.
2562-
if (MemberType->hasError() && addName.empty() && !needInit)
2562+
if (!ConstructorType && addName.empty() && !needInit)
25632563
return;
25642564

25652565
// Add the constructor, possibly including any default arguments.
@@ -2577,8 +2577,6 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
25772577
Builder.addTextChunk("init");
25782578
} else if (!addName.empty()) {
25792579
Builder.addTextChunk(addName.str());
2580-
} else {
2581-
assert(!MemberType->hasError() && "will insert empty result");
25822580
}
25832581

25842582
if (!ConstructorType) {

test/IDE/complete_constructor.swift

+27
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=AVAILABLE_1 | %FileCheck %s -check-prefix=AVAILABLE_1
5151
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=AVAILABLE_2 | %FileCheck %s -check-prefix=AVAILABLE_1
5252

53+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEPENDENT_IN_CLOSURE_1 | %FileCheck %s -check-prefix=DEPENDENT_IN_CLOSURE_1
54+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEPENDENT_IN_CLOSURE_2 | %FileCheck %s -check-prefix=DEPENDENT_IN_CLOSURE_2
55+
5356
func freeFunc() {}
5457

5558
//===---
@@ -369,3 +372,27 @@ func testAvailable() {
369372

370373
let _ = AvailableTest.init(#^AVAILABLE_2^#
371374
}
375+
376+
protocol DataType {
377+
associatedtype Content
378+
}
379+
class DependentTypeInClosure<Data: DataType> {
380+
init(_ arg: Data, fn: (Data.Content) -> Void) {}
381+
init(arg: Data, fn: () -> Data.Content) {}
382+
}
383+
func testDependentTypeInClosure() {
384+
let _: DependentTypeInClosure = .#^DEPENDENT_IN_CLOSURE_3^#
385+
let _ = DependentTypeInClosure(#^DEPENDENT_IN_CLOSURE_1^#)
386+
// DEPENDENT_IN_CLOSURE_1: Begin completions
387+
// DEPENDENT_IN_CLOSURE_1-DAG: Decl[Constructor]/CurrNominal: ['(']{#(arg): _#}, {#fn: (_.Content) -> Void##(_.Content) -> Void#}[')'][#DependentTypeInClosure<_>#];
388+
// DEPENDENT_IN_CLOSURE_1-DAG: Decl[Constructor]/CurrNominal: ['(']{#arg: _#}, {#fn: () -> _.Content##() -> _.Content#}[')'][#DependentTypeInClosure<_>#];
389+
// DEPENDENT_IN_CLOSURE_1: End completions
390+
391+
let _ = DependentTypeInClosure.#^DEPENDENT_IN_CLOSURE_2^#
392+
// DEPENDENT_IN_CLOSURE_2: Begin completions, 4 items
393+
// DEPENDENT_IN_CLOSURE_2-DAG: Keyword[self]/CurrNominal: self[#DependentTypeInClosure<_>.Type#]; name=self
394+
// DEPENDENT_IN_CLOSURE_2-DAG: Keyword/CurrNominal: Type[#DependentTypeInClosure<_>.Type#]; name=Type
395+
// DEPENDENT_IN_CLOSURE_2-DAG: Decl[Constructor]/CurrNominal: init({#(arg): _#}, {#fn: (_.Content) -> Void##(_.Content) -> Void#})[#DependentTypeInClosure<_>#]; name=init(arg: _, fn: (_.Content) -> Void)
396+
// DEPENDENT_IN_CLOSURE_2-DAG: Decl[Constructor]/CurrNominal: init({#arg: _#}, {#fn: () -> _.Content##() -> _.Content#})[#DependentTypeInClosure<_>#]; name=init(arg: _, fn: () -> _.Content)
397+
// DEPENDENT_IN_CLOSURE_2: End completions
398+
}

test/IDE/complete_crashes.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func rdar22835966() {
154154
}
155155
}
156156

157-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=RDAR_22834017 | %FileCheck %s -check-prefix=INVALID_TYPE_INIT
157+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=RDAR_22834017 | %FileCheck %s -check-prefix=RDAR_22834017
158158
struct Foo {
159159
let a: Anosuchtype
160160
let b: Bnosuchtype
@@ -164,8 +164,9 @@ struct Foo {
164164
func rdar22834017() {
165165
Foo(#^RDAR_22834017^#)
166166
}
167-
// FIXME: We could provide a useful completion here. rdar://problem/22846558
168-
// INVALID_TYPE_INIT-NOT: Begin completions
167+
// RDAR_22834017: Begin completions, 1 items
168+
// RDAR_22834017: Decl[Constructor]/CurrNominal: ['(']{#a: <<error type>>#}, {#b: <<error type>>#}, {#c: <<error type>>#}[')'][#Foo#];
169+
// RDAR_22834017: End completions
169170

170171
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=RDAR_23173692 | %FileCheck %s -check-prefix=RDAR_23173692
171172
func rdar23173692() {

0 commit comments

Comments
 (0)