Skip to content

Commit c7edc34

Browse files
committed
Sema: Record result builder transforms in the trail
1 parent 64293ec commit c7edc34

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#ifndef SWIFT_SEMA_CSTRAIL_H
1818
#define SWIFT_SEMA_CSTRAIL_H
1919

20+
#include "swift/AST/AnyFunctionRef.h"
21+
#include "swift/AST/Type.h"
22+
#include "swift/AST/Types.h"
2023
#include <vector>
2124

2225
namespace llvm {
@@ -86,6 +89,8 @@ class SolverTrail {
8689
DisabledConstraint,
8790
/// Favored a constraint.
8891
FavoredConstraint,
92+
/// Recorded a result builder transform.
93+
RecordedResultBuilderTransform,
8994
};
9095

9196
/// A change made to the constraint system.
@@ -163,6 +168,7 @@ class SolverTrail {
163168
ConstraintLocator *Locator;
164169
PackExpansionType *ExpansionTy;
165170
PackElementExpr *ElementExpr;
171+
AnyFunctionRef AFR;
166172
};
167173

168174
Change() : Kind(ChangeKind::AddedTypeVariable), TypeVar(nullptr) { }
@@ -253,6 +259,9 @@ class SolverTrail {
253259
/// Create a change that favored a constraint.
254260
static Change favoredConstraint(Constraint *constraint);
255261

262+
/// Create a change that recorded a result builder transform.
263+
static Change recordedResultBuilderTransform(AnyFunctionRef fn);
264+
256265
/// Undo this change, reverting the constraint graph to the state it
257266
/// had prior to this change.
258267
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,8 +2857,6 @@ class ConstraintSystem {
28572857
/// FIXME: Remove this.
28582858
unsigned numFixes;
28592859

2860-
unsigned numResultBuilderTransformed;
2861-
28622860
/// The length of \c appliedPropertyWrappers
28632861
unsigned numAppliedPropertyWrappers;
28642862

@@ -5317,6 +5315,14 @@ class ConstraintSystem {
53175315
ConstraintKind bodyResultConstraintKind,
53185316
Type contextualType, ConstraintLocatorBuilder locator);
53195317

5318+
/// Used by matchResultBuilder() to update resultBuilderTransformed and record
5319+
/// a change in the trail.
5320+
void recordResultBuilderTransform(AnyFunctionRef fn,
5321+
AppliedBuilderTransform transformInfo);
5322+
5323+
/// Undo the above change.
5324+
void removeResultBuilderTransform(AnyFunctionRef fn);
5325+
53205326
/// Matches a wrapped or projected value parameter type to its backing
53215327
/// property wrapper type by applying the property wrapper.
53225328
TypeMatchResult applyPropertyWrapperToParameter(

lib/Sema/BuilderTransform.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,22 +1211,30 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
12111211
transformInfo.transformedBody = transformedBody->second;
12121212

12131213
// Record the transformation.
1214-
assert(
1215-
std::find_if(
1216-
resultBuilderTransformed.begin(), resultBuilderTransformed.end(),
1217-
[&](const std::pair<AnyFunctionRef, AppliedBuilderTransform> &elt) {
1218-
return elt.first == fn;
1219-
}) == resultBuilderTransformed.end() &&
1220-
"already transformed this body along this path!?!");
1221-
resultBuilderTransformed.insert(
1222-
std::make_pair(fn, std::move(transformInfo)));
1214+
recordResultBuilderTransform(fn, std::move(transformInfo));
12231215

12241216
if (generateConstraints(fn, transformInfo.transformedBody))
12251217
return getTypeMatchFailure(locator);
12261218

12271219
return getTypeMatchSuccess();
12281220
}
12291221

1222+
void ConstraintSystem::recordResultBuilderTransform(AnyFunctionRef fn,
1223+
AppliedBuilderTransform transformInfo) {
1224+
bool inserted = resultBuilderTransformed.insert(
1225+
std::make_pair(fn, std::move(transformInfo))).second;
1226+
ASSERT(inserted);
1227+
1228+
if (solverState)
1229+
recordChange(SolverTrail::Change::recordedResultBuilderTransform(fn));
1230+
}
1231+
1232+
/// Undo the above change.
1233+
void ConstraintSystem::removeResultBuilderTransform(AnyFunctionRef fn) {
1234+
bool erased = resultBuilderTransformed.erase(fn);
1235+
ASSERT(erased);
1236+
}
1237+
12301238
namespace {
12311239
class ReturnStmtFinder : public ASTWalker {
12321240
std::vector<ReturnStmt *> ReturnStmts;

lib/Sema/CSSolver.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ void ConstraintSystem::applySolution(const Solution &solution) {
410410
}
411411

412412
for (const auto &transformed : solution.resultBuilderTransformed) {
413-
resultBuilderTransformed.insert(transformed);
413+
if (resultBuilderTransformed.count(transformed.first) == 0)
414+
recordResultBuilderTransform(transformed.first, transformed.second);
414415
}
415416

416417
for (const auto &appliedWrapper : solution.appliedPropertyWrappers) {
@@ -680,7 +681,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
680681
numTypeVariables = cs.TypeVariables.size();
681682
numFixes = cs.Fixes.size();
682683
numKeyPaths = cs.KeyPaths.size();
683-
numResultBuilderTransformed = cs.resultBuilderTransformed.size();
684684
numAppliedPropertyWrappers = cs.appliedPropertyWrappers.size();
685685
numResolvedOverloads = cs.ResolvedOverloads.size();
686686
numInferredClosureTypes = cs.ClosureTypes.size();
@@ -733,9 +733,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
733733
/// Remove any key path expressions.
734734
truncate(cs.KeyPaths, numKeyPaths);
735735

736-
/// Remove any builder transformed closures.
737-
truncate(cs.resultBuilderTransformed, numResultBuilderTransformed);
738-
739736
// Remove any applied property wrappers.
740737
truncate(cs.appliedPropertyWrappers, numAppliedPropertyWrappers);
741738

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ SolverTrail::Change::favoredConstraint(Constraint *constraint) {
262262
return result;
263263
}
264264

265+
SolverTrail::Change
266+
SolverTrail::Change::recordedResultBuilderTransform(AnyFunctionRef fn) {
267+
Change result;
268+
result.Kind = ChangeKind::RecordedResultBuilderTransform;
269+
result.AFR = fn;
270+
return result;
271+
}
272+
265273
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
266274
auto &cg = cs.getConstraintGraph();
267275

@@ -368,6 +376,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
368376
if (TheConstraint.Constraint->isFavored())
369377
TheConstraint.Constraint->setFavored(false);
370378
break;
379+
380+
case ChangeKind::RecordedResultBuilderTransform:
381+
cs.removeResultBuilderTransform(AFR);
382+
break;
371383
}
372384
}
373385

@@ -570,6 +582,12 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
570582
indent + 2);
571583
out << ")\n";
572584
break;
585+
586+
case ChangeKind::RecordedResultBuilderTransform:
587+
out << "(recorded result builder transform ";
588+
simple_display(out, AFR);
589+
out << ")\n";
590+
break;
573591
}
574592
}
575593

0 commit comments

Comments
 (0)