Skip to content

Commit 4b62682

Browse files
committed
[Sema] Add a solution callback that is called when the constraint system produces a solution
When doing solver-based cursor info, we’ll type check the expression in question using a normal `typeCheckASTNodeAtLoc` call (not in code completion mode) and listen for any solutions that were produced during the type check.
1 parent 9481e86 commit 4b62682

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

include/swift/AST/ASTContext.h

+5
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ class ASTContext final {
289289

290290
ide::TypeCheckCompletionCallback *CompletionCallback = nullptr;
291291

292+
/// A callback that will be called when the constraint system found a
293+
/// solution. Called multiple times if the constraint system has ambiguous
294+
/// solutions.
295+
ide::TypeCheckCompletionCallback *SolutionCallback = nullptr;
296+
292297
/// The request-evaluator that is used to process various requests.
293298
Evaluator evaluator;
294299

lib/Sema/CSSolver.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,24 @@ Optional<std::vector<Solution>> ConstraintSystem::solve(
13531353
}
13541354
};
13551355

1356+
auto reportSolutionsToSolutionCallback = [&](const SolutionResult &result) {
1357+
if (!getASTContext().SolutionCallback) {
1358+
return;
1359+
}
1360+
switch (result.getKind()) {
1361+
case SolutionResult::Success:
1362+
getASTContext().SolutionCallback->sawSolution(result.getSolution());
1363+
break;
1364+
case SolutionResult::Ambiguous:
1365+
for (auto &solution : result.getAmbiguousSolutions()) {
1366+
getASTContext().SolutionCallback->sawSolution(solution);
1367+
}
1368+
break;
1369+
default:
1370+
break;
1371+
}
1372+
};
1373+
13561374
// Take up to two attempts at solving the system. The first attempts to
13571375
// solve a system that is expected to be well-formed, the second kicks in
13581376
// when there is an error and attempts to salvage an ill-formed program.
@@ -1365,6 +1383,7 @@ Optional<std::vector<Solution>> ConstraintSystem::solve(
13651383
case SolutionResult::Success: {
13661384
// Return the successful solution.
13671385
dumpSolutions(solution);
1386+
reportSolutionsToSolutionCallback(solution);
13681387
std::vector<Solution> result;
13691388
result.push_back(std::move(solution).takeSolution());
13701389
return std::move(result);
@@ -1394,6 +1413,7 @@ Optional<std::vector<Solution>> ConstraintSystem::solve(
13941413
// If salvaging produced an ambiguous result, it has already been
13951414
// diagnosed.
13961415
if (stage == 1) {
1416+
reportSolutionsToSolutionCallback(solution);
13971417
solution.markAsDiagnosed();
13981418
return None;
13991419
}
@@ -1412,12 +1432,14 @@ Optional<std::vector<Solution>> ConstraintSystem::solve(
14121432

14131433
case SolutionResult::UndiagnosedError:
14141434
if (shouldSuppressDiagnostics()) {
1435+
reportSolutionsToSolutionCallback(solution);
14151436
solution.markAsDiagnosed();
14161437
return None;
14171438
}
14181439

14191440
if (stage == 1) {
14201441
diagnoseFailureFor(target);
1442+
reportSolutionsToSolutionCallback(solution);
14211443
solution.markAsDiagnosed();
14221444
return None;
14231445
}

0 commit comments

Comments
 (0)