Skip to content

Commit 8246d3f

Browse files
committed
[CodeComplete] Improve ExpectsNonVoid computation
If we know we have a Void expected type, don't set `ExpectsNonVoid` to `true`.
1 parent 4bf5a34 commit 8246d3f

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

lib/IDE/PostfixCompletion.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,23 +189,24 @@ void PostfixCompletionCallback::sawSolutionImpl(
189189

190190
bool BaseIsStaticMetaType = S.isStaticallyDerivedMetatype(ParsedExpr);
191191

192+
bool ExpectsNonVoid = false;
192193
SmallVector<Type, 4> ExpectedTypes;
193194
if (ExpectedTy) {
194195
ExpectedTypes.push_back(ExpectedTy);
195-
}
196-
197-
bool ExpectsNonVoid = false;
198-
ExpectsNonVoid |= ExpectedTy && !ExpectedTy->isVoid();
199-
ExpectsNonVoid |=
200-
!ParentExpr && CS.getContextualTypePurpose(CompletionExpr) != CTP_Unused;
196+
ExpectsNonVoid = !ExpectedTy->isVoid();
197+
} else {
198+
ExpectsNonVoid |= !ParentExpr &&
199+
CS.getContextualTypePurpose(CompletionExpr) != CTP_Unused;
201200

202-
for (auto SAT : S.targets) {
203-
if (ExpectsNonVoid) {
204-
// ExpectsNonVoid is already set. No need to iterate further.
205-
break;
206-
}
207-
if (SAT.second.getAsExpr() == CompletionExpr) {
208-
ExpectsNonVoid |= SAT.second.getExprContextualTypePurpose() != CTP_Unused;
201+
for (auto SAT : S.targets) {
202+
if (ExpectsNonVoid) {
203+
// ExpectsNonVoid is already set. No need to iterate further.
204+
break;
205+
}
206+
if (SAT.second.getAsExpr() == CompletionExpr) {
207+
ExpectsNonVoid |=
208+
SAT.second.getExprContextualTypePurpose() != CTP_Unused;
209+
}
209210
}
210211
}
211212

test/IDE/complete_single_expression_return.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,20 @@ struct TestSingleExprFunc {
190190
// TestSingleExprFunc-DAG: Decl[InstanceMethod]/CurrNominal: void()[#Void#];
191191
}
192192

193+
struct TestSingleExprFuncReturnVoid {
194+
func void() -> Void {}
195+
func str() -> String { return "" }
196+
func int() -> Int { return 0 }
197+
198+
func test() {
199+
return self.#^TestSingleExprFuncReturnVoid^#
200+
}
201+
202+
// TestSingleExprFuncReturnVoid-DAG: Decl[InstanceMethod]/CurrNominal: str()[#String#];
203+
// TestSingleExprFuncReturnVoid-DAG: Decl[InstanceMethod]/CurrNominal: int()[#Int#];
204+
// TestSingleExprFuncReturnVoid-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Convertible]: void()[#Void#];
205+
}
206+
193207
struct TestSingleExprFuncUnresolved {
194208
enum MyEnum { case myEnum }
195209
enum NotMine { case notMine }

0 commit comments

Comments
 (0)