Skip to content

Commit df692ed

Browse files
committed
Sema: Record fixes in the trail
1 parent 62a470e commit df692ed

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class SolverTrail {
5555
UpdatedTypeVariable,
5656
/// Recorded a conversion restriction kind.
5757
AddedConversionRestriction,
58+
/// Recorded a fix.
59+
AddedFix,
5860
};
5961

6062
/// A change made to the constraint system.
@@ -111,6 +113,8 @@ class SolverTrail {
111113
/// The destination type.
112114
Type DstType;
113115
} Restriction;
116+
117+
ConstraintFix *Fix;
114118
};
115119

116120
Change() : Kind(ChangeKind::AddedTypeVariable), TypeVar(nullptr) { }
@@ -150,6 +154,9 @@ class SolverTrail {
150154
/// Create a change that recorded a restriction.
151155
static Change addedConversionRestriction(Type srcType, Type dstType);
152156

157+
/// Create a change that recorded a fix.
158+
static Change addedFix(ConstraintFix *fix);
159+
153160
/// Undo this change, reverting the constraint graph to the state it
154161
/// had prior to this change.
155162
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,6 +2859,8 @@ class ConstraintSystem {
28592859
unsigned numTrailChanges;
28602860

28612861
/// The length of \c Fixes.
2862+
///
2863+
/// FIXME: Remove this.
28622864
unsigned numFixes;
28632865

28642866
/// The length of \c FixedRequirements.
@@ -4222,6 +4224,12 @@ class ConstraintSystem {
42224224
/// Called to undo the above change.
42234225
void removeConversionRestriction(Type srcType, Type dstType);
42244226

4227+
/// Update Fixes and record a change in the trail.
4228+
void addFix(ConstraintFix *fix);
4229+
4230+
/// Called to undo the above change.
4231+
void removeFix(ConstraintFix *fix);
4232+
42254233
/// Determine whether the given type is a dictionary and, if so, provide the
42264234
/// key and value types for the dictionary.
42274235
static std::optional<std::pair<Type, Type>> isDictionaryType(Type type);

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14855,7 +14855,7 @@ bool ConstraintSystem::recordFix(ConstraintFix *fix, unsigned impact) {
1485514855
return true;
1485614856

1485714857
if (isAugmentingFix(fix)) {
14858-
Fixes.insert(fix);
14858+
addFix(fix);
1485914859
return false;
1486014860
}
1486114861

@@ -14887,7 +14887,7 @@ bool ConstraintSystem::recordFix(ConstraintFix *fix, unsigned impact) {
1488714887
}
1488814888

1488914889
if (!found)
14890-
Fixes.insert(fix);
14890+
addFix(fix);
1489114891

1489214892
return false;
1489314893
}

lib/Sema/CSSolver.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ void ConstraintSystem::applySolution(const Solution &solution) {
419419
}
420420

421421
// Register any fixes produced along this path.
422-
Fixes.insert(solution.Fixes.begin(), solution.Fixes.end());
422+
for (auto *fix : solution.Fixes)
423+
addFix(fix);
423424
}
424425
bool ConstraintSystem::simplify() {
425426
// While we have a constraint in the worklist, process it.
@@ -719,9 +720,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
719720
// constraints introduced by the current scope.
720721
cs.solverState->rollback(this);
721722

722-
// Remove any fixes.
723-
truncate(cs.Fixes, numFixes);
724-
725723
// Remove any disjunction choices.
726724
truncate(cs.DisjunctionChoices, numDisjunctionChoices);
727725

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ SolverTrail::Change::addedConversionRestriction(Type srcType, Type dstType) {
131131
return result;
132132
}
133133

134+
SolverTrail::Change
135+
SolverTrail::Change::addedFix(ConstraintFix *fix) {
136+
Change result;
137+
result.Kind = ChangeKind::AddedFix;
138+
result.Fix = fix;
139+
return result;
140+
}
141+
134142
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
135143
auto &cg = cs.getConstraintGraph();
136144

@@ -174,6 +182,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
174182
cs.removeConversionRestriction(Restriction.SrcType,
175183
Restriction.DstType);
176184
break;
185+
186+
case ChangeKind::AddedFix:
187+
cs.removeFix(Fix);
188+
break;
177189
}
178190
}
179191

@@ -269,6 +281,12 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
269281
Restriction.DstType->print(out, PO);
270282
out << ")\n";
271283
break;
284+
285+
case ChangeKind::AddedFix:
286+
out << "(added a fix ";
287+
Fix->print(out);
288+
out << ")\n";
289+
break;
272290
}
273291
}
274292

lib/Sema/ConstraintSystem.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,20 @@ void ConstraintSystem::removeConversionRestriction(
279279
ASSERT(erased);
280280
}
281281

282+
void ConstraintSystem::addFix(ConstraintFix *fix) {
283+
bool inserted = Fixes.insert(fix);
284+
if (!inserted)
285+
return;
286+
287+
if (isRecordingChanges())
288+
recordChange(SolverTrail::Change::addedFix(fix));
289+
}
290+
291+
void ConstraintSystem::removeFix(ConstraintFix *fix) {
292+
ASSERT(Fixes.back() == fix);
293+
Fixes.pop_back();
294+
}
295+
282296
/// Retrieve a dynamic result signature for the given declaration.
283297
static std::tuple<char, ObjCSelector, CanType>
284298
getDynamicResultSignature(ValueDecl *decl) {

0 commit comments

Comments
 (0)