Skip to content

Commit 7287f9a

Browse files
committed
Add tests for non-single-expression closures and remove dead code
Ensure that we get the correct behaviour when we are not in the single-expression case, and remove code that handled 0-element bodies, which is no longer needed after the parser change.
1 parent d0016bf commit 7287f9a

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -716,15 +716,7 @@ class ExprContextAnalyzer {
716716
}
717717

718718
static bool isSingleExpressionBodyForCodeCompletion(BraceStmt *body) {
719-
switch (body->getNumElements()) {
720-
case 0:
721-
// Nothing in the body except the code-completion token.
722-
return true;
723-
case 1:
724-
return body->getElements()[0].is<Expr *>();
725-
default:
726-
return false;
727-
}
719+
return body->getNumElements() == 1 && body->getElements()[0].is<Expr *>();
728720
}
729721

730722
public:

test/IDE/complete_single_expression_return.swift

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TestSingleExprClosureUnresolved | %FileCheck %s -check-prefix=TestSingleExprClosureRetUnresolved
77
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TestSingleExprClosureCall | %FileCheck %s -check-prefix=TestSingleExprClosure
88
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TestSingleExprClosureGlobal | %FileCheck %s -check-prefix=TestSingleExprClosureGlobal
9+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TestNonSingleExprClosureGlobal | %FileCheck %s -check-prefix=TestNonSingleExprClosureGlobal
10+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TestNonSingleExprClosureUnresolved | %FileCheck %s -check-prefix=TestNonSingleExprClosureUnresolved
911

1012
struct TestSingleExprClosureRet {
1113
func void() -> Void {}
@@ -125,7 +127,7 @@ struct TestSingleExprClosureGlobal {
125127
func int() -> Int { return 0 }
126128

127129
func test() -> Int {
128-
return { () -> Int in
130+
return { () in
129131
#^TestSingleExprClosureGlobal^#
130132
}()
131133
}
@@ -135,3 +137,37 @@ struct TestSingleExprClosureGlobal {
135137
// TestSingleExprClosureGlobal-DAG: Decl[InstanceMethod]/OutNominal: void()[#Void#];
136138
// TestSingleExprClosureGlobal: End completions
137139
}
140+
141+
struct TestNonSingleExprClosureGlobal {
142+
func void() -> Void {}
143+
func str() -> String { return "" }
144+
func int() -> Int { return 0 }
145+
146+
func test() -> Int {
147+
return { () in
148+
#^TestNonSingleExprClosureGlobal^#
149+
return 42
150+
}()
151+
}
152+
// TestNonSingleExprClosureGlobal: Begin completions
153+
// TestNonSingleExprClosureGlobal-DAG: Decl[InstanceMethod]/OutNominal: str()[#String#];
154+
// TestNonSingleExprClosureGlobal-DAG: Decl[InstanceMethod]/OutNominal: int()[#Int#];
155+
// TestNonSingleExprClosureGlobal-DAG: Decl[InstanceMethod]/OutNominal: void()[#Void#];
156+
// TestNonSingleExprClosureGlobal: End completions
157+
}
158+
159+
struct TestNonSingleExprClosureUnresolved {
160+
enum MyEnum { case myEnum }
161+
enum NotMine { case notMine }
162+
func mine() -> MyEnum { return .myEnum }
163+
func notMine() -> NotMine { return .notMine }
164+
165+
func test() -> Int {
166+
return { () in
167+
.#^TestNonSingleExprClosureUnresolved^#
168+
return 42
169+
}()
170+
}
171+
// TestNonSingleExprClosureUnresolved-NOT: myEnum
172+
// TestNonSingleExprClosureUnresolved-NOT: notMine
173+
}

0 commit comments

Comments
 (0)