Skip to content

Commit 7d3350d

Browse files
committed
Sema: Record opened existential types in the trail
1 parent f963f36 commit 7d3350d

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class SolverTrail {
6767
RecordedMatchCallArgumentResult,
6868
/// Recorded a list of opened types at a locator.
6969
RecordedOpenedTypes,
70+
/// Recorded the opening of an existential type at a locator.
71+
RecordedOpenedExistentialType,
7072
};
7173

7274
/// A change made to the constraint system.
@@ -192,6 +194,9 @@ class SolverTrail {
192194
/// Create a change that recorded a list of opened types.
193195
static Change recordedOpenedTypes(ConstraintLocator *locator);
194196

197+
/// Create a change that recorded the opening of an existential type.
198+
static Change recordedOpenedExistentialType(ConstraintLocator *locator);
199+
195200
/// Undo this change, reverting the constraint graph to the state it
196201
/// had prior to this change.
197202
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,7 @@ class ConstraintSystem {
23912391

23922392
/// A mapping from constraint locators to the opened existential archetype
23932393
/// used for the 'self' of an existential type.
2394-
llvm::SmallMapVector<ConstraintLocator *, OpenedArchetypeType *, 4>
2394+
llvm::SmallDenseMap<ConstraintLocator *, OpenedArchetypeType *, 4>
23952395
OpenedExistentialTypes;
23962396

23972397
llvm::SmallMapVector<PackExpansionType *, TypeVariableType *, 4>
@@ -2883,9 +2883,6 @@ class ConstraintSystem {
28832883
/// FIXME: Remove this.
28842884
unsigned numFixes;
28852885

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

@@ -3456,6 +3453,16 @@ class ConstraintSystem {
34563453
std::pair<Type, OpenedArchetypeType *> openExistentialType(
34573454
Type type, ConstraintLocator *locator);
34583455

3456+
/// Update OpenedExistentials and record a change in the trail.
3457+
void recordOpenedExistentialType(ConstraintLocator *locator,
3458+
OpenedArchetypeType *opened);
3459+
3460+
/// Undo the above change.
3461+
void removeOpenedExistentialType(ConstraintLocator *locator) {
3462+
bool erased = OpenedExistentialTypes.erase(locator);
3463+
ASSERT(erased);
3464+
}
3465+
34593466
/// Get the opened element generic environment for the given locator.
34603467
GenericEnvironment *getPackElementEnvironment(ConstraintLocator *locator,
34613468
CanType shapeClass);

lib/Sema/CSSolver.cpp

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

332332
// Register the solution's opened existential types.
333333
for (const auto &openedExistential : solution.OpenedExistentialTypes) {
334-
OpenedExistentialTypes.insert(openedExistential);
334+
recordOpenedExistentialType(openedExistential.first, openedExistential.second);
335335
}
336336

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

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

736-
// Remove any opened existential types.
737-
truncate(cs.OpenedExistentialTypes, numOpenedExistentialTypes);
738-
739735
// Remove any opened pack expansion types.
740736
truncate(cs.OpenedPackExpansionTypes, numOpenedPackExpansionTypes);
741737

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ SolverTrail::Change::recordedOpenedTypes(ConstraintLocator *locator) {
185185
return result;
186186
}
187187

188+
SolverTrail::Change
189+
SolverTrail::Change::recordedOpenedExistentialType(ConstraintLocator *locator) {
190+
Change result;
191+
result.Kind = ChangeKind::RecordedOpenedExistentialType;
192+
result.Locator = locator;
193+
return result;
194+
}
195+
188196
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
189197
auto &cg = cs.getConstraintGraph();
190198

@@ -253,6 +261,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
253261
case ChangeKind::RecordedOpenedTypes:
254262
cs.removeOpenedType(Locator);
255263
break;
264+
265+
case ChangeKind::RecordedOpenedExistentialType:
266+
cs.removeOpenedExistentialType(Locator);
267+
break;
256268
}
257269
}
258270

@@ -388,6 +400,12 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
388400
Locator->dump(&cs.getASTContext().SourceMgr, out);
389401
out << ")\n";
390402
break;
403+
404+
case ChangeKind::RecordedOpenedExistentialType:
405+
out << "(recorded opened existential type at ";
406+
Locator->dump(&cs.getASTContext().SourceMgr, out);
407+
out << ")\n";
408+
break;
391409
}
392410
}
393411

lib/Sema/ConstraintSystem.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -845,11 +845,20 @@ std::pair<Type, OpenedArchetypeType *> ConstraintSystem::openExistentialType(
845845
t = t->getMetatypeInstanceType();
846846
auto *opened = t->castTo<OpenedArchetypeType>();
847847

848-
assert(OpenedExistentialTypes.count(locator) == 0);
849-
OpenedExistentialTypes.insert({locator, opened});
848+
recordOpenedExistentialType(locator, opened);
849+
850850
return {result, opened};
851851
}
852852

853+
void ConstraintSystem::recordOpenedExistentialType(
854+
ConstraintLocator *locator, OpenedArchetypeType *opened) {
855+
bool inserted = OpenedExistentialTypes.insert({locator, opened}).second;
856+
if (inserted) {
857+
if (isRecordingChanges())
858+
recordChange(SolverTrail::Change::recordedOpenedExistentialType(locator));
859+
}
860+
}
861+
853862
GenericEnvironment *
854863
ConstraintSystem::getPackElementEnvironment(ConstraintLocator *locator,
855864
CanType shapeClass) {
@@ -2740,8 +2749,7 @@ DeclReferenceType ConstraintSystem::getTypeOfMemberReference(
27402749
} else if (baseObjTy->isExistentialType()) {
27412750
auto openedArchetype =
27422751
OpenedArchetypeType::get(baseObjTy->getCanonicalType());
2743-
OpenedExistentialTypes.insert(
2744-
{getConstraintLocator(locator), openedArchetype});
2752+
recordOpenedExistentialType(getConstraintLocator(locator), openedArchetype);
27452753
baseOpenedTy = openedArchetype;
27462754
}
27472755

0 commit comments

Comments
 (0)