Skip to content

Commit 666361a

Browse files
committed
Sema: Record implied results in the trail
1 parent 1d18cd0 commit 666361a

File tree

5 files changed

+46
-34
lines changed

5 files changed

+46
-34
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#define LOCATOR_CHANGE(Name) CHANGE(Name)
2323
#endif
2424

25+
#ifndef EXPR_CHANGE
26+
#define EXPR_CHANGE(Name) CHANGE(Name)
27+
#endif
28+
2529
#ifndef LAST_CHANGE
2630
#define LAST_CHANGE(Name)
2731
#endif
@@ -34,6 +38,9 @@ LOCATOR_CHANGE(RecordedPackExpansionEnvironment)
3438
LOCATOR_CHANGE(RecordedDefaultedConstraint)
3539
LOCATOR_CHANGE(ResolvedOverload)
3640

41+
EXPR_CHANGE(AppliedPropertyWrapper)
42+
EXPR_CHANGE(RecordedImpliedResult)
43+
3744
CHANGE(AddedTypeVariable)
3845
CHANGE(AddedConstraint)
3946
CHANGE(RemovedConstraint)
@@ -53,11 +60,11 @@ CHANGE(RecordedKeyPathComponentType)
5360
CHANGE(DisabledConstraint)
5461
CHANGE(FavoredConstraint)
5562
CHANGE(RecordedResultBuilderTransform)
56-
CHANGE(AppliedPropertyWrapper)
5763
CHANGE(RecordedClosureType)
5864

5965
LAST_CHANGE(RecordedClosureType)
6066

6167
#undef LOCATOR_CHANGE
68+
#undef EXPR_CHANGE
6269
#undef LAST_CHANGE
6370
#undef CHANGE

