Skip to content

[CodeCompletion] Allow ErrorType in constructor #24727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2559,7 +2559,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
}

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

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

if (!ConstructorType) {
Expand Down
27 changes: 27 additions & 0 deletions test/IDE/complete_constructor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=AVAILABLE_1 | %FileCheck %s -check-prefix=AVAILABLE_1
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=AVAILABLE_2 | %FileCheck %s -check-prefix=AVAILABLE_1

// 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
// 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

func freeFunc() {}

//===---
Expand Down Expand Up @@ -369,3 +372,27 @@ func testAvailable() {

let _ = AvailableTest.init(#^AVAILABLE_2^#
}

protocol DataType {
associatedtype Content
}
class DependentTypeInClosure<Data: DataType> {
init(_ arg: Data, fn: (Data.Content) -> Void) {}
init(arg: Data, fn: () -> Data.Content) {}
}
func testDependentTypeInClosure() {
let _: DependentTypeInClosure = .#^DEPENDENT_IN_CLOSURE_3^#
let _ = DependentTypeInClosure(#^DEPENDENT_IN_CLOSURE_1^#)
// DEPENDENT_IN_CLOSURE_1: Begin completions
// DEPENDENT_IN_CLOSURE_1-DAG: Decl[Constructor]/CurrNominal: ['(']{#(arg): _#}, {#fn: (_.Content) -> Void##(_.Content) -> Void#}[')'][#DependentTypeInClosure<_>#];
// DEPENDENT_IN_CLOSURE_1-DAG: Decl[Constructor]/CurrNominal: ['(']{#arg: _#}, {#fn: () -> _.Content##() -> _.Content#}[')'][#DependentTypeInClosure<_>#];
// DEPENDENT_IN_CLOSURE_1: End completions

let _ = DependentTypeInClosure.#^DEPENDENT_IN_CLOSURE_2^#
// DEPENDENT_IN_CLOSURE_2: Begin completions, 4 items
// DEPENDENT_IN_CLOSURE_2-DAG: Keyword[self]/CurrNominal: self[#DependentTypeInClosure<_>.Type#]; name=self
// DEPENDENT_IN_CLOSURE_2-DAG: Keyword/CurrNominal: Type[#DependentTypeInClosure<_>.Type#]; name=Type
// DEPENDENT_IN_CLOSURE_2-DAG: Decl[Constructor]/CurrNominal: init({#(arg): _#}, {#fn: (_.Content) -> Void##(_.Content) -> Void#})[#DependentTypeInClosure<_>#]; name=init(arg: _, fn: (_.Content) -> Void)
// DEPENDENT_IN_CLOSURE_2-DAG: Decl[Constructor]/CurrNominal: init({#arg: _#}, {#fn: () -> _.Content##() -> _.Content#})[#DependentTypeInClosure<_>#]; name=init(arg: _, fn: () -> _.Content)
// DEPENDENT_IN_CLOSURE_2: End completions
}
7 changes: 4 additions & 3 deletions test/IDE/complete_crashes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func rdar22835966() {
}
}

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

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