Skip to content

Commit c25952f

Browse files
committed
[CSStep] Conjunction: Drop element scores in ambiguity cases
1 parent 2b875c5 commit c25952f

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

lib/Sema/CSStep.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,10 @@ StepResult ConjunctionStep::resume(bool prevFailed) {
916916
// Restore all outer type variables, constraints
917917
// and scoring information.
918918
Snapshot.reset();
919-
restoreOuterState();
919+
920+
// Restore original scores of outer context before
921+
// trying to produce a combined solution.
922+
restoreOriginalScores();
920923

921924
// Apply all of the information deduced from the
922925
// conjunction (up to the point of ambiguity)
@@ -925,6 +928,13 @@ StepResult ConjunctionStep::resume(bool prevFailed) {
925928
ConstraintSystem::SolverScope scope(CS);
926929

927930
CS.applySolution(solution);
931+
932+
// `applySolution` changes best/current scores
933+
// of the constraint system, so they have to be
934+
// restored right afterwards because score of the
935+
// element does contribute to the overall score.
936+
restoreOriginalScores();
937+
928938
// Note that `worseThanBestSolution` isn't checked
929939
// here because `Solutions` were pre-filtered, and
930940
// outer score is the same for all of them.
@@ -953,9 +963,9 @@ StepResult ConjunctionStep::resume(bool prevFailed) {
953963
}
954964

955965
void ConjunctionStep::restoreOuterState() const {
956-
// Restore best score, since upcoming step is going to
966+
// Restore best/current score, since upcoming step is going to
957967
// work with outer scope in relation to the conjunction.
958-
CS.solverState->BestScore = BestScore;
968+
restoreOriginalScores();
959969

960970
// Active all of the previously out-of-scope constraints
961971
// because conjunction can propagate type information up
@@ -967,8 +977,4 @@ void ConjunctionStep::restoreOuterState() const {
967977
for (auto &constraint : CS.ActiveConstraints)
968978
constraint.setActive(true);
969979
}
970-
971-
// Restore score to the one before conjunction. This has
972-
// be done after solution, reached for the body, is applied.
973-
CS.CurrentScore = CurrentScore;
974980
}

lib/Sema/CSStep.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -909,10 +909,8 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
909909

910910
// Restore best score only if conjunction fails because
911911
// successful outcome should keep a score set by `restoreOuterState`.
912-
if (HadFailure) {
913-
CS.solverState->BestScore = BestScore;
914-
CS.CurrentScore = CurrentScore;
915-
}
912+
if (HadFailure)
913+
restoreOriginalScores();
916914
}
917915

918916
StepResult resume(bool prevFailed) override;
@@ -957,6 +955,12 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
957955
}
958956

959957
private:
958+
/// Restore best and current scores as they were before conjunction.
959+
void restoreOriginalScores() const {
960+
CS.solverState->BestScore = BestScore;
961+
CS.CurrentScore = CurrentScore;
962+
}
963+
960964
// Restore constraint system state before conjunction.
961965
//
962966
// Note that this doesn't include conjunction constraint

0 commit comments

Comments
 (0)