Skip to content

Commit 4a82d38

Browse files
committed
Sema: Record synthesized conformances in the trail
1 parent 12eb7ce commit 4a82d38

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ LOCATOR_CHANGE(ResolvedOverload, ResolvedOverloads)
4545
LOCATOR_CHANGE(RecordedImplicitValueConversion, ImplicitValueConversions)
4646
LOCATOR_CHANGE(RecordedArgumentList, ArgumentLists)
4747
LOCATOR_CHANGE(RecordedImplicitCallAsFunctionRoot, ImplicitCallAsFunctionRoots)
48+
LOCATOR_CHANGE(RecordedSynthesizedConformance, SynthesizedConformances)
4849

4950
EXPR_CHANGE(AppliedPropertyWrapper)
5051
EXPR_CHANGE(RecordedImpliedResult)

include/swift/Sema/ConstraintSystem.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ class Solution {
16181618

16191619
/// The set of conformances synthesized during solving (i.e. for
16201620
/// ad-hoc distributed `SerializationRequirement` conformances).
1621-
llvm::MapVector<ConstraintLocator *, ProtocolConformanceRef>
1621+
llvm::DenseMap<ConstraintLocator *, ProtocolConformanceRef>
16221622
SynthesizedConformances;
16231623

16241624
/// Record a new argument matching choice for given locator that maps a
@@ -2428,7 +2428,7 @@ class ConstraintSystem {
24282428

24292429
/// The set of conformances synthesized during solving (i.e. for
24302430
/// ad-hoc distributed `SerializationRequirement` conformances).
2431-
llvm::MapVector<ConstraintLocator *, ProtocolConformanceRef>
2431+
llvm::DenseMap<ConstraintLocator *, ProtocolConformanceRef>
24322432
SynthesizedConformances;
24332433

24342434
private:
@@ -2855,9 +2855,6 @@ class ConstraintSystem {
28552855
/// FIXME: Remove this.
28562856
unsigned numFixes;
28572857

2858-
/// The length of \c SynthesizedConformances.
2859-
unsigned numSynthesizedConformances;
2860-
28612858
/// The previous score.
28622859
Score PreviousScore;
28632860

@@ -5064,6 +5061,9 @@ class ConstraintSystem {
50645061
ConstraintLocatorBuilder locator,
50655062
TypeMatchOptions flags);
50665063

5064+
void recordSynthesizedConformance(ConstraintLocator *locator,
5065+
ProtocolConformanceRef conformance);
5066+
50675067
/// Attempt to simplify the given conformance constraint.
50685068
///
50695069
/// \param type The type being tested.

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8329,6 +8329,16 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
83298329
return matchExistentialTypes(type, protocol, kind, flags, locator);
83308330
}
83318331

8332+
void ConstraintSystem::recordSynthesizedConformance(
8333+
ConstraintLocator *locator,
8334+
ProtocolConformanceRef conformance) {
8335+
bool inserted = SynthesizedConformances.insert({locator, conformance}).second;
8336+
ASSERT(inserted);
8337+
8338+
if (solverState)
8339+
recordChange(SolverTrail::Change::RecordedSynthesizedConformance(locator));
8340+
}
8341+
83328342
ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
83338343
Type type,
83348344
ProtocolDecl *protocol,
@@ -8487,7 +8497,9 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
84878497
ProtocolConformanceRef synthesized(protocol);
84888498
auto witnessLoc = getConstraintLocator(
84898499
locator.getAnchor(), LocatorPathElt::Witness(witness));
8490-
SynthesizedConformances.insert({witnessLoc, synthesized});
8500+
// FIXME: Why are we recording the same locator more than once here?
8501+
if (SynthesizedConformances.count(witnessLoc) == 0)
8502+
recordSynthesizedConformance(witnessLoc, synthesized);
84918503
return recordConformance(synthesized);
84928504
};
84938505

lib/Sema/CSSolver.cpp

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

462462
for (auto &synthesized : solution.SynthesizedConformances) {
463-
SynthesizedConformances.insert(synthesized);
463+
if (SynthesizedConformances.count(synthesized.first) == 0)
464+
recordSynthesizedConformance(synthesized.first, synthesized.second);
464465
}
465466

466467
// Register any fixes produced along this path.
@@ -708,7 +709,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
708709

709710
numTypeVariables = cs.TypeVariables.size();
710711
numFixes = cs.Fixes.size();
711-
numSynthesizedConformances = cs.SynthesizedConformances.size();
712712

713713
PreviousScore = cs.CurrentScore;
714714

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

744-
// Remove any implicitly synthesized conformances.
745-
truncate(cs.SynthesizedConformances, numSynthesizedConformances);
746-
747744
// Reset the previous score.
748745
cs.CurrentScore = PreviousScore;
749746

0 commit comments

Comments
 (0)