Skip to content

Commit 343fcc0

Browse files
committed
[CodeCompletion] Migrate yield completions to solver-based
We didn’t actually have any tests for this. Completions aren’t great here at the moment but since this is an underscored language feature it’s not that important at the moment.
1 parent 23cfd23 commit 343fcc0

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,8 @@ bool CodeCompletionCallbacksImpl::trySolverCompletion(bool MaybeFuncBody) {
15551555
case CompletionKind::ForEachSequence:
15561556
case CompletionKind::PostfixExprBeginning:
15571557
case CompletionKind::StmtOrExpr:
1558-
case CompletionKind::ReturnStmtExpr: {
1558+
case CompletionKind::ReturnStmtExpr:
1559+
case CompletionKind::YieldStmtExpr: {
15591560
assert(CurDeclContext);
15601561

15611562
bool AddUnresolvedMemberCompletions =
@@ -1728,6 +1729,7 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
17281729
case CompletionKind::PostfixExprParen:
17291730
case CompletionKind::PostfixExpr:
17301731
case CompletionKind::ReturnStmtExpr:
1732+
case CompletionKind::YieldStmtExpr:
17311733
llvm_unreachable("should be already handled");
17321734
return;
17331735

@@ -1976,19 +1978,6 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
19761978
break;
19771979
}
19781980

1979-
case CompletionKind::YieldStmtExpr: {
1980-
SourceLoc Loc = P.Context.SourceMgr.getIDEInspectionTargetLoc();
1981-
if (auto FD = dyn_cast<AccessorDecl>(CurDeclContext)) {
1982-
if (FD->isCoroutine()) {
1983-
// TODO: handle multi-value yields.
1984-
Lookup.setExpectedTypes(FD->getStorage()->getValueInterfaceType(),
1985-
/*isImplicitSingleExpressionReturn*/ false);
1986-
}
1987-
}
1988-
Lookup.getValueCompletionsInDeclContext(Loc);
1989-
break;
1990-
}
1991-
19921981
case CompletionKind::AfterPoundDirective: {
19931982
addPoundDirectives(CompletionContext.getResultSink());
19941983

lib/IDE/ExprCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void ExprTypeCheckCompletionCallback::sawSolutionImpl(
7878
auto &CS = S.getConstraintSystem();
7979

8080
Type ExpectedTy;
81-
if (auto ContextualType = CS.getContextualType(CompletionExpr, /*forConstraint=*/false)) {
81+
if (auto ContextualType = S.getContextualType(CompletionExpr)) {
8282
ExpectedTy = ContextualType;
8383
} else {
8484
ExpectedTy = getTypeForCompletion(S, CompletionExpr);

lib/IDE/PostfixCompletion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,12 @@ void PostfixCompletionCallback::sawSolutionImpl(
177177

178178
auto *Locator = CS.getConstraintLocator(SemanticExpr);
179179
Type ExpectedTy;
180-
Expr *ParentExpr = CS.getParentExpr(CompletionExpr);
181-
if (auto ContextualType = CS.getContextualType(CompletionExpr, /*forConstraint=*/false)) {
180+
if (auto ContextualType = S.getContextualType(CompletionExpr)) {
182181
ExpectedTy = ContextualType;
183182
} else {
184183
ExpectedTy = getTypeForCompletion(S, CompletionExpr);
185184
}
185+
Expr *ParentExpr = CS.getParentExpr(CompletionExpr);
186186

187187
auto *CalleeLocator = S.getCalleeLocator(Locator);
188188
ValueDecl *ReferencedDecl = nullptr;

lib/Parse/ParseStmt.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -880,9 +880,10 @@ ParserResult<Stmt> Parser::parseStmtYield(SourceLoc tryLoc) {
880880
}
881881

882882
auto expr = parseExpr(diag::expected_expr_yield);
883-
if (expr.hasCodeCompletion())
884-
return makeParserCodeCompletionResult<Stmt>();
885-
if (expr.isParseErrorOrHasCompletion()) {
883+
if (expr.hasCodeCompletion() && expr.isNonNull()) {
884+
status |= expr;
885+
yields.push_back(expr.get());
886+
} else if (expr.isParseErrorOrHasCompletion()) {
886887
auto endLoc = (Tok.getLoc() == beginLoc ? beginLoc : PreviousLoc);
887888
yields.push_back(
888889
new (Context) ErrorExpr(SourceRange(beginLoc, endLoc)));

test/IDE/complete_yield.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t
3+
4+
struct YieldVariables {
5+
var stringValue: String
6+
7+
var property: String {
8+
_read {
9+
yield #^IN_READ^#
10+
// IN_READ-DAG: Decl[InstanceVar]/CurrNominal/TypeRelation[Convertible]: stringValue[#String#]; name=stringValue
11+
}
12+
_modify {
13+
var localString = ""
14+
yield #^IN_MODIFY^#
15+
// IN_MODIFY-DAG: Decl[InstanceVar]/CurrNominal: stringValue[#String#]; name=stringValue
16+
// IN_MODIFY-DAG: Decl[LocalVar]/Local: localString[#String#]; name=localString
17+
}
18+
}
19+
20+
var otherProperty: String {
21+
_modify {
22+
var localString = ""
23+
yield &#^IN_MODIFY_WITH_REF^#
24+
// FIXME: We should probably be returning a convertible type relation here.
25+
// IN_MODIFY_WITH_REF: Decl[LocalVar]/Local: localString[#String#]; name=localString
26+
// IN_MODIFY_WITH_REF: Decl[InstanceVar]/CurrNominal: stringValue[#String#]; name=stringValue
27+
}
28+
}
29+
}
30+

0 commit comments

Comments
 (0)