Skip to content

[5.0][CodeCompletion] Rework getOperatorCompletions() #20785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions include/swift/Sema/IDETypeChecking.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,16 @@ namespace swift {
Expr *&parsedExpr,
ConcreteDeclRef &referencedDecl);

/// Typecheck the sequence expression \p parsedExpr for code completion.
/// Resolve type of operator function with \c opName appending it to \c LHS.
///
/// This requires that \p parsedExpr is a SequenceExpr and that it contains:
/// * ... leading sequence LHS
/// * UnresolvedDeclRefExpr operator
/// * CodeCompletionExpr RHS
///
/// On success, returns false, and replaces parsedExpr with the binary
/// expression corresponding to the operator. The type of the operator and
/// RHS are also set, but the rest of the expression may not be typed
///
/// The LHS should already be type-checked or this will be very slow.
bool typeCheckCompletionSequence(DeclContext *DC, Expr *&parsedExpr);
/// For \p refKind, use \c DeclRefKind::PostfixOperator for postfix operator,
/// or \c DeclRefKind::BinaryOperator for infix operator.
/// On success, returns resolved function type of the operator. The LHS should
/// already be type-checked. This function guarantees LHS not to be modified.
FunctionType *getTypeOfCompletionOperator(DeclContext *DC, Expr *LHS,
Identifier opName,
DeclRefKind refKind,
ConcreteDeclRef &referencedDecl);

/// Typecheck the given expression.
bool typeCheckExpression(DeclContext *DC, Expr *&parsedExpr);
Expand Down
212 changes: 45 additions & 167 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3291,35 +3291,15 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
}

void tryPostfixOperator(Expr *expr, PostfixOperatorDecl *op) {
auto Ty = expr->getType();
if (!Ty)
ConcreteDeclRef referencedDecl;
FunctionType *funcTy = getTypeOfCompletionOperator(
const_cast<DeclContext *>(CurrDeclContext), expr, op->getName(),
DeclRefKind::PostfixOperator, referencedDecl);
if (!funcTy)
return;

SWIFT_DEFER {
// Restore type.
// FIXME: This is workaround for getTypeOfExpressionWithoutApplying()
// modifies type of 'expr'.
expr->setType(Ty);
prepareForRetypechecking(expr);
};

// We allocate these expressions on the stack because we know they can't
// escape and there isn't a better way to allocate scratch Expr nodes.
UnresolvedDeclRefExpr UDRE(op->getName(), DeclRefKind::PostfixOperator,
DeclNameLoc(expr->getSourceRange().End));
ParenExpr parenExpr(expr->getSourceRange().Start, expr,
expr->getSourceRange().End,
/*hasTrailingClosure=*/false);
PostfixUnaryExpr opExpr(&UDRE, &parenExpr);
Expr *tempExpr = &opExpr;
ConcreteDeclRef referencedDecl;
if (auto T = getTypeOfCompletionContextExpr(
CurrDeclContext->getASTContext(),
const_cast<DeclContext *>(CurrDeclContext),
CompletionTypeCheckKind::Normal,
tempExpr,
referencedDecl))
addPostfixOperatorCompletion(op, *T);
// TODO: Use referencedDecl (FuncDecl) instead of 'op' (OperatorDecl).
addPostfixOperatorCompletion(op, funcTy->getResult());
}

void addAssignmentOperator(Type RHSType, Type resultType) {
Expand Down Expand Up @@ -3366,169 +3346,67 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
addTypeAnnotation(builder, resultType);
}

