Skip to content

Commit 6862955

Browse files
committed
Sema: Record applied property wrappers in the trail
1 parent c7edc34 commit 6862955

File tree

5 files changed

+59
-12
lines changed

5 files changed

+59
-12
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class SolverTrail {
9191
FavoredConstraint,
9292
/// Recorded a result builder transform.
9393
RecordedResultBuilderTransform,
94+
/// Applied a property wrapper.
95+
AppliedPropertyWrapper,
9496
};
9597

9698
/// A change made to the constraint system.
@@ -168,6 +170,7 @@ class SolverTrail {
168170
ConstraintLocator *Locator;
169171
PackExpansionType *ExpansionTy;
170172
PackElementExpr *ElementExpr;
173+
Expr *TheExpr;
171174
AnyFunctionRef AFR;
172175
};
173176

@@ -262,6 +265,9 @@ class SolverTrail {
262265
/// Create a change that recorded a result builder transform.
263266
static Change recordedResultBuilderTransform(AnyFunctionRef fn);
264267

268+
/// Create a change that recorded a result builder transform.
269+
static Change appliedPropertyWrapper(Expr *anchor);
270+
265271
/// Undo this change, reverting the constraint graph to the state it
266272
/// had prior to this change.
267273
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,7 @@ class Solution {
16041604
resultBuilderTransformed;
16051605

16061606
/// A map from argument expressions to their applied property wrapper expressions.
1607-
llvm::MapVector<ASTNode, SmallVector<AppliedPropertyWrapper, 2>> appliedPropertyWrappers;
1607+
llvm::DenseMap<ASTNode, SmallVector<AppliedPropertyWrapper, 2>> appliedPropertyWrappers;
16081608

16091609
/// A mapping from the constraint locators for references to various
16101610
/// names (e.g., member references, normal name references, possible
@@ -2403,7 +2403,7 @@ class ConstraintSystem {
24032403

24042404
public:
24052405
/// A map from argument expressions to their applied property wrapper expressions.
2406-
llvm::SmallMapVector<ASTNode, SmallVector<AppliedPropertyWrapper, 2>, 4>
2406+
llvm::SmallDenseMap<ASTNode, SmallVector<AppliedPropertyWrapper, 2>, 4>
24072407
appliedPropertyWrappers;
24082408

24092409
/// The locators of \c Defaultable constraints whose defaults were used.
@@ -2857,9 +2857,6 @@ class ConstraintSystem {
28572857
/// FIXME: Remove this.
28582858
unsigned numFixes;
28592859

2860-
/// The length of \c appliedPropertyWrappers
2861-
unsigned numAppliedPropertyWrappers;
2862-
28632860
/// The length of \c ResolvedOverloads.
28642861
unsigned numResolvedOverloads;
28652862

@@ -5329,6 +5326,13 @@ class ConstraintSystem {
53295326
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
53305327
ConstraintKind matchKind, ConstraintLocatorBuilder locator);
53315328

5329+
/// Used by applyPropertyWrapperToParameter() to update appliedPropertyWrappers
5330+
/// and record a change in the trail.
5331+
void applyPropertyWrapper(Expr *anchor, AppliedPropertyWrapper applied);
5332+
5333+
/// Undo the above change.
5334+
void removePropertyWrapper(Expr *anchor);
5335+
53325336
/// Determine whether given type variable with its set of bindings is viable
53335337
/// to be attempted on the next step of the solver.
53345338
std::optional<BindingSet> determineBestBindings(

lib/Sema/CSGen.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5046,6 +5046,22 @@ bool ConstraintSystem::generateConstraints(StmtCondition condition,
50465046
return false;
50475047
}
50485048

5049+
void ConstraintSystem::applyPropertyWrapper(
5050+
Expr *anchor, AppliedPropertyWrapper applied) {
5051+
appliedPropertyWrappers[anchor].push_back(applied);
5052+
5053+
if (solverState)
5054+
recordChange(SolverTrail::Change::appliedPropertyWrapper(anchor));
5055+
}
5056+
5057+
void ConstraintSystem::removePropertyWrapper(Expr *anchor) {
5058+
auto found = appliedPropertyWrappers.find(anchor);
5059+
ASSERT(found != appliedPropertyWrappers.end());
5060+
auto &wrappers = found->second;
5061+
ASSERT(!wrappers.empty());
5062+
wrappers.pop_back();
5063+
}
5064+
50495065
ConstraintSystem::TypeMatchResult
50505066
ConstraintSystem::applyPropertyWrapperToParameter(
50515067
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
@@ -5079,13 +5095,13 @@ ConstraintSystem::applyPropertyWrapperToParameter(
50795095
setType(param->getPropertyWrapperProjectionVar(), projectionType);
50805096
}
50815097

5082-
appliedPropertyWrappers[anchor].push_back({ wrapperType, PropertyWrapperInitKind::ProjectedValue });
5098+
applyPropertyWrapper(anchor, { wrapperType, PropertyWrapperInitKind::ProjectedValue });
50835099
} else if (param->hasExternalPropertyWrapper()) {
50845100
Type wrappedValueType = computeWrappedValueType(param, wrapperType);
50855101
addConstraint(matchKind, paramType, wrappedValueType, locator);
50865102
setType(param->getPropertyWrapperWrappedValueVar(), wrappedValueType);
50875103

5088-
appliedPropertyWrappers[anchor].push_back({ wrapperType, PropertyWrapperInitKind::WrappedValue });
5104+
applyPropertyWrapper(anchor, { wrapperType, PropertyWrapperInitKind::WrappedValue });
50895105
} else {
50905106
return getTypeMatchFailure(locator);
50915107
}

lib/Sema/CSSolver.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,14 @@ void ConstraintSystem::applySolution(const Solution &solution) {
415415
}
416416

417417
for (const auto &appliedWrapper : solution.appliedPropertyWrappers) {
418-
appliedPropertyWrappers.insert(appliedWrapper);
418+
auto found = appliedPropertyWrappers.find(appliedWrapper.first);
419+
if (found == appliedPropertyWrappers.end()) {
420+
appliedPropertyWrappers.insert(appliedWrapper);
421+
} else {
422+
auto &existing = found->second;
423+
ASSERT(existing.size() <= appliedWrapper.second.size());
424+
existing = appliedWrapper.second;
425+
}
419426
}
420427

421428
for (auto &valueConversion : solution.ImplicitValueConversions) {
@@ -681,7 +688,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
681688
numTypeVariables = cs.TypeVariables.size();
682689
numFixes = cs.Fixes.size();
683690
numKeyPaths = cs.KeyPaths.size();
684-
numAppliedPropertyWrappers = cs.appliedPropertyWrappers.size();
685691
numResolvedOverloads = cs.ResolvedOverloads.size();
686692
numInferredClosureTypes = cs.ClosureTypes.size();
687693
numImpliedResults = cs.ImpliedResults.size();
@@ -733,9 +739,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
733739
/// Remove any key path expressions.
734740
truncate(cs.KeyPaths, numKeyPaths);
735741

736-
// Remove any applied property wrappers.
737-
truncate(cs.appliedPropertyWrappers, numAppliedPropertyWrappers);
738-
739742
// Remove any inferred closure types (e.g. used in result builder body).
740743
truncate(cs.ClosureTypes, numInferredClosureTypes);
741744

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ SolverTrail::Change::recordedResultBuilderTransform(AnyFunctionRef fn) {
270270
return result;
271271
}
272272

273+
SolverTrail::Change
274+
SolverTrail::Change::appliedPropertyWrapper(Expr *expr) {
275+
Change result;
276+
result.Kind = ChangeKind::AppliedPropertyWrapper;
277+
result.TheExpr = expr;
278+
return result;
279+
}
280+
273281
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
274282
auto &cg = cs.getConstraintGraph();
275283

@@ -380,6 +388,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
380388
case ChangeKind::RecordedResultBuilderTransform:
381389
cs.removeResultBuilderTransform(AFR);
382390
break;
391+
392+
case ChangeKind::AppliedPropertyWrapper:
393+
cs.removePropertyWrapper(TheExpr);
394+
break;
383395
}
384396
}
385397

@@ -588,6 +600,12 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
588600
simple_display(out, AFR);
589601
out << ")\n";
590602
break;
603+
604+
case ChangeKind::AppliedPropertyWrapper:
605+
out << "(applied property wrapper to ";
606+
simple_display(out, TheExpr);
607+
out << ")\n";
608+
break;
591609
}
592610
}
593611

0 commit comments

Comments
 (0)