@@ -414,6 +414,11 @@ ConstraintSystem::SolverState::~SolverState() {
414414 " Expected constraint system to have this solver state!" );
415415 CS.solverState = nullptr ;
416416
417+ // If constraint system ended up being in an invalid state
418+ // let's just drop the state without attempting to rollback.
419+ if (CS.inInvalidState ())
420+ return ;
421+
417422 // Make sure that all of the retired constraints have been returned
418423 // to constraint system.
419424 assert (!hasRetiredConstraints ());
@@ -504,6 +509,10 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
504509}
505510
506511ConstraintSystem::SolverScope::~SolverScope () {
512+ // Don't attempt to rollback from an incorrect state.
513+ if (cs.inInvalidState ())
514+ return ;
515+
507516 // Erase the end of various lists.
508517 while (cs.TypeVariables .size () > numTypeVariables)
509518 cs.TypeVariables .pop_back ();
@@ -1362,6 +1371,13 @@ void ConstraintSystem::solveImpl(SmallVectorImpl<Solution> &solutions) {
13621371 if (failedConstraint)
13631372 return ;
13641373
1374+ // Attempt to solve a constraint system already in an invalid
1375+ // state should be immediately aborted.
1376+ if (inInvalidState ()) {
1377+ solutions.clear ();
1378+ return ;
1379+ }
1380+
13651381 // Allocate new solver scope, so constraint system
13661382 // could be restored to its original state afterwards.
13671383 // Otherwise there is a risk that some of the constraints
@@ -1404,6 +1420,14 @@ void ConstraintSystem::solveImpl(SmallVectorImpl<Solution> &solutions) {
14041420 // or error, which means that current path is inconsistent.
14051421 {
14061422 auto result = advance (step.get (), prevFailed);
1423+
1424+ // If execution of this step let constraint system in an
1425+ // invalid state, let's drop all of the solutions and abort.
1426+ if (inInvalidState ()) {
1427+ solutions.clear ();
1428+ return ;
1429+ }
1430+
14071431 switch (result.getKind ()) {
14081432 // It was impossible to solve this step, let's note that
14091433 // for followup steps, to propogate the error.
0 commit comments