Skip to content

Commit 3dc16a9

Browse files
committed
Sema: Record defaulted constraints in the trail
1 parent fcd6bc0 commit 3dc16a9

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class SolverTrail {
7676
/// Recorded the mapping from a pack element expression to its parent
7777
/// pack expansion expression.
7878
RecordedPackEnvironment,
79+
/// Record a defaulted constraint at a locator.
80+
RecordedDefaultedConstraint,
7981
};
8082

8183
/// A change made to the constraint system.
@@ -216,6 +218,9 @@ class SolverTrail {
216218
/// to its parent expansion expression.
217219
static Change recordedPackEnvironment(PackElementExpr *packElement);
218220

221+
/// Create a change that recorded a defaulted constraint at a locator.
222+
static Change recordedDefaultedConstraint(ConstraintLocator *locator);
223+
219224
/// Undo this change, reverting the constraint graph to the state it
220225
/// had prior to this change.
221226
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,10 +2412,25 @@ class ConstraintSystem {
24122412

24132413
public:
24142414
/// A map from argument expressions to their applied property wrapper expressions.
2415-
llvm::SmallMapVector<ASTNode, SmallVector<AppliedPropertyWrapper, 2>, 4> appliedPropertyWrappers;
2415+
llvm::SmallMapVector<ASTNode, SmallVector<AppliedPropertyWrapper, 2>, 4>
2416+
appliedPropertyWrappers;
24162417

24172418
/// The locators of \c Defaultable constraints whose defaults were used.
2418-
llvm::SetVector<ConstraintLocator *> DefaultedConstraints;
2419+
llvm::DenseSet<ConstraintLocator *> DefaultedConstraints;
2420+
2421+
void recordDefaultedConstraint(ConstraintLocator *locator) {
2422+
bool inserted = DefaultedConstraints.insert(locator).second;
2423+
if (inserted) {
2424+
if (isRecordingChanges()) {
2425+
recordChange(SolverTrail::Change::recordedDefaultedConstraint(locator));
2426+
}
2427+
}
2428+
}
2429+
2430+
void removeDefaultedConstraint(ConstraintLocator *locator) {
2431+
bool erased = DefaultedConstraints.erase(locator);
2432+
ASSERT(erased);
2433+
}
24192434

24202435
/// A cache that stores the @dynamicCallable required methods implemented by
24212436
/// types.
@@ -2879,9 +2894,6 @@ class ConstraintSystem {
28792894
/// FIXME: Remove this.
28802895
unsigned numFixes;
28812896

2882-
/// The length of \c DefaultedConstraints.
2883-
unsigned numDefaultedConstraints;
2884-
28852897
unsigned numAddedNodeTypes;
28862898

28872899
unsigned numAddedKeyPathComponentTypes;

lib/Sema/CSBindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2747,7 +2747,7 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
27472747

27482748
// If this was from a defaultable binding note that.
27492749
if (Binding.isDefaultableBinding()) {
2750-
cs.DefaultedConstraints.insert(srcLocator);
2750+
cs.recordDefaultedConstraint(srcLocator);
27512751

27522752
// Fail if hole reporting fails.
27532753
if (type->isPlaceholder() && reportHole())

lib/Sema/CSSolver.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ void ConstraintSystem::applySolution(const Solution &solution) {
347347
}
348348

349349
// Register the defaulted type variables.
350-
DefaultedConstraints.insert(solution.DefaultedConstraints.begin(),
351-
solution.DefaultedConstraints.end());
350+
for (auto *locator : solution.DefaultedConstraints)
351+
recordDefaultedConstraint(locator);
352352

353353
// Add the node types back.
354354
for (auto &nodeType : solution.nodeTypes) {
@@ -663,7 +663,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
663663

664664
numTypeVariables = cs.TypeVariables.size();
665665
numFixes = cs.Fixes.size();
666-
numDefaultedConstraints = cs.DefaultedConstraints.size();
667666
numAddedNodeTypes = cs.addedNodeTypes.size();
668667
numAddedKeyPathComponentTypes = cs.addedKeyPathComponentTypes.size();
669668
numKeyPaths = cs.KeyPaths.size();
@@ -719,9 +718,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
719718
// constraints introduced by the current scope.
720719
cs.solverState->rollback(this);
721720

722-
// Remove any defaulted type variables.
723-
truncate(cs.DefaultedConstraints, numDefaultedConstraints);
724-
725721
// Remove any node types we registered.
726722
for (unsigned i :
727723
reverse(range(numAddedNodeTypes, cs.addedNodeTypes.size()))) {

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ SolverTrail::Change::recordedPackEnvironment(PackElementExpr *packElement) {
217217
return result;
218218
}
219219

220+
SolverTrail::Change
221+
SolverTrail::Change::recordedDefaultedConstraint(ConstraintLocator *locator) {
222+
Change result;
223+
result.Kind = ChangeKind::RecordedDefaultedConstraint;
224+
result.Locator = locator;
225+
return result;
226+
}
227+
220228
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
221229
auto &cg = cs.getConstraintGraph();
222230

@@ -301,6 +309,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
301309
case ChangeKind::RecordedPackEnvironment:
302310
cs.removePackEnvironment(ElementExpr);
303311
break;
312+
313+
case ChangeKind::RecordedDefaultedConstraint:
314+
cs.removeDefaultedConstraint(Locator);
315+
break;
304316
}
305317
}
306318

@@ -458,6 +470,12 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
458470
case ChangeKind::RecordedPackEnvironment:
459471
out << "(recorded pack environment)\n";
460472
break;
473+
474+
case ChangeKind::RecordedDefaultedConstraint:
475+
out << "(recorded defaulted constraint at ";
476+
Locator->dump(&cs.getASTContext().SourceMgr, out);
477+
out << ")\n";
478+
break;
461479
}
462480
}
463481

0 commit comments

Comments
 (0)