Skip to content

Commit f2412f3

Browse files
committed
Sema: Record key path expressions in the trail
1 parent 881a010 commit f2412f3

File tree

6 files changed

+54
-19
lines changed

6 files changed

+54
-19
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ CHANGE(RecordedTarget)
7474
CHANGE(RecordedCaseLabelItemInfo)
7575
CHANGE(RecordedPotentialThrowSite)
7676
CHANGE(RecordedIsolatedParam)
77+
CHANGE(RecordedKeyPath)
7778

78-
LAST_CHANGE(RecordedIsolatedParam)
79+
LAST_CHANGE(RecordedKeyPath)
7980

8081
#undef LOCATOR_CHANGE
8182
#undef EXPR_CHANGE

include/swift/Sema/CSTrail.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ class SolverTrail {
226226
/// Create a change that recorded an isolated parameter.
227227
static Change RecordedIsolatedParam(ParamDecl *param);
228228

229+
/// Create a change that recorded a key path expression.
230+
static Change RecordedKeyPath(KeyPathExpr *expr);
231+
229232
/// Undo this change, reverting the constraint graph to the state it
230233
/// had prior to this change.
231234
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,9 +1567,9 @@ class Solution {
15671567

15681568
/// The key path expression and its root type, value type, and decl context
15691569
/// introduced by this solution.
1570-
llvm::MapVector<const KeyPathExpr *,
1571-
std::tuple</*root=*/TypeVariableType *,
1572-
/*value=*/TypeVariableType *, DeclContext *>>
1570+
llvm::DenseMap<const KeyPathExpr *,
1571+
std::tuple</*root=*/TypeVariableType *,
1572+
/*value=*/TypeVariableType *, DeclContext *>>
15731573
KeyPaths;
15741574

15751575
/// Contextual types introduced by this solution.
@@ -2265,9 +2265,9 @@ class ConstraintSystem {
22652265
KeyPathComponentTypes;
22662266

22672267
/// Maps a key path root, value, and decl context to the key path expression.
2268-
llvm::MapVector<const KeyPathExpr *,
2269-
std::tuple</*root=*/TypeVariableType *,
2270-
/*value=*/TypeVariableType *, DeclContext *>>
2268+
llvm::DenseMap<const KeyPathExpr *,
2269+
std::tuple</*root=*/TypeVariableType *,
2270+
/*value=*/TypeVariableType *, DeclContext *>>
22712271
KeyPaths;
22722272

22732273
/// Maps AST entries to their targets.
@@ -2850,9 +2850,6 @@ class ConstraintSystem {
28502850
/// FIXME: Remove this.
28512851
unsigned numFixes;
28522852

2853-
/// The length of \c KeyPaths.
2854-
unsigned numKeyPaths;
2855-
28562853
/// The length of \c ArgumentLists.
28572854
unsigned numArgumentLists;
28582855

@@ -3641,10 +3638,13 @@ class ConstraintSystem {
36413638
ConstraintLocator *locator);
36423639

36433640
/// Record root, value, and declContext of keypath expression for use across
3644-
/// constraint system.
3645-
void recordKeyPath(KeyPathExpr *keypath, TypeVariableType *root,
3641+
/// constraint system, and add a change to the trail.
3642+
void recordKeyPath(const KeyPathExpr *keypath, TypeVariableType *root,
36463643
TypeVariableType *value, DeclContext *dc);
36473644

3645+
/// Undo the above change.
3646+
void removeKeyPath(const KeyPathExpr *keypath);
3647+
36483648
/// Walk a closure AST to determine its effects.
36493649
///
36503650
/// \returns a function's extended info describing the effects, as

lib/Sema/CSSimplify.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14983,10 +14983,22 @@ void ConstraintSystem::recordCallAsFunction(UnresolvedDotExpr *root,
1498314983
getConstraintLocator(root, ConstraintLocator::ApplyArgument), arguments);
1498414984
}
1498514985

14986-
void ConstraintSystem::recordKeyPath(KeyPathExpr *keypath,
14986+
void ConstraintSystem::recordKeyPath(const KeyPathExpr *keypath,
1498714987
TypeVariableType *root,
1498814988
TypeVariableType *value, DeclContext *dc) {
14989-
KeyPaths.insert(std::make_pair(keypath, std::make_tuple(root, value, dc)));
14989+
bool inserted = KeyPaths.insert(
14990+
std::make_pair(keypath, std::make_tuple(root, value, dc))).second;
14991+
ASSERT(inserted);
14992+
14993+
if (solverState) {
14994+
recordChange(SolverTrail::Change::RecordedKeyPath(
14995+
const_cast<KeyPathExpr *>(keypath)));
14996+
}
14997+
}
14998+
14999+
void ConstraintSystem::removeKeyPath(const KeyPathExpr *keypath) {
15000+
bool erased = KeyPaths.erase(keypath);
15001+
ASSERT(erased);
1499015002
}
1499115003

1499215004
ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(

lib/Sema/CSSolver.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,12 @@ void ConstraintSystem::applySolution(const Solution &solution) {
378378

379379
// Add key paths.
380380
for (const auto &keypath : solution.KeyPaths) {
381-
KeyPaths.insert(keypath);
381+
if (KeyPaths.count(keypath.first) == 0) {
382+
recordKeyPath(keypath.first,
383+
std::get<0>(keypath.second),
384+
std::get<1>(keypath.second),
385+
std::get<2>(keypath.second));
386+
}
382387
}
383388

384389
// Add the contextual types.
@@ -701,7 +706,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
701706

702707
numTypeVariables = cs.TypeVariables.size();
703708
numFixes = cs.Fixes.size();
704-
numKeyPaths = cs.KeyPaths.size();
705709
numArgumentLists = cs.ArgumentLists.size();
706710
numImplicitCallAsFunctionRoots = cs.ImplicitCallAsFunctionRoots.size();
707711
numSynthesizedConformances = cs.SynthesizedConformances.size();
@@ -737,9 +741,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
737741
// constraints introduced by the current scope.
738742
cs.solverState->rollback(this);
739743

740-
/// Remove any key path expressions.
741-
truncate(cs.KeyPaths, numKeyPaths);
742-
743744
// Remove any argument lists no longer in scope.
744745
truncate(cs.ArgumentLists, numArgumentLists);
745746

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ SolverTrail::Change::RecordedIsolatedParam(ParamDecl *param) {
317317
return result;
318318
}
319319

320+
SolverTrail::Change
321+
SolverTrail::Change::RecordedKeyPath(KeyPathExpr *expr) {
322+
Change result;
323+
result.Kind = ChangeKind::RecordedKeyPath;
324+
result.KeyPath.Expr = expr;
325+
return result;
326+
}
327+
320328
SyntacticElementTargetKey
321329
SolverTrail::Change::getSyntacticElementTargetKey() const {
322330
ASSERT(Kind == ChangeKind::RecordedTarget);
@@ -475,6 +483,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
475483
case ChangeKind::RecordedPreconcurrencyClosure:
476484
cs.removePreconcurrencyClosure(TheClosure);
477485
break;
486+
487+
case ChangeKind::RecordedKeyPath:
488+
cs.removeKeyPath(KeyPath.Expr);
489+
break;
478490
}
479491
}
480492

@@ -688,6 +700,12 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
688700
TheParam->dumpRef(out);
689701
out << ")\n";
690702
break;
703+
704+
case ChangeKind::RecordedKeyPath:
705+
out << "(RecordedKeyPath ";
706+
simple_display(out, KeyPath.Expr);
707+
out << ")\n";
708+
break;
691709
}
692710
}
693711

0 commit comments

Comments
 (0)