Skip to content

[CSFix] Introduce non-fatal "warning" fixes #21987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7808,23 +7808,31 @@ bool ConstraintSystem::applySolutionFixes(Expr *E, const Solution &solution) {
if (fixes == fixesPerExpr.end())
return false;

bool diagnosed = false;
for (const auto *fix : fixes->second)
diagnosed |= fix->diagnose(E);
return diagnosed;
bool diagnosedError = false;
for (const auto *fix : fixes->second) {
auto diagnosed = fix->diagnose(E);

if (fix->isWarning()) {
assert(diagnosed && "warnings should always be diagnosed");
(void)diagnosed;
} else {
diagnosedError |= diagnosed;
}
}
return diagnosedError;
};

bool diagnosed = false;
bool diagnosedError = false;
E->forEachChildExpr([&](Expr *subExpr) -> Expr * {
// Diagnose root expression at the end to
// preserve ordering.
if (subExpr != E)
diagnosed |= diagnoseExprFailures(subExpr);
diagnosedError |= diagnoseExprFailures(subExpr);
return subExpr;
});

diagnosed |= diagnoseExprFailures(E);
return diagnosed;
diagnosedError |= diagnoseExprFailures(E);
return diagnosedError;
}

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

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

// If we didn't manage to diagnose anything well, so fall back to
// diagnosing mining the system to construct a reasonable error message.
diagnoseFailureForExpr(expr);
return nullptr;
// If we didn't manage to diagnose anything well, so fall back to
// diagnosing mining the system to construct a reasonable error message.
diagnoseFailureForExpr(expr);
return nullptr;
}
}

// Mark any normal conformances used in this solution as "used".
Expand Down
11 changes: 9 additions & 2 deletions lib/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,21 @@ class ConstraintFix {
FixKind Kind;
ConstraintLocator *Locator;

/// Determines whether this fix is simplify a warning which doesn't
/// require immediate source changes.
bool IsWarning;

public:
ConstraintFix(ConstraintSystem &cs, FixKind kind, ConstraintLocator *locator)
: CS(cs), Kind(kind), Locator(locator) {}
ConstraintFix(ConstraintSystem &cs, FixKind kind, ConstraintLocator *locator,
bool warning = false)
: CS(cs), Kind(kind), Locator(locator), IsWarning(warning) {}

virtual ~ConstraintFix();

FixKind getKind() const { return Kind; }

bool isWarning() const { return IsWarning; }

virtual std::string getName() const = 0;

/// Diagnose a failure associated with this fix given
Expand Down