void tryInfixOperatorCompletion(InfixOperatorDecl *op, SequenceExpr *SE) {
if (op->getName().str() == "~>")
return;

MutableArrayRef<Expr *> sequence = SE->getElements();
assert(sequence.size() >= 3 && !sequence.back() &&
!sequence.drop_back(1).back() && "sequence not cleaned up");
assert((sequence.size() & 1) && "sequence expr ending with operator");

// FIXME: these checks should apply to the LHS of the operator, not the
// immediately left expression. Move under the type-checking.
Expr *LHS = sequence.drop_back(2).back();
if (LHS->getType() && (LHS->getType()->is<MetatypeType>() ||
LHS->getType()->is<AnyFunctionType>()))
void tryInfixOperatorCompletion(Expr *foldedExpr, InfixOperatorDecl *op) {
ConcreteDeclRef referencedDecl;
FunctionType *funcTy = getTypeOfCompletionOperator(
const_cast<DeclContext *>(CurrDeclContext), foldedExpr, op->getName(),
DeclRefKind::BinaryOperator, referencedDecl);
if (!funcTy)
return;

// Preserve LHS type for restoring it.
Type LHSTy = LHS->getType();

// We allocate these expressions on the stack because we know they can't
// escape and there isn't a better way to allocate scratch Expr nodes.
UnresolvedDeclRefExpr UDRE(op->getName(), DeclRefKind::BinaryOperator,
DeclNameLoc(LHS->getEndLoc()));
sequence.drop_back(1).back() = &UDRE;
CodeCompletionExpr CCE(LHS->getSourceRange());
sequence.back() = &CCE;

SWIFT_DEFER {
// Reset sequence.
SE->setElement(SE->getNumElements() - 1, nullptr);
SE->setElement(SE->getNumElements() - 2, nullptr);
LHS->setType(LHSTy);
prepareForRetypechecking(SE);

for (auto &element : sequence.drop_back(2)) {
// Unfold expressions for re-typechecking sequence.
if (auto *assignExpr = dyn_cast_or_null<AssignExpr>(element)) {
assignExpr->setSrc(nullptr);
assignExpr->setDest(nullptr);
} else if (auto *ifExpr = dyn_cast_or_null<IfExpr>(element)) {
ifExpr->setCondExpr(nullptr);
ifExpr->setElseExpr(nullptr);
}

// Reset any references to operators in types, so they are properly
// handled as operators by sequence folding.
//
// FIXME: Would be better to have some kind of 'OperatorRefExpr'?
if (auto operatorRef = element->getMemberOperatorRef()) {
operatorRef->setType(nullptr);
element = operatorRef;
}
}
};
Type lhsTy = funcTy->getParams()[0].getPlainType();
Type rhsTy = funcTy->getParams()[1].getPlainType();
Type resultTy = funcTy->getResult();

Expr *expr = SE;
if (!typeCheckCompletionSequence(const_cast<DeclContext *>(CurrDeclContext),
expr)) {
if (!LHS->getType() ||
!LHS->getType()->getRValueType()->getOptionalObjectType()) {
// Don't complete optional operators on non-optional types.
// FIXME: can we get the type-checker to disallow these for us?
if (op->getName().str() == "??")
// Don't complete optional operators on non-optional types.
if (!lhsTy->getRValueType()->getOptionalObjectType()) {
// 'T ?? T'
if (op->getName().str() == "??")
return;
// 'T == nil'
if (auto NT = rhsTy->getNominalOrBoundGenericNominal())
if (NT->getName() ==
CurrDeclContext->getASTContext().Id_OptionalNilComparisonType)
return;
if (auto NT = CCE.getType()->getNominalOrBoundGenericNominal()) {
if (NT->getName() ==
CurrDeclContext->getASTContext().Id_OptionalNilComparisonType)
return;
}
}
}

// If the right-hand side and result type are both type parameters, we're
// not providing a useful completion.
if (expr->getType()->isTypeParameter() &&
CCE.getType()->isTypeParameter())
return;
// If the right-hand side and result type are both type parameters, we're
// not providing a useful completion.
if (resultTy->isTypeParameter() && rhsTy->isTypeParameter())
return;

addInfixOperatorCompletion(op, expr->getType(), CCE.getType());
}
}

void flattenBinaryExpr(Expr *expr, SmallVectorImpl<Expr *> &sequence) {
if (auto binExpr = dyn_cast<BinaryExpr>(expr)) {
flattenBinaryExpr(binExpr->getArg()->getElement(0), sequence);
sequence.push_back(binExpr->getFn());
flattenBinaryExpr(binExpr->getArg()->getElement(1), sequence);
} else if (auto assignExpr = dyn_cast<AssignExpr>(expr)) {
flattenBinaryExpr(assignExpr->getDest(), sequence);
sequence.push_back(assignExpr);
flattenBinaryExpr(assignExpr->getSrc(), sequence);
assignExpr->setDest(nullptr);
assignExpr->setSrc(nullptr);
} else if (auto ifExpr = dyn_cast<IfExpr>(expr)) {
flattenBinaryExpr(ifExpr->getCondExpr(), sequence);
sequence.push_back(ifExpr);
flattenBinaryExpr(ifExpr->getElseExpr(), sequence);
ifExpr->setCondExpr(nullptr);
ifExpr->setElseExpr(nullptr);
} else if (auto tryExpr = dyn_cast<AnyTryExpr>(expr)) {
// Strip out try expression. It doesn't affect completion.
flattenBinaryExpr(tryExpr->getSubExpr(), sequence);
} else if (auto optEval = dyn_cast<OptionalEvaluationExpr>(expr)){
// Strip out optional evaluation expression. It doesn't affect completion.
flattenBinaryExpr(optEval->getSubExpr(), sequence);
} else {
sequence.push_back(expr);
}
// TODO: Use referencedDecl (FuncDecl) instead of 'op' (OperatorDecl).
addInfixOperatorCompletion(op, funcTy->getResult(),
funcTy->getParams()[1].getPlainType());
}

void typeCheckLeadingSequence(SmallVectorImpl<Expr *> &sequence) {
Expr *typeCheckLeadingSequence(Expr *LHS, ArrayRef<Expr *> leadingSequence) {
if (leadingSequence.empty())
return LHS;

// Strip out try and optional evaluation expr because foldSequence() mutates
// hierarchy of these expressions. They don't affect completion anyway.
for (auto &element : sequence) {
if (auto *tryExpr = dyn_cast<AnyTryExpr>(element))
element = tryExpr->getSubExpr();
if (auto *optEval = dyn_cast<OptionalEvaluationExpr>(element))
element = optEval->getSubExpr();
}
assert(leadingSequence.size() % 2 == 0);
SmallVector<Expr *, 3> sequence(leadingSequence.begin(),
leadingSequence.end());
sequence.push_back(LHS);

Expr *expr =
SequenceExpr::create(CurrDeclContext->getASTContext(), sequence);
prepareForRetypechecking(expr);
// Take advantage of the fact the type-checker leaves the types on the AST.
if (!typeCheckExpression(const_cast<DeclContext *>(CurrDeclContext),
expr)) {
// Rebuild the sequence from the type-checked version.
sequence.clear();
flattenBinaryExpr(expr, sequence);
return;
return expr;
}

// Fall back to just using the immediate LHS.
auto LHS = sequence.back();
sequence.clear();
sequence.push_back(LHS);
return LHS;
}

void getOperatorCompletions(Expr *LHS, ArrayRef<Expr *> leadingSequence) {
std::vector<OperatorDecl *> operators = collectOperators();
Expr *foldedExpr = typeCheckLeadingSequence(LHS, leadingSequence);

std::vector<OperatorDecl *> operators = collectOperators();
// FIXME: this always chooses the first operator with the given name.
llvm::DenseSet<Identifier> seenPostfixOperators;
llvm::DenseSet<Identifier> seenInfixOperators;

SmallVector<Expr *, 3> sequence(leadingSequence.begin(),
leadingSequence.end());
sequence.push_back(LHS);
assert((sequence.size() & 1) && "sequence expr ending with operator");

if (sequence.size() > 1)
typeCheckLeadingSequence(sequence);

// Retrieve typechecked LHS.
LHS = sequence.back();

// Create a single sequence expression, which we will modify for each
// operator, filling in the operator and dummy right-hand side.
sequence.push_back(nullptr); // operator
sequence.push_back(nullptr); // RHS
auto *SE = SequenceExpr::create(CurrDeclContext->getASTContext(), sequence);
prepareForRetypechecking(SE);

for (auto op : operators) {
switch (op->getKind()) {
case DeclKind::PrefixOperator:
Expand All @@ -3541,7 +3419,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
break;
case DeclKind::InfixOperator:
if (seenInfixOperators.insert(op->getName()).second)
tryInfixOperatorCompletion(cast<InfixOperatorDecl>(op), SE);
tryInfixOperatorCompletion(foldedExpr, cast<InfixOperatorDecl>(op));
break;
default:
llvm_unreachable("unexpected operator kind");
Expand Down
43 changes: 37 additions & 6 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,20 @@ namespace {
class LinkedExprCollector : public ASTWalker {

llvm::SmallVectorImpl<Expr*> &LinkedExprs;
ConstraintSystem &CS;

public:

LinkedExprCollector(llvm::SmallVectorImpl<Expr*> &linkedExprs) :
LinkedExprs(linkedExprs) {}
LinkedExprCollector(llvm::SmallVectorImpl<Expr *> &linkedExprs,
ConstraintSystem &cs)
: LinkedExprs(linkedExprs), CS(cs) {}

std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {


if (CS.shouldReusePrecheckedType() &&
!CS.getType(expr)->hasTypeVariable()) {
return { false, expr };
}

// Store top-level binary exprs for further analysis.
if (isa<BinaryExpr>(expr) ||

Expand Down Expand Up @@ -195,6 +201,11 @@ namespace {
LTI(lti), CS(cs) {}

std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {

if (CS.shouldReusePrecheckedType() &&
!CS.getType(expr)->hasTypeVariable()) {
return { false, expr };
}

if (isa<IntegerLiteralExpr>(expr)) {
LTI.haveIntLiteral = true;
Expand Down Expand Up @@ -934,6 +945,11 @@ namespace {
CS(cs) {}

std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {

if (CS.shouldReusePrecheckedType() &&
!CS.getType(expr)->hasTypeVariable()) {
return { false, expr };
}

if (auto applyExpr = dyn_cast<ApplyExpr>(expr)) {
if (isa<PrefixUnaryExpr>(applyExpr) ||
Expand Down Expand Up @@ -3240,6 +3256,12 @@ namespace {

std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
while (true) {

// If we should reuse pre-checked types, don't sanitize the expression
// if it's already type-checked.
if (CS.shouldReusePrecheckedType() && expr->getType())
return { false, expr };

// OpenExistentialExpr contains OpaqueValueExpr in its sub expression.
if (auto OOE = dyn_cast<OpenExistentialExpr>(expr)) {
auto archetypeVal = OOE->getOpaqueValue();
Expand Down Expand Up @@ -3409,6 +3431,15 @@ namespace {
ConstraintWalker(ConstraintGenerator &CG) : CG(CG) { }

std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {

if (CG.getConstraintSystem().shouldReusePrecheckedType()) {
if (expr->getType()) {
assert(!expr->getType()->hasTypeVariable());
CG.getConstraintSystem().cacheType(expr);
return { false, expr };
}
}

// Note that the subexpression of a #selector expression is
// unevaluated.
if (auto sel = dyn_cast<ObjCSelectorExpr>(expr)) {
Expand Down Expand Up @@ -3629,7 +3660,7 @@ void ConstraintSystem::optimizeConstraints(Expr *e) {
SmallVector<Expr *, 16> linkedExprs;

// Collect any linked expressions.
LinkedExprCollector collector(linkedExprs);
LinkedExprCollector collector(linkedExprs, *this);
e->walk(collector);

// Favor types, as appropriate.
Expand Down
Loading