Skip to content

Commit 43a4ac9

Browse files
committed
Sema: Record resolved overloads in the trail
1 parent 892e79c commit 43a4ac9

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ LOCATOR_CHANGE(RecordedOpenedTypes)
3232
LOCATOR_CHANGE(RecordedOpenedExistentialType)
3333
LOCATOR_CHANGE(RecordedPackExpansionEnvironment)
3434
LOCATOR_CHANGE(RecordedDefaultedConstraint)
35+
LOCATOR_CHANGE(ResolvedOverload)
3536

3637
CHANGE(AddedTypeVariable)
3738
CHANGE(AddedConstraint)

include/swift/Sema/ConstraintSystem.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,7 +2207,7 @@ class ConstraintSystem {
22072207
llvm::FoldingSetVector<ConstraintLocator> ConstraintLocators;
22082208

22092209
/// The overload sets that have been resolved along the current path.
2210-
llvm::MapVector<ConstraintLocator *, SelectedOverload> ResolvedOverloads;
2210+
llvm::DenseMap<ConstraintLocator *, SelectedOverload> ResolvedOverloads;
22112211

22122212
/// The current fixed score for this constraint system and the (partial)
22132213
/// solution it represents.
@@ -2857,9 +2857,6 @@ class ConstraintSystem {
28572857
/// FIXME: Remove this.
28582858
unsigned numFixes;
28592859

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

@@ -4932,6 +4929,11 @@ class ConstraintSystem {
49324929
buildDisjunctionForOptionalVsUnderlying(boundTy, type, dynamicLocator);
49334930
}
49344931

4932+
void recordResolvedOverload(ConstraintLocator *locator,
4933+
SelectedOverload choice);
4934+
4935+
void removeResolvedOverload(ConstraintLocator *locator);
4936+
49354937
/// Resolve the given overload set to the given choice.
49364938
void resolveOverload(ConstraintLocator *locator, Type boundType,
49374939
OverloadChoice choice, DeclContext *useDC);
@@ -5689,7 +5691,7 @@ class ConstraintSystem {
56895691
}
56905692

56915693
/// The overload sets that have already been resolved along the current path.
5692-
const llvm::MapVector<ConstraintLocator *, SelectedOverload> &
5694+
const llvm::DenseMap<ConstraintLocator *, SelectedOverload> &
56935695
getResolvedOverloads() const {
56945696
return ResolvedOverloads;
56955697
}

lib/Sema/CSSolver.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,10 @@ void ConstraintSystem::applySolution(const Solution &solution) {
290290

291291
// Register overload choices.
292292
// FIXME: Copy these directly into some kind of partial solution?
293-
for (auto overload : solution.overloadChoices)
294-
ResolvedOverloads.insert(overload);
293+
for (auto overload : solution.overloadChoices) {
294+
if (!ResolvedOverloads.count(overload.first))
295+
recordResolvedOverload(overload.first, overload.second);
296+
}
295297

296298
// Register constraint restrictions.
297299
// FIXME: Copy these directly into some kind of partial solution?
@@ -688,7 +690,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
688690
numTypeVariables = cs.TypeVariables.size();
689691
numFixes = cs.Fixes.size();
690692
numKeyPaths = cs.KeyPaths.size();
691-
numResolvedOverloads = cs.ResolvedOverloads.size();
692693
numInferredClosureTypes = cs.ClosureTypes.size();
693694
numImpliedResults = cs.ImpliedResults.size();
694695
numContextualTypes = cs.contextualTypes.size();
@@ -717,8 +718,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
717718
// Erase the end of various lists.
718719
truncate(cs.TypeVariables, numTypeVariables);
719720

720-
truncate(cs.ResolvedOverloads, numResolvedOverloads);
721-
722721
// Move any remaining active constraints into the inactive list.
723722
if (!cs.ActiveConstraints.empty()) {
724723
for (auto &constraint : cs.ActiveConstraints) {

lib/Sema/CSTrail.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
360360
case ChangeKind::AppliedPropertyWrapper:
361361
cs.removePropertyWrapper(TheExpr);
362362
break;
363+
364+
case ChangeKind::ResolvedOverload:
365+
cs.removeResolvedOverload(Locator);
366+
break;
363367
}
364368
}
365369

lib/Sema/ConstraintSystem.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3735,6 +3735,20 @@ void ConstraintSystem::bindOverloadType(
37353735
llvm_unreachable("Unhandled OverloadChoiceKind in switch.");
37363736
}
37373737

3738+
void ConstraintSystem::recordResolvedOverload(ConstraintLocator *locator,
3739+
SelectedOverload overload) {
3740+
bool inserted = ResolvedOverloads.insert({locator, overload}).second;
3741+
ASSERT(inserted);
3742+
3743+
if (solverState)
3744+
recordChange(SolverTrail::Change::ResolvedOverload(locator));
3745+
}
3746+
3747+
void ConstraintSystem::removeResolvedOverload(ConstraintLocator *locator) {
3748+
bool erased = ResolvedOverloads.erase(locator);
3749+
ASSERT(erased);
3750+
}
3751+
37383752
void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
37393753
Type boundType,
37403754
OverloadChoice choice,
@@ -3990,9 +4004,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
39904004
auto overload = SelectedOverload{
39914005
choice, openedType, adjustedOpenedType, refType, adjustedRefType,
39924006
boundType};
3993-
auto result = ResolvedOverloads.insert({locator, overload});
3994-
assert(result.second && "Already resolved this overload?");
3995-
(void)result;
4007+
recordResolvedOverload(locator, overload);
39964008

39974009
// Add the constraints necessary to bind the overload type.
39984010
bindOverloadType(overload, boundType, locator, useDC,

0 commit comments

Comments
 (0)