Skip to content

Commit 4692e30

Browse files
committed
[ConstraintSystem] NFC: Move salvage from CSDiag to ConstraintSystem
1 parent 5ca7700 commit 4692e30

File tree

2 files changed

+79
-80
lines changed

2 files changed

+79
-80
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9069,86 +9069,6 @@ ValueDecl *ConstraintSystem::findResolvedMemberRef(ConstraintLocator *locator) {
90699069
return nullptr;
90709070
}
90719071

9072-
9073-
bool ConstraintSystem::salvage(SmallVectorImpl<Solution> &viable, Expr *expr) {
9074-
if (TC.getLangOpts().DebugConstraintSolver) {
9075-
auto &log = TC.Context.TypeCheckerDebug->getStream();
9076-
log << "---Attempting to salvage and emit diagnostics---\n";
9077-
}
9078-
9079-
// Attempt to solve again, capturing all states that come from our attempts to
9080-
// select overloads or bind type variables.
9081-
//
9082-
// FIXME: can this be removed? We need to arrange for recordFixes to be
9083-
// eliminated.
9084-
viable.clear();
9085-
9086-
{
9087-
// Set up solver state.
9088-
SolverState state(expr, *this);
9089-
state.recordFixes = true;
9090-
9091-
// Solve the system.
9092-
solveRec(viable, FreeTypeVariableBinding::Disallow);
9093-
9094-
// Check whether we have a best solution; this can happen if we found
9095-
// a series of fixes that worked.
9096-
if (auto best = findBestSolution(viable, state.ExprWeights,
9097-
/*minimize=*/true)) {
9098-
if (*best != 0)
9099-
viable[0] = std::move(viable[*best]);
9100-
viable.erase(viable.begin() + 1, viable.end());
9101-
return false;
9102-
}
9103-
9104-
// FIXME: If we were able to actually fix things along the way,
9105-
// we may have to hunt for the best solution. For now, we don't care.
9106-
9107-
// Before removing any "fixed" solutions, let's check
9108-
// if ambiguity is caused by fixes and diagnose if possible.
9109-
if (diagnoseAmbiguityWithFixes(expr, viable))
9110-
return true;
9111-
9112-
// Remove solutions that require fixes; the fixes in those systems should
9113-
// be diagnosed rather than any ambiguity.
9114-
auto hasFixes = [](const Solution &sol) { return !sol.Fixes.empty(); };
9115-
auto newEnd = std::remove_if(viable.begin(), viable.end(), hasFixes);
9116-
viable.erase(newEnd, viable.end());
9117-
9118-
// If there are multiple solutions, try to diagnose an ambiguity.
9119-
if (viable.size() > 1) {
9120-
if (getASTContext().LangOpts.DebugConstraintSolver) {
9121-
auto &log = getASTContext().TypeCheckerDebug->getStream();
9122-
log << "---Ambiguity error: "
9123-
<< viable.size() << " solutions found---\n";
9124-
int i = 0;
9125-
for (auto &solution : viable) {
9126-
log << "---Ambiguous solution #" << i++ << "---\n";
9127-
solution.dump(log);
9128-
log << "\n";
9129-
}
9130-
}
9131-
9132-
if (diagnoseAmbiguity(expr, viable)) {
9133-
return true;
9134-
}
9135-
}
9136-
9137-
// Fall through to produce diagnostics.
9138-
}
9139-
9140-
if (getExpressionTooComplex(viable)) {
9141-
TC.diagnose(expr->getLoc(), diag::expression_too_complex).
9142-
highlight(expr->getSourceRange());
9143-
return true;
9144-
}
9145-
9146-
// If all else fails, diagnose the failure by looking through the system's
9147-
// constraints.
9148-
diagnoseFailureForExpr(expr);
9149-
return true;
9150-
}
9151-
91529072
bool swift::diagnoseBaseUnwrapForMemberAccess(Expr *baseExpr, Type baseType,
91539073
DeclName memberName,
91549074
bool resultOptional,

lib/Sema/ConstraintSystem.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,3 +2082,82 @@ bool OverloadChoice::isImplicitlyUnwrappedValueOrReturnValue() const {
20822082
return true;
20832083
}
20842084
}
2085+
2086+
bool ConstraintSystem::salvage(SmallVectorImpl<Solution> &viable, Expr *expr) {
2087+
if (TC.getLangOpts().DebugConstraintSolver) {
2088+
auto &log = TC.Context.TypeCheckerDebug->getStream();
2089+
log << "---Attempting to salvage and emit diagnostics---\n";
2090+
}
2091+
2092+
// Attempt to solve again, capturing all states that come from our attempts to
2093+
// select overloads or bind type variables.
2094+
//
2095+
// FIXME: can this be removed? We need to arrange for recordFixes to be
2096+
// eliminated.
2097+
viable.clear();
2098+
2099+
{
2100+
// Set up solver state.
2101+
SolverState state(expr, *this);
2102+
state.recordFixes = true;
2103+
2104+
// Solve the system.
2105+
solveRec(viable, FreeTypeVariableBinding::Disallow);
2106+
2107+
// Check whether we have a best solution; this can happen if we found
2108+
// a series of fixes that worked.
2109+
if (auto best = findBestSolution(viable, state.ExprWeights,
2110+
/*minimize=*/true)) {
2111+
if (*best != 0)
2112+
viable[0] = std::move(viable[*best]);
2113+
viable.erase(viable.begin() + 1, viable.end());
2114+
return false;
2115+
}
2116+
2117+
// FIXME: If we were able to actually fix things along the way,
2118+
// we may have to hunt for the best solution. For now, we don't care.
2119+
2120+
// Before removing any "fixed" solutions, let's check
2121+
// if ambiguity is caused by fixes and diagnose if possible.
2122+
if (diagnoseAmbiguityWithFixes(expr, viable))
2123+
return true;
2124+
2125+
// Remove solutions that require fixes; the fixes in those systems should
2126+
// be diagnosed rather than any ambiguity.
2127+
auto hasFixes = [](const Solution &sol) { return !sol.Fixes.empty(); };
2128+
auto newEnd = std::remove_if(viable.begin(), viable.end(), hasFixes);
2129+
viable.erase(newEnd, viable.end());
2130+
2131+
// If there are multiple solutions, try to diagnose an ambiguity.
2132+
if (viable.size() > 1) {
2133+
if (getASTContext().LangOpts.DebugConstraintSolver) {
2134+
auto &log = getASTContext().TypeCheckerDebug->getStream();
2135+
log << "---Ambiguity error: " << viable.size()
2136+
<< " solutions found---\n";
2137+
int i = 0;
2138+
for (auto &solution : viable) {
2139+
log << "---Ambiguous solution #" << i++ << "---\n";
2140+
solution.dump(log);
2141+
log << "\n";
2142+
}
2143+
}
2144+
2145+
if (diagnoseAmbiguity(expr, viable)) {
2146+
return true;
2147+
}
2148+
}
2149+
2150+
// Fall through to produce diagnostics.
2151+
}
2152+
2153+
if (getExpressionTooComplex(viable)) {
2154+
TC.diagnose(expr->getLoc(), diag::expression_too_complex)
2155+
.highlight(expr->getSourceRange());
2156+
return true;
2157+
}
2158+
2159+
// If all else fails, diagnose the failure by looking through the system's
2160+
// constraints.
2161+
diagnoseFailureForExpr(expr);
2162+
return true;
2163+
}

0 commit comments

Comments
 (0)