Skip to content

Commit a9f3922

Browse files
committed
Sema: Record argument matching choices in the trail
1 parent 8b4a58f commit a9f3922

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class SolverTrail {
6363
RecordedDisjunctionChoice,
6464
/// Recorded an applied disjunction.
6565
RecordedAppliedDisjunction,
66+
/// Recorded an argument matching choice.
67+
RecordedMatchCallArgumentResult,
6668
};
6769

6870
/// A change made to the constraint system.
@@ -182,6 +184,9 @@ class SolverTrail {
182184
/// Create a change that recorded an applied disjunction.
183185
static Change recordedAppliedDisjunction(ConstraintLocator *locator);
184186

187+
/// Create a change that recorded an applied disjunction.
188+
static Change recordedMatchCallArgumentResult(ConstraintLocator *locator);
189+
185190
/// Undo this change, reverting the constraint graph to the state it
186191
/// had prior to this change.
187192
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ class Solution {
15191519

15201520
/// For locators associated with call expressions, the trailing closure
15211521
/// matching rule and parameter bindings that were applied.
1522-
llvm::MapVector<ConstraintLocator *, MatchCallArgumentResult>
1522+
llvm::DenseMap<ConstraintLocator *, MatchCallArgumentResult>
15231523
argumentMatchingChoices;
15241524

15251525
/// The set of disjunction choices used to arrive at this solution,
@@ -2341,7 +2341,7 @@ class ConstraintSystem {
23412341

23422342
/// For locators associated with call expressions, the trailing closure
23432343
/// matching rule and parameter bindings that were applied.
2344-
llvm::MapVector<ConstraintLocator *, MatchCallArgumentResult>
2344+
llvm::DenseMap<ConstraintLocator *, MatchCallArgumentResult>
23452345
argumentMatchingChoices;
23462346

23472347
/// The set of implicit value conversions performed by the solver on
@@ -2883,9 +2883,6 @@ class ConstraintSystem {
28832883
/// FIXME: Remove this.
28842884
unsigned numFixes;
28852885

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

@@ -3637,9 +3634,16 @@ class ConstraintSystem {
36373634
/// \param type The type on which to holeify.
36383635
void recordTypeVariablesAsHoles(Type type);
36393636

3637+
/// Add a MatchCallArgumentResult and record the change in the trail.
36403638
void recordMatchCallArgumentResult(ConstraintLocator *locator,
36413639
MatchCallArgumentResult result);
36423640

3641+
/// Undo the above change.
3642+
void removeMatchCallArgumentResult(ConstraintLocator *locator) {
3643+
bool erased = argumentMatchingChoices.erase(locator);
3644+
ASSERT(erased);
3645+
}
3646+
36433647
/// Record implicitly generated `callAsFunction` with root at the
36443648
/// given expression, located at \c locator.
36453649
void recordCallAsFunction(UnresolvedDotExpr *root, ArgumentList *arguments,

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14925,7 +14925,11 @@ void ConstraintSystem::recordTypeVariablesAsHoles(Type type) {
1492514925
void ConstraintSystem::recordMatchCallArgumentResult(
1492614926
ConstraintLocator *locator, MatchCallArgumentResult result) {
1492714927
assert(locator->isLastElement<LocatorPathElt::ApplyArgument>());
14928-
argumentMatchingChoices.insert({locator, result});
14928+
bool inserted = argumentMatchingChoices.insert({locator, result}).second;
14929+
if (inserted) {
14930+
if (isRecordingChanges())
14931+
recordChange(SolverTrail::Change::recordedMatchCallArgumentResult(locator));
14932+
}
1492914933
}
1493014934

1493114935
void ConstraintSystem::recordCallAsFunction(UnresolvedDotExpr *root,

lib/Sema/CSSolver.cpp

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

318318
// Remember all of the argument/parameter matching choices we made.
319319
for (auto &argumentMatch : solution.argumentMatchingChoices) {
320-
argumentMatchingChoices.insert(argumentMatch);
320+
recordMatchCallArgumentResult(argumentMatch.first, argumentMatch.second);
321321
}
322322

323323
// Remember implied results.
@@ -672,7 +672,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
672672

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

738-
// Remove any argument matching choices;
739-
truncate(cs.argumentMatchingChoices, numArgumentMatchingChoices);
740-
741737
// Remove any opened types.
742738
truncate(cs.OpenedTypes, numOpenedTypes);
743739

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ SolverTrail::Change::recordedAppliedDisjunction(ConstraintLocator *locator) {
169169
return result;
170170
}
171171

172+
SolverTrail::Change
173+
SolverTrail::Change::recordedMatchCallArgumentResult(ConstraintLocator *locator) {
174+
Change result;
175+
result.Kind = ChangeKind::RecordedMatchCallArgumentResult;
176+
result.Locator = locator;
177+
return result;
178+
}
179+
172180
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
173181
auto &cg = cs.getConstraintGraph();
174182

@@ -229,6 +237,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
229237
case ChangeKind::RecordedAppliedDisjunction:
230238
cs.removeAppliedDisjunction(Locator);
231239
break;
240+
241+
case ChangeKind::RecordedMatchCallArgumentResult:
242+
cs.removeMatchCallArgumentResult(Locator);
243+
break;
232244
}
233245
}
234246

@@ -352,6 +364,12 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
352364
Locator->dump(&cs.getASTContext().SourceMgr, out);
353365
out << ")\n";
354366
break;
367+
368+
case ChangeKind::RecordedMatchCallArgumentResult:
369+
out << "(recorded argument matching choice at ";
370+
Locator->dump(&cs.getASTContext().SourceMgr, out);
371+
out << ")\n";
372+
break;
355373
}
356374
}
357375

0 commit comments

Comments
 (0)