@@ -422,6 +422,11 @@ ConstraintSystem::SolverState::~SolverState() {
422422 " Expected constraint system to have this solver state!" );
423423 CS .solverState = nullptr ;
424424
425+ // If constraint system ended up being in an invalid state
426+ // let's just drop the state without attempting to rollback.
427+ if (CS .inInvalidState ())
428+ return ;
429+
425430 // Make sure that all of the retired constraints have been returned
426431 // to constraint system.
427432 assert (!hasRetiredConstraints ());
@@ -513,6 +518,10 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
513518}
514519
515520ConstraintSystem::SolverScope::~SolverScope () {
521+ // Don't attempt to rollback from an incorrect state.
522+ if (cs.inInvalidState ())
523+ return ;
524+
516525 // Erase the end of various lists.
517526 while (cs.TypeVariables .size () > numTypeVariables)
518527 cs.TypeVariables .pop_back ();
@@ -1384,6 +1393,13 @@ void ConstraintSystem::solveImpl(SmallVectorImpl<Solution> &solutions) {
13841393 if (failedConstraint)
13851394 return ;
13861395
1396+ // Attempt to solve a constraint system already in an invalid
1397+ // state should be immediately aborted.
1398+ if (inInvalidState ()) {
1399+ solutions.clear ();
1400+ return ;
1401+ }
1402+
13871403 // Allocate new solver scope, so constraint system
13881404 // could be restored to its original state afterwards.
13891405 // Otherwise there is a risk that some of the constraints
@@ -1426,6 +1442,14 @@ void ConstraintSystem::solveImpl(SmallVectorImpl<Solution> &solutions) {
14261442 // or error, which means that current path is inconsistent.
14271443 {
14281444 auto result = advance (step.get (), prevFailed);
1445+
1446+ // If execution of this step let constraint system in an
1447+ // invalid state, let's drop all of the solutions and abort.
1448+ if (inInvalidState ()) {
1449+ solutions.clear ();
1450+ return ;
1451+ }
1452+
14291453 switch (result.getKind ()) {
14301454 // It was impossible to solve this step, let's note that
14311455 // for followup steps, to propogate the error.
0 commit comments