From 0142d0b92e0598f9bf4a1596837eb945ebc45057 Mon Sep 17 00:00:00 2001 From: Hamish Knight Date: Mon, 1 Sep 2025 10:51:17 +0100 Subject: [PATCH] [CS] Add debug message when hitting too complex limit Add a message to the constraints debugging log when hitting one of the complexity limits. --- include/swift/Sema/ConstraintSystem.h | 28 +++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/include/swift/Sema/ConstraintSystem.h b/include/swift/Sema/ConstraintSystem.h index 84d1233eda1dd..407d9a5afd3f6 100644 --- a/include/swift/Sema/ConstraintSystem.h +++ b/include/swift/Sema/ConstraintSystem.h @@ -5671,13 +5671,22 @@ class ConstraintSystem { if (CancellationFlag && CancellationFlag->load(std::memory_order_relaxed)) return true; + auto markTooComplex = [&](SourceRange range, StringRef reason) { + if (isDebugMode()) { + if (solverState) + llvm::errs().indent(solverState->getCurrentIndent()); + llvm::errs() << "(too complex: " << reason << ")\n"; + } + isAlreadyTooComplex = {true, range}; + return true; + }; + auto used = getASTContext().getSolverMemory() + solutionMemory; MaxMemory = std::max(used, MaxMemory); auto threshold = getASTContext().TypeCheckerOpts.SolverMemoryThreshold; if (MaxMemory > threshold) { // No particular location for OoM problems. - isAlreadyTooComplex.first = true; - return true; + return markTooComplex(SourceRange(), "exceeded memory limit"); } if (Timer && Timer->isExpired()) { @@ -5686,23 +5695,18 @@ class ConstraintSystem { // emitting an error. Timer->disableWarning(); - isAlreadyTooComplex = {true, Timer->getAffectedRange()}; - return true; + return markTooComplex(Timer->getAffectedRange(), "exceeded time limit"); } auto &opts = getASTContext().TypeCheckerOpts; // Bail out once we've looked at a really large number of choices. - if (opts.SolverScopeThreshold && NumSolverScopes > opts.SolverScopeThreshold) { - isAlreadyTooComplex.first = true; - return true; - } + if (opts.SolverScopeThreshold && NumSolverScopes > opts.SolverScopeThreshold) + return markTooComplex(SourceRange(), "exceeded scope limit"); // Bail out once we've taken a really large number of steps. - if (opts.SolverTrailThreshold && NumTrailSteps > opts.SolverTrailThreshold) { - isAlreadyTooComplex.first = true; - return true; - } + if (opts.SolverTrailThreshold && NumTrailSteps > opts.SolverTrailThreshold) + return markTooComplex(SourceRange(), "exceeded trail limit"); return false; }