Skip to content

Commit 2e3aa67

Browse files
committed
[Constraint solver] Treat downgraded errors as "disfavored overloads".
This allows us to still maintain them in the score kind, but not treat them as being as severe as an error requiring a fix.
1 parent c564698 commit 2e3aa67

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

include/swift/Sema/CSFix.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class ConstraintSystem;
4646
class ConstraintLocator;
4747
class ConstraintLocatorBuilder;
4848
enum class ConversionRestrictionKind;
49+
enum ScoreKind: unsigned int;
4950
class Solution;
5051
struct MemberLookupResult;
5152

@@ -441,18 +442,9 @@ class ConstraintFix {
441442
}
442443
}
443444

444-
/// Whether this kind of fix affects the solution score.
445-
bool affectsSolutionScore() const {
446-
switch (fixBehavior) {
447-
case FixBehavior::AlwaysWarning:
448-
case FixBehavior::DowngradeToWarning:
449-
case FixBehavior::Suppress:
450-
return false;
451-
452-
case FixBehavior::Error:
453-
return true;
454-
}
455-
}
445+
/// Whether this kind of fix affects the solution score, and which score
446+
/// it affects.
447+
Optional<ScoreKind> affectsSolutionScore() const;
456448

457449
/// The diagnostic behavior limit that will be applied to any emitted
458450
/// diagnostics.

include/swift/Sema/ConstraintSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ class FunctionArgApplyInfo {
819819

820820
/// Describes an aspect of a solution that affects its overall score, i.e., a
821821
/// user-defined conversions.
822-
enum ScoreKind {
822+
enum ScoreKind: unsigned int {
823823
// These values are used as indices into a Score value.
824824

825825
/// A fix needs to be applied to the source.

lib/Sema/CSApply.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9132,16 +9132,23 @@ Optional<SolutionApplicationTarget> ConstraintSystem::applySolution(
91329132
Solution &solution, SolutionApplicationTarget target) {
91339133
// If any fixes needed to be applied to arrive at this solution, resolve
91349134
// them to specific expressions.
9135+
unsigned numResolvableFixes = 0;
91359136
if (!solution.Fixes.empty()) {
91369137
if (shouldSuppressDiagnostics())
91379138
return None;
91389139

91399140
bool diagnosedErrorsViaFixes = applySolutionFixes(solution);
9141+
bool canApplySolution = true;
9142+
for (const auto fix : solution.Fixes) {
9143+
if (!fix->canApplySolution())
9144+
canApplySolution = false;
9145+
if (fix->affectsSolutionScore() == SK_Fix && fix->canApplySolution())
9146+
++numResolvableFixes;
9147+
}
9148+
91409149
// If all of the available fixes would result in a warning,
91419150
// we can go ahead and apply this solution to AST.
9142-
if (!llvm::all_of(solution.Fixes, [](const ConstraintFix *fix) {
9143-
return fix->canApplySolution();
9144-
})) {
9151+
if (!canApplySolution) {
91459152
// If we already diagnosed any errors via fixes, that's it.
91469153
if (diagnosedErrorsViaFixes)
91479154
return None;
@@ -9158,7 +9165,7 @@ Optional<SolutionApplicationTarget> ConstraintSystem::applySolution(
91589165
// produce a fallback diagnostic to highlight the problem.
91599166
{
91609167
const auto &score = solution.getFixedScore();
9161-
if (score.Data[SK_Fix] > 0 || score.Data[SK_Hole] > 0) {
9168+
if (score.Data[SK_Fix] > numResolvableFixes || score.Data[SK_Hole] > 0) {
91629169
maybeProduceFallbackDiagnostic(target);
91639170
return None;
91649171
}

lib/Sema/CSFix.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ using namespace constraints;
3737

3838
ConstraintFix::~ConstraintFix() {}
3939

40+
Optional<ScoreKind> ConstraintFix::affectsSolutionScore() const {
41+
switch (fixBehavior) {
42+
case FixBehavior::AlwaysWarning:
43+
return None;
44+
45+
case FixBehavior::Error:
46+
return SK_Fix;
47+
48+
case FixBehavior::DowngradeToWarning:
49+
case FixBehavior::Suppress:
50+
return SK_DisfavoredOverload;
51+
}
52+
}
53+
4054
ASTNode ConstraintFix::getAnchor() const { return getLocator()->getAnchor(); }
4155

4256
void ConstraintFix::print(llvm::raw_ostream &Out) const {

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12701,8 +12701,8 @@ bool ConstraintSystem::recordFix(ConstraintFix *fix, unsigned impact) {
1270112701
// Record the fix.
1270212702

1270312703
// If this should affect the solution score, do so.
12704-
if (fix->affectsSolutionScore())
12705-
increaseScore(SK_Fix, impact);
12704+
if (auto scoreKind = fix->affectsSolutionScore())
12705+
increaseScore(*scoreKind, impact);
1270612706

1270712707
// If we've made the current solution worse than the best solution we've seen
1270812708
// already, stop now.

0 commit comments

Comments
 (0)