Skip to content

Commit b512ab2

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 2b5a8a5 commit b512ab2

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
@@ -714,15 +714,7 @@ class ExprContextAnalyzer {
714714
}
715715

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

728720
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)