include/swift/Sema/CSTrail.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class SolverTrail {
130130
Change() : Kind(ChangeKind::AddedTypeVariable), TypeVar(nullptr) { }
131131

132132
#define LOCATOR_CHANGE(Name) static Change Name(ConstraintLocator *locator);
133+
#define EXPR_CHANGE(Name) static Change Name(Expr *expr);
133134
#include "swift/Sema/CSTrail.def"
134135

135136
/// Create a change that added a type variable.
@@ -203,9 +204,6 @@ class SolverTrail {
203204
/// Create a change that recorded a result builder transform.
204205
static Change RecordedResultBuilderTransform(AnyFunctionRef fn);
205206

206-
/// Create a change that recorded a result builder transform.
207-
static Change AppliedPropertyWrapper(Expr *anchor);
208-
209207
/// Create a change that recorded a closure type.
210208
static Change RecordedClosureType(const ClosureExpr *closure);
211209

include/swift/Sema/ConstraintSystem.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,7 @@ class Solution {
15151515
/// Maps expressions for implied results (e.g implicit 'then' statements,
15161516
/// implicit 'return' statements in single expression body closures) to their
15171517
/// result kind.
1518-
llvm::MapVector<const Expr *, ImpliedResultKind> ImpliedResults;
1518+
llvm::DenseMap<Expr *, ImpliedResultKind> ImpliedResults;
15191519

15201520
/// For locators associated with call expressions, the trailing closure
15211521
/// matching rule and parameter bindings that were applied.
@@ -2233,7 +2233,7 @@ class ConstraintSystem {
22332233
/// Maps expressions for implied results (e.g implicit 'then' statements,
22342234
/// implicit 'return' statements in single expression body closures) to their
22352235
/// result kind.
2236-
llvm::MapVector<const Expr *, ImpliedResultKind> ImpliedResults;
2236+
llvm::DenseMap<Expr *, ImpliedResultKind> ImpliedResults;
22372237

22382238
/// This is a *global* list of all result builder bodies that have
22392239
/// been determined to be incorrect by failing constraint generation.
@@ -2860,9 +2860,6 @@ class ConstraintSystem {
28602860
/// FIXME: Remove this.
28612861
unsigned numFixes;
28622862

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

@@ -3065,16 +3062,24 @@ class ConstraintSystem {
30653062
}
30663063

30673064
/// Record an implied result for a ReturnStmt or ThenStmt.
3068-
void recordImpliedResult(const Expr *E, ImpliedResultKind kind) {
3069-
assert(E);
3065+
void recordImpliedResult(Expr *E, ImpliedResultKind kind) {
3066+
ASSERT(E);
30703067
auto inserted = ImpliedResults.insert({E, kind}).second;
3071-
assert(inserted && "Duplicate implied result?");
3072-
(void)inserted;
3068+
ASSERT(inserted && "Duplicate implied result?");
3069+
3070+
if (solverState)
3071+
recordChange(SolverTrail::Change::RecordedImpliedResult(E));
3072+
}
3073+
3074+
/// Undo the above change.
3075+
void removeImpliedResult(Expr *E) {
3076+
bool erased = ImpliedResults.erase(E);
3077+
ASSERT(erased);
30733078
}
30743079

30753080
/// Whether the given expression is the implied result for either a ReturnStmt
30763081
/// or ThenStmt, and if so, the kind of implied result.
3077-
std::optional<ImpliedResultKind> isImpliedResult(const Expr *E) const {
3082+
std::optional<ImpliedResultKind> isImpliedResult(Expr *E) const {
30783083
auto result = ImpliedResults.find(E);
30793084
if (result == ImpliedResults.end())
30803085
return std::nullopt;

lib/Sema/CSSolver.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,10 @@ void ConstraintSystem::applySolution(const Solution &solution) {
323323
}
324324

325325
// Remember implied results.
326-
for (auto impliedResult : solution.ImpliedResults)
327-
ImpliedResults.insert(impliedResult);
326+
for (auto impliedResult : solution.ImpliedResults) {
327+
if (ImpliedResults.count(impliedResult.first) == 0)
328+
recordImpliedResult(impliedResult.first, impliedResult.second);
329+
}
328330

329331
// Register the solution's opened types.
330332
for (const auto &opened : solution.OpenedTypes) {
@@ -690,7 +692,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
690692
numTypeVariables = cs.TypeVariables.size();
691693
numFixes = cs.Fixes.size();
692694
numKeyPaths = cs.KeyPaths.size();
693-
numImpliedResults = cs.ImpliedResults.size();
694695
numContextualTypes = cs.contextualTypes.size();
695696
numTargets = cs.targets.size();
696697
numCaseLabelItems = cs.caseLabelItems.size();
@@ -737,9 +738,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
737738
/// Remove any key path expressions.
738739
truncate(cs.KeyPaths, numKeyPaths);
739740

740-
// Remove any implied results.
741-
truncate(cs.ImpliedResults, numImpliedResults);
742-
743741
// Remove any contextual types.
744742
truncate(cs.contextualTypes, numContextualTypes);
745743

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ SolverTrail::~SolverTrail() {
5656
result.Locator = locator; \
5757
return result; \
5858
}
59+
#define EXPR_CHANGE(Name) \
60+
SolverTrail::Change \
61+
SolverTrail::Change::Name(Expr *expr) { \
62+
Change result; \
63+
result.Kind = ChangeKind::Name; \
64+
result.TheExpr = expr; \
65+
return result; \
66+
}
5967
#include "swift/Sema/CSTrail.def"
6068

6169
SolverTrail::Change
@@ -239,14 +247,6 @@ SolverTrail::Change::RecordedResultBuilderTransform(AnyFunctionRef fn) {
239247
return result;
240248
}
241249

242-
SolverTrail::Change
243-
SolverTrail::Change::AppliedPropertyWrapper(Expr *expr) {
244-
Change result;
245-
result.Kind = ChangeKind::AppliedPropertyWrapper;
246-
result.TheExpr = expr;
247-
return result;
248-
}
249-
250250
SolverTrail::Change
251251
SolverTrail::Change::RecordedClosureType(const ClosureExpr *closure) {
252252
Change result;
@@ -376,6 +376,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
376376
case ChangeKind::RecordedClosureType:
377377
cs.removeClosureType(Closure);
378378
break;
379+
380+
case ChangeKind::RecordedImpliedResult:
381+
cs.removeImpliedResult(TheExpr);
382+
break;
379383
}
380384
}
381385

@@ -395,6 +399,12 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
395399
Locator->dump(&cs.getASTContext().SourceMgr, out); \
396400
out << ")\n"; \
397401
break;
402+
#define EXPR_CHANGE(Name) \
403+
case ChangeKind::Name: \
404+
out << "(" << #Name << " "; \
405+
simple_display(out, TheExpr); \
406+
out << ")\n"; \
407+
break;
398408
#include "swift/Sema/CSTrail.def"
399409

400410
case ChangeKind::AddedTypeVariable:
@@ -558,12 +568,6 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
558568
out << ")\n";
559569
break;
560570

561-
case ChangeKind::AppliedPropertyWrapper:
562-
out << "(AppliedPropertyWrapper ";
563-
simple_display(out, TheExpr);
564-
out << ")\n";
565-
break;
566-
567571
case ChangeKind::RecordedClosureType:
568572
out << "(RecordedClosureType ";
569573
simple_display(out, Closure);

0 commit comments

Comments
 (0)