Skip to content

Commit 17c9b2b

Browse files
committed
IDE: Don't store TypeVariableTypes into CodeCompletionExprs
Instead, generate the type variable in ConstraintGenerator. However, we only want to generate it if we're type checking from inside TypeChecker::typeCheckCompletionSequence(), so add an isActivated() flag to CodeCompletionExpr. If it is not set, constraint generation will simply fail on an expression containing a CodeCompletionExpr.
1 parent 5fc07b8 commit 17c9b2b

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

include/swift/AST/Expr.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,13 +717,20 @@ class ErrorExpr : public Expr {
717717
/// can help us preserve the context of the code completion position.
718718
class CodeCompletionExpr : public Expr {
719719
SourceRange Range;
720+
bool Activated;
721+
720722
public:
721723
CodeCompletionExpr(SourceRange Range, Type Ty = Type()) :
722724
Expr(ExprKind::CodeCompletion, /*Implicit=*/true, Ty),
723-
Range(Range) {}
725+
Range(Range) {
726+
Activated = false;
727+
}
724728

725729
SourceRange getSourceRange() const { return Range; }
726730

731+
bool isActivated() const { return Activated; }
732+
void setActivated() { Activated = true; }
733+
727734
static bool classof(const Expr *E) {
728735
return E->getKind() == ExprKind::CodeCompletion;
729736
}

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,8 @@ namespace {
19121912

19131913
Expr *visitCodeCompletionExpr(CodeCompletionExpr *expr) {
19141914
// Do nothing with code completion expressions.
1915+
auto toType = simplifyType(cs.getType(expr));
1916+
cs.setType(expr, toType);
19151917
return expr;
19161918
}
19171919

lib/Sema/CSGen.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,8 +1164,11 @@ namespace {
11641164
}
11651165

11661166
virtual Type visitCodeCompletionExpr(CodeCompletionExpr *E) {
1167-
// If the expression has already been assigned a type; just use that type.
1168-
return E->getType();
1167+
if (!E->isActivated())
1168+
return Type();
1169+
1170+
return CS.createTypeVariable(CS.getConstraintLocator(E),
1171+
TVO_CanBindToLValue);
11691172
}
11701173

11711174
Type visitLiteralExpr(LiteralExpr *expr) {

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,9 +2089,7 @@ bool TypeChecker::typeCheckCompletionSequence(Expr *&expr, DeclContext *DC) {
20892089
assert(exprAsBinOp == expr && "found wrong expr?");
20902090

20912091
// Add type variable for the code-completion expression.
2092-
auto tvRHS =
2093-
CS.createTypeVariable(CS.getConstraintLocator(CCE), TVO_CanBindToLValue);
2094-
CCE->setType(tvRHS);
2092+
CCE->setActivated();
20952093

20962094
if (auto generated = CS.generateConstraints(expr)) {
20972095
expr = generated;

0 commit comments

Comments
 (0)