Skip to content

Commit f963f36

Browse files
committed
Sema: Record opened types in the trail
1 parent a9f3922 commit f963f36

File tree

5 files changed

+48
-19
lines changed

5 files changed

+48
-19
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class SolverTrail {
6565
RecordedAppliedDisjunction,
6666
/// Recorded an argument matching choice.
6767
RecordedMatchCallArgumentResult,
68+
/// Recorded a list of opened types at a locator.
69+
RecordedOpenedTypes,
6870
};
6971

7072
/// A change made to the constraint system.
@@ -187,6 +189,9 @@ class SolverTrail {
187189
/// Create a change that recorded an applied disjunction.
188190
static Change recordedMatchCallArgumentResult(ConstraintLocator *locator);
189191

192+
/// Create a change that recorded a list of opened types.
193+
static Change recordedOpenedTypes(ConstraintLocator *locator);
194+
190195
/// Undo this change, reverting the constraint graph to the state it
191196
/// had prior to this change.
192197
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@ class ConstraintSystem {
23622362

23632363
/// A mapping from constraint locators to the set of opened types associated
23642364
/// with that locator.
2365-
llvm::SmallMapVector<ConstraintLocator *, ArrayRef<OpenedType>, 4>
2365+
llvm::SmallDenseMap<ConstraintLocator *, ArrayRef<OpenedType>, 4>
23662366
OpenedTypes;
23672367

23682368
/// A dictionary of all conformances that have been looked up by the solver.
@@ -2883,9 +2883,6 @@ class ConstraintSystem {
28832883
/// FIXME: Remove this.
28842884
unsigned numFixes;
28852885

2886-
/// The length of \c OpenedTypes.
2887-
unsigned numOpenedTypes;
2888-
28892886
/// The length of \c OpenedExistentialTypes.
28902887
unsigned numOpenedExistentialTypes;
28912888

@@ -4393,6 +4390,13 @@ class ConstraintSystem {
43934390
ConstraintLocatorBuilder locator,
43944391
llvm::function_ref<Type(Type)> subst);
43954392

4393+
/// Update OpenedTypes and record a change in the trail.
4394+
void recordOpenedType(
4395+
ConstraintLocator *locator, ArrayRef<OpenedType> openedTypes);
4396+
4397+
/// Undo the above change.
4398+
void removeOpenedType(ConstraintLocator *locator);
4399+
43964400
/// Record the set of opened types for the given locator.
43974401
void recordOpenedTypes(
43984402
ConstraintLocatorBuilder locator,

lib/Sema/CSSolver.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ void ConstraintSystem::applySolution(const Solution &solution) {
326326

327327
// Register the solution's opened types.
328328
for (const auto &opened : solution.OpenedTypes) {
329-
OpenedTypes.insert(opened);
329+
recordOpenedType(opened.first, opened.second);
330330
}
331331

332332
// Register the solution's opened existential types.
@@ -672,7 +672,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
672672

673673
numTypeVariables = cs.TypeVariables.size();
674674
numFixes = cs.Fixes.size();
675-
numOpenedTypes = cs.OpenedTypes.size();
676675
numOpenedExistentialTypes = cs.OpenedExistentialTypes.size();
677676
numOpenedPackExpansionTypes = cs.OpenedPackExpansionTypes.size();
678677
numPackExpansionEnvironments = cs.PackExpansionEnvironments.size();
@@ -734,9 +733,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
734733
// constraints introduced by the current scope.
735734
cs.solverState->rollback(this);
736735

737-
// Remove any opened types.
738-
truncate(cs.OpenedTypes, numOpenedTypes);
739-
740736
// Remove any opened existential types.
741737
truncate(cs.OpenedExistentialTypes, numOpenedExistentialTypes);
742738

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ SolverTrail::Change::recordedMatchCallArgumentResult(ConstraintLocator *locator)
177177
return result;
178178
}
179179

180+
SolverTrail::Change
181+
SolverTrail::Change::recordedOpenedTypes(ConstraintLocator *locator) {
182+
Change result;
183+
result.Kind = ChangeKind::RecordedOpenedTypes;
184+
result.Locator = locator;
185+
return result;
186+
}
187+
180188
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
181189
auto &cg = cs.getConstraintGraph();
182190

@@ -241,6 +249,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
241249
case ChangeKind::RecordedMatchCallArgumentResult:
242250
cs.removeMatchCallArgumentResult(Locator);
243251
break;
252+
253+
case ChangeKind::RecordedOpenedTypes:
254+
cs.removeOpenedType(Locator);
255+
break;
244256
}
245257
}
246258

@@ -370,6 +382,12 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
370382
Locator->dump(&cs.getASTContext().SourceMgr, out);
371383
out << ")\n";
372384
break;
385+
386+
case ChangeKind::RecordedOpenedTypes:
387+
out << "(recorded list of opened types at ";
388+
Locator->dump(&cs.getASTContext().SourceMgr, out);
389+
out << ")\n";
390+
break;
373391
}
374392
}
375393

lib/Sema/ConstraintSystem.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,20 @@ Type ConstraintSystem::getUnopenedTypeOfReference(
16541654
return requestedType;
16551655
}
16561656

1657+
void ConstraintSystem::recordOpenedType(
1658+
ConstraintLocator *locator, ArrayRef<OpenedType> openedTypes) {
1659+
bool inserted = OpenedTypes.insert({locator, openedTypes}).second;
1660+
if (inserted) {
1661+
if (isRecordingChanges())
1662+
recordChange(SolverTrail::Change::recordedOpenedTypes(locator));
1663+
}
1664+
}
1665+
1666+
void ConstraintSystem::removeOpenedType(ConstraintLocator *locator) {
1667+
bool erased = OpenedTypes.erase(locator);
1668+
ASSERT(erased);
1669+
}
1670+
16571671
void ConstraintSystem::recordOpenedTypes(
16581672
ConstraintLocatorBuilder locator,
16591673
const OpenedTypeMap &replacements) {
@@ -1673,20 +1687,12 @@ void ConstraintSystem::recordOpenedTypes(
16731687

16741688
ConstraintLocator *locatorPtr = getConstraintLocator(locator);
16751689
assert(locatorPtr && "No locator for opened types?");
1676-
#if false
1677-
assert(std::find_if(OpenedTypes.begin(), OpenedTypes.end(),
1678-
[&](const std::pair<ConstraintLocator *,
1679-
ArrayRef<OpenedType>> &entry) {
1680-
return entry.first == locatorPtr;
1681-
}) == OpenedTypes.end() &&
1682-
"already registered opened types for this locator");
1683-
#endif
16841690

16851691
OpenedType* openedTypes
16861692
= Allocator.Allocate<OpenedType>(replacements.size());
16871693
std::copy(replacements.begin(), replacements.end(), openedTypes);
1688-
OpenedTypes.insert(
1689-
{locatorPtr, llvm::ArrayRef(openedTypes, replacements.size())});
1694+
recordOpenedType(
1695+
locatorPtr, llvm::ArrayRef(openedTypes, replacements.size()));
16901696
}
16911697

16921698
/// Determine how many levels of argument labels should be removed from the

0 commit comments

Comments
 (0)