Skip to content

Commit 516277f

Browse files
committed
Sema: Record contextual types in the trail
1 parent 666361a commit 516277f

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ CHANGE(DisabledConstraint)
6161
CHANGE(FavoredConstraint)
6262
CHANGE(RecordedResultBuilderTransform)
6363
CHANGE(RecordedClosureType)
64+
CHANGE(RecordedContextualInfo)
6465

65-
LAST_CHANGE(RecordedClosureType)
66+
LAST_CHANGE(RecordedContextualInfo)
6667

6768
#undef LOCATOR_CHANGE
6869
#undef EXPR_CHANGE

include/swift/Sema/CSTrail.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ class SolverTrail {
207207
/// Create a change that recorded a closure type.
208208
static Change RecordedClosureType(const ClosureExpr *closure);
209209

210+
/// Create a change that recorded the contextual type of an AST node.
211+
static Change RecordedContextualInfo(ASTNode node);
212+
210213
/// Undo this change, reverting the constraint graph to the state it
211214
/// had prior to this change.
212215
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,7 +2281,7 @@ class ConstraintSystem {
22812281
/// constraint system. The second type, if valid, contains the type as it
22822282
/// should appear in actual constraint. This will have unbound generic types
22832283
/// opened, placeholder types converted to type variables, etc.
2284-
llvm::MapVector<ASTNode, std::pair<ContextualTypeInfo, Type>> contextualTypes;
2284+
llvm::DenseMap<ASTNode, std::pair<ContextualTypeInfo, Type>> contextualTypes;
22852285

22862286
/// Information about each case label item tracked by the constraint system.
22872287
llvm::SmallMapVector<const CaseLabelItem *, CaseLabelItemInfo, 4>
@@ -2860,9 +2860,6 @@ class ConstraintSystem {
28602860
/// FIXME: Remove this.
28612861
unsigned numFixes;
28622862

2863-
/// The length of \c contextualTypes.
2864-
unsigned numContextualTypes;
2865-
28662863
/// The length of \c targets.
28672864
unsigned numTargets;
28682865

@@ -3294,10 +3291,17 @@ class ConstraintSystem {
32943291
}
32953292

32963293
void setContextualInfo(ASTNode node, ContextualTypeInfo info) {
3297-
assert(bool(node) && "Expected non-null expression!");
3298-
assert(contextualTypes.count(node) == 0 &&
3299-
"Already set this contextual type");
3300-
contextualTypes[node] = {info, Type()};
3294+
ASSERT(bool(node) && "Expected non-null expression!");
3295+
bool inserted = contextualTypes.insert({node, {info, Type()}}).second;
3296+
ASSERT(inserted);
3297+
3298+
if (solverState)
3299+
recordChange(SolverTrail::Change::RecordedContextualInfo(node));
3300+
}
3301+
3302+
void removeContextualInfo(ASTNode node) {
3303+
bool erased = contextualTypes.erase(node);
3304+
ASSERT(erased);
33013305
}
33023306

33033307
std::optional<ContextualTypeInfo> getContextualTypeInfo(ASTNode node) const {

lib/Sema/CSSolver.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
692692
numTypeVariables = cs.TypeVariables.size();
693693
numFixes = cs.Fixes.size();
694694
numKeyPaths = cs.KeyPaths.size();
695-
numContextualTypes = cs.contextualTypes.size();
696695
numTargets = cs.targets.size();
697696
numCaseLabelItems = cs.caseLabelItems.size();
698697
numPotentialThrowSites = cs.potentialThrowSites.size();
@@ -738,9 +737,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
738737
/// Remove any key path expressions.
739738
truncate(cs.KeyPaths, numKeyPaths);
740739

741-
// Remove any contextual types.
742-
truncate(cs.contextualTypes, numContextualTypes);
743-
744740
// Remove any targets.
745741
truncate(cs.targets, numTargets);
746742

lib/Sema/CSTrail.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ SolverTrail::Change::RecordedClosureType(const ClosureExpr *closure) {
255255
return result;
256256
}
257257

258+
SolverTrail::Change
259+
SolverTrail::Change::RecordedContextualInfo(ASTNode node) {
260+
Change result;
261+
result.Kind = ChangeKind::RecordedContextualInfo;
262+
result.Node.Node = node;
263+
return result;
264+
}
265+
258266
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
259267
auto &cg = cs.getConstraintGraph();
260268

@@ -380,6 +388,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
380388
case ChangeKind::RecordedImpliedResult:
381389
cs.removeImpliedResult(TheExpr);
382390
break;
391+
392+
case ChangeKind::RecordedContextualInfo:
393+
cs.removeContextualInfo(Node.Node);
394+
break;
383395
}
384396
}
385397

@@ -573,6 +585,11 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
573585
simple_display(out, Closure);
574586
out << ")\n";
575587
break;
588+
589+
case ChangeKind::RecordedContextualInfo:
590+
// FIXME: Print short form of ASTNode
591+
out << "(RecordedContextualInfo)\n";
592+
break;
576593
}
577594
}
578595

0 commit comments

Comments
 (0)