Skip to content

Commit 12eb7ce

Browse files
committed
Sema: Record implicit callAsFunction() roots in the trail
1 parent 72a6072 commit 12eb7ce

File tree

5 files changed

+18
-22
lines changed

5 files changed

+18
-22
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ LOCATOR_CHANGE(RecordedDefaultedConstraint, DefaultedConstraints)
4444
LOCATOR_CHANGE(ResolvedOverload, ResolvedOverloads)
4545
LOCATOR_CHANGE(RecordedImplicitValueConversion, ImplicitValueConversions)
4646
LOCATOR_CHANGE(RecordedArgumentList, ArgumentLists)
47+
LOCATOR_CHANGE(RecordedImplicitCallAsFunctionRoot, ImplicitCallAsFunctionRoots)
4748

4849
EXPR_CHANGE(AppliedPropertyWrapper)
4950
EXPR_CHANGE(RecordedImpliedResult)

include/swift/Sema/ConstraintSystem.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,7 +2423,7 @@ class ConstraintSystem {
24232423
/// A cache of implicitly generated dot-member expressions used as roots
24242424
/// for some `.callAsFunction` calls. The key here is "base" locator for
24252425
/// the `.callAsFunction` member reference.
2426-
llvm::SmallMapVector<ConstraintLocator *, UnresolvedDotExpr *, 2>
2426+
llvm::SmallDenseMap<ConstraintLocator *, UnresolvedDotExpr *, 2>
24272427
ImplicitCallAsFunctionRoots;
24282428

24292429
/// The set of conformances synthesized during solving (i.e. for
@@ -2855,9 +2855,6 @@ class ConstraintSystem {
28552855
/// FIXME: Remove this.
28562856
unsigned numFixes;
28572857

2858-
/// The length of \c ImplicitCallAsFunctionRoots.
2859-
unsigned numImplicitCallAsFunctionRoots;
2860-
28612858
/// The length of \c SynthesizedConformances.
28622859
unsigned numSynthesizedConformances;
28632860

@@ -3634,10 +3631,8 @@ class ConstraintSystem {
36343631
void recordMatchCallArgumentResult(ConstraintLocator *locator,
36353632
MatchCallArgumentResult result);
36363633

3637-
/// Record implicitly generated `callAsFunction` with root at the
3638-
/// given expression, located at \c locator.
3639-
void recordCallAsFunction(UnresolvedDotExpr *root, ArgumentList *arguments,
3640-
ConstraintLocator *locator);
3634+
void recordImplicitCallAsFunctionRoot(
3635+
ConstraintLocator *locator, UnresolvedDotExpr *root);
36413636

36423637
/// Record root, value, and declContext of keypath expression for use across
36433638
/// constraint system, and add a change to the trail.

lib/Sema/CSSimplify.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12899,7 +12899,11 @@ createImplicitRootForCallAsFunction(ConstraintSystem &cs, Type refType,
1289912899
// Record a type of the new reference in the constraint system.
1290012900
cs.setType(implicitRef, refType);
1290112901
// Record new `.callAsFunction` in the constraint system.
12902-
cs.recordCallAsFunction(implicitRef, arguments, calleeLocator);
12902+
cs.recordImplicitCallAsFunctionRoot(calleeLocator, implicitRef);
12903+
12904+
auto *implicitRefLocator = cs.getConstraintLocator(
12905+
implicitRef, ConstraintLocator::ApplyArgument);
12906+
cs.associateArgumentList(implicitRefLocator, arguments);
1290312907
}
1290412908

1290512909
return implicitRef;
@@ -14974,13 +14978,13 @@ void ConstraintSystem::recordMatchCallArgumentResult(
1497414978
recordChange(SolverTrail::Change::RecordedMatchCallArgumentResult(locator));
1497514979
}
1497614980

14977-
void ConstraintSystem::recordCallAsFunction(UnresolvedDotExpr *root,
14978-
ArgumentList *arguments,
14979-
ConstraintLocator *locator) {
14980-
ImplicitCallAsFunctionRoots.insert({locator, root});
14981+
void ConstraintSystem::recordImplicitCallAsFunctionRoot(
14982+
ConstraintLocator *locator, UnresolvedDotExpr *root) {
14983+
bool inserted = ImplicitCallAsFunctionRoots.insert({locator, root}).second;
14984+
ASSERT(inserted);
1498114985

14982-
associateArgumentList(
14983-
getConstraintLocator(root, ConstraintLocator::ApplyArgument), arguments);
14986+
if (solverState)
14987+
recordChange(SolverTrail::Change::RecordedImplicitCallAsFunctionRoot(locator));
1498414988
}
1498514989

1498614990
void ConstraintSystem::recordKeyPath(const KeyPathExpr *keypath,

lib/Sema/CSSolver.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ void ConstraintSystem::applySolution(const Solution &solution) {
455455
}
456456

457457
for (auto &implicitRoot : solution.ImplicitCallAsFunctionRoots) {
458-
ImplicitCallAsFunctionRoots.insert(implicitRoot);
458+
if (ImplicitCallAsFunctionRoots.count(implicitRoot.first) == 0)
459+
recordImplicitCallAsFunctionRoot(implicitRoot.first, implicitRoot.second);
459460
}
460461

461462
for (auto &synthesized : solution.SynthesizedConformances) {
@@ -707,7 +708,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
707708

708709
numTypeVariables = cs.TypeVariables.size();
709710
numFixes = cs.Fixes.size();
710-
numImplicitCallAsFunctionRoots = cs.ImplicitCallAsFunctionRoots.size();
711711
numSynthesizedConformances = cs.SynthesizedConformances.size();
712712

713713
PreviousScore = cs.CurrentScore;
@@ -741,10 +741,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
741741
// constraints introduced by the current scope.
742742
cs.solverState->rollback(this);
743743

744-
// Remove any implicitly generated root expressions for `.callAsFunction`
745-
// which are no longer in scope.
746-
truncate(cs.ImplicitCallAsFunctionRoots, numImplicitCallAsFunctionRoots);
747-
748744
// Remove any implicitly synthesized conformances.
749745
truncate(cs.SynthesizedConformances, numSynthesizedConformances);
750746

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4491,7 +4491,7 @@ size_t Solution::getTotalMemory() const {
44914491
size_in_bytes(resultBuilderTransformed) +
44924492
size_in_bytes(appliedPropertyWrappers) +
44934493
size_in_bytes(argumentLists) +
4494-
ImplicitCallAsFunctionRoots.getMemorySize() +
4494+
size_in_bytes(ImplicitCallAsFunctionRoots) +
44954495
size_in_bytes(SynthesizedConformances);
44964496
}
44974497

0 commit comments

Comments
 (0)