Skip to content

Commit b034c48

Browse files
committed
[CodeCompletion] Record fixes while solving result builders for code completion
We record fixes while solving normal expressions for code completion and we should do the same when solving result builders if we are reporting the solutions to completion callbacks.
1 parent f9d27b9 commit b034c48

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5296,18 +5296,21 @@ class ConstraintSystem {
52965296
= FreeTypeVariableBinding::Disallow,
52975297
bool allowFixes = false);
52985298

5299-
/// Construct and solve a system of constraints based on the given expression
5300-
/// and its contextual information.
5299+
/// Assuming that constraints have already been generated, solve the
5300+
/// constraint system for code completion, writing all solutions to
5301+
/// \p solutions.
53015302
///
53025303
/// This method is designed to be used for code completion which means that
53035304
/// it doesn't mutate given expression, even if there is a single valid
53045305
/// solution, and constraint solver is allowed to produce partially correct
53055306
/// solutions. Such solutions can have any number of holes in them.
53065307
///
5307-
/// \param target The expression involved in code completion.
5308-
///
53095308
/// \param solutions The solutions produced for the given target without
53105309
/// filtering.
5310+
void solveForCodeCompletion(SmallVectorImpl<Solution> &solutions);
5311+
5312+
/// Generate constraints for \p target and solve the resulting constraint
5313+
/// system for code completion (see overload above).
53115314
///
53125315
/// \returns `false` if this call fails (e.g. pre-check or constraint
53135316
/// generation fails), `true` otherwise.

lib/Sema/BuilderTransform.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,10 +1714,10 @@ Optional<BraceStmt *> TypeChecker::applyResultBuilderBodyTransform(
17141714
}
17151715

17161716
// Solve the constraint system.
1717-
SmallVector<Solution, 4> solutions;
1718-
bool solvingFailed = cs.solve(solutions);
1719-
17201717
if (cs.getASTContext().CompletionCallback) {
1718+
SmallVector<Solution, 4> solutions;
1719+
cs.solveForCodeCompletion(solutions);
1720+
17211721
CompletionContextFinder analyzer(func, func->getDeclContext());
17221722
filterSolutionsForCodeCompletion(solutions, analyzer);
17231723
for (const auto &solution : solutions) {
@@ -1726,6 +1726,9 @@ Optional<BraceStmt *> TypeChecker::applyResultBuilderBodyTransform(
17261726
return nullptr;
17271727
}
17281728

1729+
SmallVector<Solution, 4> solutions;
1730+
bool solvingFailed = cs.solve(solutions);
1731+
17291732
if (solvingFailed || solutions.size() != 1) {
17301733
// Try to fix the system or provide a decent diagnostic.
17311734
auto salvagedResult = cs.salvage();

lib/Sema/CSSolver.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,26 +1516,8 @@ void ConstraintSystem::solveImpl(SmallVectorImpl<Solution> &solutions) {
15161516
}
15171517
}
15181518

1519-
bool ConstraintSystem::solveForCodeCompletion(
1520-
SolutionApplicationTarget &target, SmallVectorImpl<Solution> &solutions) {
1521-
auto *expr = target.getAsExpr();
1522-
// Tell the constraint system what the contextual type is.
1523-
setContextualType(expr, target.getExprContextualTypeLoc(),
1524-
target.getExprContextualTypePurpose());
1525-
1526-
// Set up the expression type checker timer.
1527-
Timer.emplace(expr, *this);
1528-
1529-
shrink(expr);
1530-
1531-
if (isDebugMode()) {
1532-
auto &log = llvm::errs();
1533-
log << "--- Code Completion ---\n";
1534-
}
1535-
1536-
if (generateConstraints(target, FreeTypeVariableBinding::Disallow))
1537-
return false;
1538-
1519+
void ConstraintSystem::solveForCodeCompletion(
1520+
SmallVectorImpl<Solution> &solutions) {
15391521
{
15401522
SolverState state(*this, FreeTypeVariableBinding::Disallow);
15411523

@@ -1556,6 +1538,30 @@ bool ConstraintSystem::solveForCodeCompletion(
15561538
}
15571539
}
15581540

1541+
return;
1542+
}
1543+
1544+
bool ConstraintSystem::solveForCodeCompletion(
1545+
SolutionApplicationTarget &target, SmallVectorImpl<Solution> &solutions) {
1546+
auto *expr = target.getAsExpr();
1547+
// Tell the constraint system what the contextual type is.
1548+
setContextualType(expr, target.getExprContextualTypeLoc(),
1549+
target.getExprContextualTypePurpose());
1550+
1551+
// Set up the expression type checker timer.
1552+
Timer.emplace(expr, *this);
1553+
1554+
shrink(expr);
1555+
1556+
if (isDebugMode()) {
1557+
auto &log = llvm::errs();
1558+
log << "--- Code Completion ---\n";
1559+
}
1560+
1561+
if (generateConstraints(target, FreeTypeVariableBinding::Disallow))
1562+
return false;
1563+
1564+
solveForCodeCompletion(solutions);
15591565
return true;
15601566
}
15611567

0 commit comments

Comments
 (0)