Skip to content

Commit e779790

Browse files
authored
Merge pull request #39861 from xedin/conjunction-reset-score-only-on-failure
[CSStep] Conjunction: Reset current/best score only on failure
2 parents 920bcc2 + c25952f commit e779790

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

lib/Sema/CSStep.cpp

Lines changed: 13 additions & 7 deletions
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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -907,9 +907,10 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
907907
// Restore conjunction constraint.
908908
restore(AfterConjunction, Conjunction);
909909

910-
// Restore best score.
911-
CS.solverState->BestScore = BestScore;
912-
CS.CurrentScore = CurrentScore;
910+
// Restore best score only if conjunction fails because
911+
// successful outcome should keep a score set by `restoreOuterState`.
912+
if (HadFailure)
913+
restoreOriginalScores();
913914
}
914915

915916
StepResult resume(bool prevFailed) override;
@@ -954,6 +955,12 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
954955
}
955956

956957
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+
957964
// Restore constraint system state before conjunction.
958965
//
959966
// Note that this doesn't include conjunction constraint

0 commit comments

Comments
 (0)