Skip to content

Commit 31ca8db

Browse files
committed
Move all shouldRecordFix logic back into CS itself keying off of FixKind.
1 parent 821f63f commit 31ca8db

File tree

3 files changed

+32
-38
lines changed

3 files changed

+32
-38
lines changed

lib/Sema/CSFix.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,6 @@ void ConstraintFix::print(llvm::raw_ostream &Out) const {
4444

4545
void ConstraintFix::dump() const {print(llvm::errs()); }
4646

47-
bool ConstraintFix::constraintSystemContainsFixInExpr(Expr *expr) const {
48-
llvm::SmallDenseSet<Expr *> fixExprs;
49-
for (auto fix : CS.Fixes)
50-
fixExprs.insert(fix->getAnchor());
51-
bool found = false;
52-
expr->forEachChildExpr([&](Expr *subExpr) -> Expr * {
53-
found |= fixExprs.count(subExpr) > 0;
54-
return subExpr;
55-
});
56-
return found;
57-
}
58-
5947
std::string ForceDowncast::getName() const {
6048
llvm::SmallString<16> name;
6149
name += "force downcast (as! ";
@@ -117,10 +105,6 @@ AddAddressOf *AddAddressOf::create(ConstraintSystem &cs,
117105
return new (cs.getAllocator()) AddAddressOf(cs, locator);
118106
}
119107

120-
bool TreatRValueAsLValue::shouldRecordFix() const {
121-
return !constraintSystemContainsFixInExpr(getAnchor());
122-
}
123-
124108
bool TreatRValueAsLValue::diagnose(Expr *root, bool asNote) const {
125109
RValueTreatedAsLValueFailure failure(getConstraintSystem(), getLocator());
126110
return failure.diagnose(asNote);

lib/Sema/CSFix.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@ class ConstraintFix {
9292

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

95-
/// Determine from the current CS (primarily the already existing fixes)
96-
/// whether this fix should be recorded for later diagnosis.
97-
virtual bool shouldRecordFix() const {
98-
return true;
99-
}
100-
10195
/// Diagnose a failure associated with this fix given
10296
/// root expression and information from constraint system.
10397
virtual bool diagnose(Expr *root, bool asNote = false) const = 0;
@@ -116,8 +110,6 @@ class ConstraintFix {
116110

117111
protected:
118112
ConstraintSystem &getConstraintSystem() const { return CS; }
119-
120-
bool constraintSystemContainsFixInExpr(Expr *expr) const;
121113
};
122114

123115
/// Append 'as! T' to force a downcast to the specified type.
@@ -197,7 +189,6 @@ class TreatRValueAsLValue final : public ConstraintFix {
197189
public:
198190
std::string getName() const override { return "treat rvalue as lvalue"; }
199191

200-
bool shouldRecordFix() const override;
201192
bool diagnose(Expr *root, bool asNote = false) const override;
202193

203194
static TreatRValueAsLValue *create(ConstraintSystem &cs,

lib/Sema/CSSimplify.cpp

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4987,6 +4987,15 @@ ConstraintSystem::simplifyRestrictedConstraint(
49874987
llvm_unreachable("Unhandled SolutionKind in switch.");
49884988
}
49894989

4990+
static bool isAugmentingFix(ConstraintFix *fix) {
4991+
switch (fix->getKind()) {
4992+
case swift::constraints::FixKind::TreatRValueAsLValue:
4993+
return false;
4994+
default:
4995+
return true;
4996+
}
4997+
}
4998+
49904999
bool ConstraintSystem::recordFix(ConstraintFix *fix) {
49915000
auto &ctx = getASTContext();
49925001
if (ctx.LangOpts.DebugConstraintSolver) {
@@ -5005,20 +5014,30 @@ bool ConstraintSystem::recordFix(ConstraintFix *fix) {
50055014
if (worseThanBestSolution())
50065015
return true;
50075016

5008-
if (!fix->shouldRecordFix())
5009-
return false;
5010-
5011-
auto *loc = fix->getLocator();
5012-
auto existingFix = llvm::find_if(Fixes, [&](const ConstraintFix *e) {
5013-
// If we already have a fix like this recorded, let's not do it again,
5014-
// this situation might happen when the same fix kind is applicable to
5017+
if (isAugmentingFix(fix)) {
5018+
// Always useful, unless duplicate of exactly the same fix and location.
5019+
// This situation might happen when the same fix kind is applicable to
50155020
// different overload choices.
5016-
return e->getKind() == fix->getKind() && e->getLocator() == loc;
5017-
});
5018-
5019-
if (existingFix == Fixes.end())
5020-
Fixes.push_back(fix);
5021-
5021+
auto *loc = fix->getLocator();
5022+
auto existingFix = llvm::find_if(Fixes, [&](const ConstraintFix *e) {
5023+
// If we already have a fix like this recorded, let's not do it again,
5024+
return e->getKind() == fix->getKind() && e->getLocator() == loc;
5025+
});
5026+
if (existingFix == Fixes.end())
5027+
Fixes.push_back(fix);
5028+
} else {
5029+
// Only useful to record if no pre-existing fix in the subexpr tree.
5030+
llvm::SmallDenseSet<Expr *> fixExprs;
5031+
for (auto fix : Fixes)
5032+
fixExprs.insert(fix->getAnchor());
5033+
bool found = false;
5034+
fix->getAnchor()->forEachChildExpr([&](Expr *subExpr) -> Expr * {
5035+
found |= fixExprs.count(subExpr) > 0;
5036+
return subExpr;
5037+
});
5038+
if (!found)
5039+
Fixes.push_back(fix);
5040+
}
50225041
return false;
50235042
}
50245043

0 commit comments

Comments
 (0)