Skip to content

Commit aa8ccfd

Browse files
committed
[CSFix] Introduce non-fatal "warning" fixes
The intention here is to be able to detect warnings earlier and move some of the logic from CSApply and MiscDiagnostics to solver. Warning fixes still lead to solution being applied to AST.
1 parent 3dda7ee commit aa8ccfd

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

lib/Sema/CSApply.cpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7808,23 +7808,31 @@ bool ConstraintSystem::applySolutionFixes(Expr *E, const Solution &solution) {
78087808
if (fixes == fixesPerExpr.end())
78097809
return false;
78107810

7811-
bool diagnosed = false;
7812-
for (const auto *fix : fixes->second)
7813-
diagnosed |= fix->diagnose(E);
7814-
return diagnosed;
7811+
bool diagnosedError = false;
7812+
for (const auto *fix : fixes->second) {
7813+
auto diagnosed = fix->diagnose(E);
7814+
7815+
if (fix->isWarning()) {
7816+
assert(diagnosed && "warnings should always be diagnosed");
7817+
(void)diagnosed;
7818+
} else {
7819+
diagnosedError |= diagnosed;
7820+
}
7821+
}
7822+
return diagnosedError;
78157823
};
78167824

7817-
bool diagnosed = false;
7825+
bool diagnosedError = false;
78187826
E->forEachChildExpr([&](Expr *subExpr) -> Expr * {
78197827
// Diagnose root expression at the end to
78207828
// preserve ordering.
78217829
if (subExpr != E)
7822-
diagnosed |= diagnoseExprFailures(subExpr);
7830+
diagnosedError |= diagnoseExprFailures(subExpr);
78237831
return subExpr;
78247832
});
78257833

7826-
diagnosed |= diagnoseExprFailures(E);
7827-
return diagnosed;
7834+
diagnosedError |= diagnoseExprFailures(E);
7835+
return diagnosedError;
78287836
}
78297837

78307838
/// Apply a given solution to the expression, producing a fully
@@ -7839,15 +7847,21 @@ Expr *ConstraintSystem::applySolution(Solution &solution, Expr *expr,
78397847
if (shouldSuppressDiagnostics())
78407848
return nullptr;
78417849

7842-
// If we can diagnose the problem with the fixits that we've pre-assumed,
7843-
// do so now.
7844-
if (applySolutionFixes(expr, solution))
7845-
return nullptr;
7850+
bool diagnosedErrorsViaFixes = applySolutionFixes(expr, solution);
7851+
// If all of the available fixes would result in a warning,
7852+
// we can go ahead and apply this solution to AST.
7853+
if (!llvm::all_of(solution.Fixes, [](const ConstraintFix *fix) {
7854+
return fix->isWarning();
7855+
})) {
7856+
// If we already diagnosed any errors via fixes, that's it.
7857+
if (diagnosedErrorsViaFixes)
7858+
return nullptr;
78467859

7847-
// If we didn't manage to diagnose anything well, so fall back to
7848-
// diagnosing mining the system to construct a reasonable error message.
7849-
diagnoseFailureForExpr(expr);
7850-
return nullptr;
7860+
// If we didn't manage to diagnose anything well, so fall back to
7861+
// diagnosing mining the system to construct a reasonable error message.
7862+
diagnoseFailureForExpr(expr);
7863+
return nullptr;
7864+
}
78517865
}
78527866

78537867
// Mark any normal conformances used in this solution as "used".

lib/Sema/CSFix.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,21 @@ class ConstraintFix {
111111
FixKind Kind;
112112
ConstraintLocator *Locator;
113113

114+
/// Determines whether this fix is simplify a warning which doesn't
115+
/// require immediate source changes.
116+
bool IsWarning;
117+
114118
public:
115-
ConstraintFix(ConstraintSystem &cs, FixKind kind, ConstraintLocator *locator)
116-
: CS(cs), Kind(kind), Locator(locator) {}
119+
ConstraintFix(ConstraintSystem &cs, FixKind kind, ConstraintLocator *locator,
120+
bool warning = false)
121+
: CS(cs), Kind(kind), Locator(locator), IsWarning(warning) {}
117122

118123
virtual ~ConstraintFix();
119124

120125
FixKind getKind() const { return Kind; }
121126

127+
bool isWarning() const { return IsWarning; }
128+
122129
virtual std::string getName() const = 0;
123130

124131
/// Diagnose a failure associated with this fix given

0 commit comments

Comments
 (0)