Skip to content

Commit 2ee5b30

Browse files
committed
[CSStep] Conjunction: Extract constraint system state restoration into a method
In preparation to handle ambiguities in the elements, it's useful to extract the logic dealing with constraint system state restoration into a separate logical entity.
1 parent d8ebc55 commit 2ee5b30

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

Diff for: lib/Sema/CSStep.cpp

+27-23
Original file line numberDiff line numberDiff line change
@@ -885,34 +885,17 @@ StepResult ConjunctionStep::resume(bool prevFailed) {
885885
log << "(applying conjunction result to outer context\n";
886886
}
887887

888-
// Restore constraint system state before conjunction.
889-
//
890-
// Note that this doesn't include conjunction constraint
891-
// itself because we don't want to re-solve it at this
892-
// point.
893888
assert(
894889
Snapshot &&
895890
"Isolated conjunction requires a snapshot of the constraint system");
896-
Snapshot->setupOuterContext(Solutions.pop_back_val());
897891

898-
// Restore best score, since upcoming step is going to
899-
// work with outer scope in relation to the conjunction.
900-
CS.solverState->BestScore = BestScore;
901-
902-
// Active all of the previously out-of-scope constraints
903-
// because conjunction can propagate type information up
904-
// by allowing its elements to reference type variables
905-
// from outer scope (e.g. variable declarations and or captures).
906-
{
907-
CS.ActiveConstraints.splice(CS.ActiveConstraints.end(),
908-
CS.InactiveConstraints);
909-
for (auto &constraint : CS.ActiveConstraints)
910-
constraint.setActive(true);
911-
}
892+
// Restore outer type variables and prepare to solve
893+
// constraints associated with outer context together
894+
// with information deduced from the conjunction.
895+
Snapshot->setupOuterContext(Solutions.pop_back_val());
912896

913-
// Restore score to the one before conjunction. This has
914-
// be done after solution, reached for the body, is applied.
915-
CS.CurrentScore = CurrentScore;
897+
// Pretend that conjunction never happend.
898+
restoreOuterState();
916899

917900
// Now that all of the information from the conjunction has
918901
// been applied, let's attempt to solve the outer scope.
@@ -923,3 +906,24 @@ StepResult ConjunctionStep::resume(bool prevFailed) {
923906
// Attempt next conjunction choice.
924907
return take(prevFailed);
925908
}
909+
910+
void ConjunctionStep::restoreOuterState() const {
911+
// Restore best score, since upcoming step is going to
912+
// work with outer scope in relation to the conjunction.
913+
CS.solverState->BestScore = BestScore;
914+
915+
// Active all of the previously out-of-scope constraints
916+
// because conjunction can propagate type information up
917+
// by allowing its elements to reference type variables
918+
// from outer scope (e.g. variable declarations and or captures).
919+
{
920+
CS.ActiveConstraints.splice(CS.ActiveConstraints.end(),
921+
CS.InactiveConstraints);
922+
for (auto &constraint : CS.ActiveConstraints)
923+
constraint.setActive(true);
924+
}
925+
926+
// Restore score to the one before conjunction. This has
927+
// be done after solution, reached for the body, is applied.
928+
CS.CurrentScore = CurrentScore;
929+
}

Diff for: lib/Sema/CSStep.h

+7
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,13 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
949949
// successfully by use of fixes, and ignore the rest.
950950
AnySolved = false;
951951
}
952+
953+
private:
954+
// Restore constraint system state before conjunction.
955+
//
956+
// Note that this doesn't include conjunction constraint
957+
// itself because we don't want to re-solve it.
958+
void restoreOuterState() const;
952959
};
953960

954961
} // end namespace constraints

0 commit comments

Comments
 (0)