Skip to content

Commit 1d18cd0

Browse files
committed
Sema: Record closure types in the trail
1 parent 43a4ac9 commit 1d18cd0

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ CHANGE(DisabledConstraint)
5454
CHANGE(FavoredConstraint)
5555
CHANGE(RecordedResultBuilderTransform)
5656
CHANGE(AppliedPropertyWrapper)
57+
CHANGE(RecordedClosureType)
5758

58-
LAST_CHANGE(AppliedPropertyWrapper)
59+
LAST_CHANGE(RecordedClosureType)
5960

6061
#undef LOCATOR_CHANGE
6162
#undef LAST_CHANGE

include/swift/Sema/CSTrail.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class SolverTrail {
124124
PackElementExpr *ElementExpr;
125125
Expr *TheExpr;
126126
AnyFunctionRef AFR;
127+
const ClosureExpr *Closure;
127128
};
128129

129130
Change() : Kind(ChangeKind::AddedTypeVariable), TypeVar(nullptr) { }
@@ -205,6 +206,9 @@ class SolverTrail {
205206
/// Create a change that recorded a result builder transform.
206207
static Change AppliedPropertyWrapper(Expr *anchor);
207208

209+
/// Create a change that recorded a closure type.
210+
static Change RecordedClosureType(const ClosureExpr *closure);
211+
208212
/// Undo this change, reverting the constraint graph to the state it
209213
/// had prior to this change.
210214
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,9 @@ class ConstraintSystem {
22212221

22222222
/// Maps discovered closures to their types inferred
22232223
/// from declared parameters/result and body.
2224+
///
2225+
/// This is a MapVector because contractEdges() iterates over it and
2226+
/// may depend on order.
22242227
llvm::MapVector<const ClosureExpr *, FunctionType *> ClosureTypes;
22252228

22262229
/// Maps closures and local functions to the pack expansion expressions they
@@ -2857,9 +2860,6 @@ class ConstraintSystem {
28572860
/// FIXME: Remove this.
28582861
unsigned numFixes;
28592862

2860-
/// The length of \c ClosureTypes.
2861-
unsigned numInferredClosureTypes;
2862-
28632863
/// The length of \c ImpliedResults.
28642864
unsigned numImpliedResults;
28652865

@@ -3083,10 +3083,18 @@ class ConstraintSystem {
30833083
}
30843084

30853085
void setClosureType(const ClosureExpr *closure, FunctionType *type) {
3086-
assert(closure);
3087-
assert(type && "Expected non-null type");
3088-
assert(ClosureTypes.count(closure) == 0 && "Cannot reset closure type");
3089-
ClosureTypes.insert({closure, type});
3086+
ASSERT(closure);
3087+
ASSERT(type);
3088+
bool inserted = ClosureTypes.insert({closure, type}).second;
3089+
ASSERT(inserted);
3090+
3091+
if (solverState)
3092+
recordChange(SolverTrail::Change::RecordedClosureType(closure));
3093+
}
3094+
3095+
void removeClosureType(const ClosureExpr *closure) {
3096+
bool erased = ClosureTypes.erase(closure);
3097+
ASSERT(erased);
30903098
}
30913099

30923100
FunctionType *getClosureType(const ClosureExpr *closure) const {

lib/Sema/CSSolver.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
690690
numTypeVariables = cs.TypeVariables.size();
691691
numFixes = cs.Fixes.size();
692692
numKeyPaths = cs.KeyPaths.size();
693-
numInferredClosureTypes = cs.ClosureTypes.size();
694693
numImpliedResults = cs.ImpliedResults.size();
695694
numContextualTypes = cs.contextualTypes.size();
696695
numTargets = cs.targets.size();
@@ -738,9 +737,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
738737
/// Remove any key path expressions.
739738
truncate(cs.KeyPaths, numKeyPaths);
740739

741-
// Remove any inferred closure types (e.g. used in result builder body).
742-
truncate(cs.ClosureTypes, numInferredClosureTypes);
743-
744740
// Remove any implied results.
745741
truncate(cs.ImpliedResults, numImpliedResults);
746742

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ SolverTrail::Change::AppliedPropertyWrapper(Expr *expr) {
247247
return result;
248248
}
249249

250+
SolverTrail::Change
251+
SolverTrail::Change::RecordedClosureType(const ClosureExpr *closure) {
252+
Change result;
253+
result.Kind = ChangeKind::RecordedClosureType;
254+
result.Closure = closure;
255+
return result;
256+
}
257+
250258
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
251259
auto &cg = cs.getConstraintGraph();
252260

@@ -364,6 +372,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
364372
case ChangeKind::ResolvedOverload:
365373
cs.removeResolvedOverload(Locator);
366374
break;
375+
376+
case ChangeKind::RecordedClosureType:
377+
cs.removeClosureType(Closure);
378+
break;
367379
}
368380
}
369381

@@ -551,6 +563,12 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
551563
simple_display(out, TheExpr);
552564
out << ")\n";
553565
break;
566+
567+
case ChangeKind::RecordedClosureType:
568+
out << "(RecordedClosureType ";
569+
simple_display(out, Closure);
570+
out << ")\n";
571+
break;
554572
}
555573
}
556574

0 commit comments

Comments
 (0)