Skip to content

Commit 1d177d0

Browse files
committed
Sema: Record expression patterns in the trail
1 parent 500acd1 commit 1d177d0

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ LOCATOR_CHANGE(ResolvedOverload)
4040

4141
EXPR_CHANGE(AppliedPropertyWrapper)
4242
EXPR_CHANGE(RecordedImpliedResult)
43+
EXPR_CHANGE(RecordedExprPattern)
4344

4445
CHANGE(AddedTypeVariable)
4546
CHANGE(AddedConstraint)

include/swift/Sema/ConstraintSystem.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ class Solution {
15911591

15921592
/// A map of expressions to the ExprPatterns that they are being solved as
15931593
/// a part of.
1594-
llvm::MapVector<Expr *, ExprPattern *> exprPatterns;
1594+
llvm::DenseMap<Expr *, ExprPattern *> exprPatterns;
15951595

15961596
/// The set of parameters that have been inferred to be 'isolated'.
15971597
std::vector<ParamDecl *> isolatedParams;
@@ -2289,7 +2289,7 @@ class ConstraintSystem {
22892289

22902290
/// A map of expressions to the ExprPatterns that they are being solved as
22912291
/// a part of.
2292-
llvm::SmallMapVector<Expr *, ExprPattern *, 2> exprPatterns;
2292+
llvm::SmallDenseMap<Expr *, ExprPattern *, 2> exprPatterns;
22932293

22942294
/// The set of parameters that have been inferred to be 'isolated'.
22952295
llvm::SmallSetVector<ParamDecl *, 2> isolatedParams;
@@ -2855,9 +2855,6 @@ class ConstraintSystem {
28552855
/// FIXME: Remove this.
28562856
unsigned numFixes;
28572857

2858-
/// The length of \c exprPatterns.
2859-
unsigned numExprPatterns;
2860-
28612858
/// The length of \c isolatedParams.
28622859
unsigned numIsolatedParams;
28632860

@@ -3401,11 +3398,19 @@ class ConstraintSystem {
34013398

34023399
/// Record a given ExprPattern as the parent of its sub-expression.
34033400
void setExprPatternFor(Expr *E, ExprPattern *EP) {
3404-
assert(E);
3405-
assert(EP);
3406-
auto inserted = exprPatterns.insert({E, EP}).second;
3407-
assert(inserted && "Mapping already defined?");
3408-
(void)inserted;
3401+
ASSERT(E);
3402+
ASSERT(EP);
3403+
bool inserted = exprPatterns.insert({E, EP}).second;
3404+
ASSERT(inserted);
3405+
3406+
if (solverState)
3407+
recordChange(SolverTrail::Change::RecordedExprPattern(E));
3408+
}
3409+
3410+
/// Record a given ExprPattern as the parent of its sub-expression.
3411+
void removeExprPatternFor(Expr *E) {
3412+
bool erased = exprPatterns.erase(E);
3413+
ASSERT(erased);
34093414
}
34103415

34113416
std::optional<CaseLabelItemInfo>

lib/Sema/CSSolver.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,10 @@ void ConstraintSystem::applySolution(const Solution &solution) {
409409
isolatedParams.insert(param);
410410
}
411411

412-
for (auto &pair : solution.exprPatterns)
413-
exprPatterns.insert(pair);
412+
for (auto &pair : solution.exprPatterns) {
413+
if (exprPatterns.count(pair.first) == 0)
414+
setExprPatternFor(pair.first, pair.second);
415+
}
414416

415417
for (auto closure : solution.preconcurrencyClosures) {
416418
preconcurrencyClosures.insert(closure);
@@ -695,7 +697,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
695697
numTypeVariables = cs.TypeVariables.size();
696698
numFixes = cs.Fixes.size();
697699
numKeyPaths = cs.KeyPaths.size();
698-
numExprPatterns = cs.exprPatterns.size();
699700
numIsolatedParams = cs.isolatedParams.size();
700701
numPreconcurrencyClosures = cs.preconcurrencyClosures.size();
701702
numImplicitValueConversions = cs.ImplicitValueConversions.size();
@@ -737,9 +738,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
737738
/// Remove any key path expressions.
738739
truncate(cs.KeyPaths, numKeyPaths);
739740

740-
// Remove any ExprPattern mappings.
741-
truncate(cs.exprPatterns, numExprPatterns);
742-
743741
// Remove any isolated parameters.
744742
truncate(cs.isolatedParams, numIsolatedParams);
745743

lib/Sema/CSTrail.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
489489
case ChangeKind::RecordedPotentialThrowSite:
490490
cs.removePotentialThrowSite(TheCatchNode);
491491
break;
492+
493+
case ChangeKind::RecordedExprPattern:
494+
cs.removeExprPatternFor(TheExpr);
495+
break;
492496
}
493497
}
494498

0 commit comments

Comments
 (0)