Skip to content

Commit d8cdbcd

Browse files
committed
[ConstraintSystem] Add InvalidState flag to indicate that any attempt to solve should fail
As soon as `InvalidState` flag is set solving of the constraint system as aborted and all subsequent calls to `solveImpl` would produce no solutions.
1 parent 04faa2a commit d8cdbcd

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,11 @@ class ConstraintSystem {
21412141
/// explored.
21422142
size_t MaxMemory = 0;
21432143

2144+
/// Flag to indicate to the solver that the system is in invalid
2145+
/// state and it shouldn't proceed but instead produce a fallback
2146+
/// diagnostic.
2147+
bool InvalidState = false;
2148+
21442149
/// Cached member lookups.
21452150
llvm::DenseMap<std::pair<Type, DeclNameRef>, Optional<LookupResult>>
21462151
MemberLookups;
@@ -2640,6 +2645,11 @@ class ConstraintSystem {
26402645
Phase = newPhase;
26412646
}
26422647

2648+
/// Check whether constraint system is in valid state e.g.
2649+
/// has left-over active/inactive constraints which should
2650+
/// have been simplified.
2651+
bool inInvalidState() const { return InvalidState; }
2652+
26432653
/// Cache the types of the given expression and all subexpressions.
26442654
void cacheExprTypes(Expr *expr) {
26452655
bool excludeRoot = false;

lib/Sema/CSSolver.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,13 @@ void ConstraintSystem::solveImpl(SmallVectorImpl<Solution> &solutions) {
13841384
if (failedConstraint)
13851385
return;
13861386

1387+
// Attempt to solve a constraint system already in an invalid
1388+
// state should be immediately aborted.
1389+
if (inInvalidState()) {
1390+
solutions.clear();
1391+
return;
1392+
}
1393+
13871394
// Allocate new solver scope, so constraint system
13881395
// could be restored to its original state afterwards.
13891396
// Otherwise there is a risk that some of the constraints
@@ -1426,6 +1433,14 @@ void ConstraintSystem::solveImpl(SmallVectorImpl<Solution> &solutions) {
14261433
// or error, which means that current path is inconsistent.
14271434
{
14281435
auto result = advance(step.get(), prevFailed);
1436+
1437+
// If execution of this step let constraint system in an
1438+
// invalid state, let's drop all of the solutions and abort.
1439+
if (inInvalidState()) {
1440+
solutions.clear();
1441+
return;
1442+
}
1443+
14291444
switch (result.getKind()) {
14301445
// It was impossible to solve this step, let's note that
14311446
// for followup steps, to propogate the error.

lib/Sema/CSStep.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ StepResult ComponentStep::take(bool prevFailed) {
384384
getDebugLogger() << ")\n";
385385
}
386386

387+
CS.InvalidState = true;
387388
return finalize(/*isSuccess=*/false);
388389
#endif
389390
}
@@ -400,6 +401,7 @@ StepResult ComponentStep::take(bool prevFailed) {
400401
getDebugLogger() << ")\n";
401402
}
402403

404+
CS.InvalidState = true;
403405
return finalize(/*isSuccess=*/false);
404406
#endif
405407
}

0 commit comments

Comments
 (0)