Skip to content

Commit 5e6b4a9

Browse files
committed
[Constraint solver] Fix memory corruption issue.
We use simplifyConstraint() to activate other constraints, and then examine those constraints to find related disjunctions. In examining those active constraints, we were simplifying them in case they failed (which would allow us to bail out earlier). In doing so, we could potentially generate new disjunctions when we simplify an unresolved value member constraint. If we do that, we end up collecting these new disjunctions as part of the set of related disjunctions, but that's problematic because as part of exiting the solver scope to roll back changes we delete these disjunctions from the system. Instead of actually simplifying the active constraints, just collect the disjunctions and move the active constraints back to the inactive list. With this change we can build the stdlib.
1 parent 1103e27 commit 5e6b4a9

File tree

2 files changed

+5
-38
lines changed

2 files changed

+5
-38
lines changed

lib/Sema/CSPropagate.cpp

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ getBindOverloadDisjunction(ConstraintSystem &CS, Constraint *applicableFn) {
6969
return found;
7070
}
7171

72-
bool ConstraintSystem::collectNeighboringBindOverloadDisjunctions(
72+
void ConstraintSystem::collectNeighboringBindOverloadDisjunctions(
7373
llvm::SetVector<Constraint *> &neighbors) {
74-
bool failed = false;
7574

7675
while (!ActiveConstraints.empty()) {
7776
auto *constraint = &ActiveConstraints.front();
@@ -87,32 +86,14 @@ bool ConstraintSystem::collectNeighboringBindOverloadDisjunctions(
8786
} else if (constraint->getKind() == ConstraintKind::ApplicableFunction) {
8887
if (auto *bindDisjunction =
8988
getBindOverloadDisjunction(*this, constraint)) {
90-
// FIXME: should we bind this now so that we do something when
91-
// we test the applicable constraint?
9289
neighbors.insert(bindDisjunction);
9390
}
9491
}
9592

96-
// Simplify this constraint.
97-
switch (simplifyConstraint(*constraint)) {
98-
case SolutionKind::Error:
99-
failed = true;
100-
LLVM_FALLTHROUGH;
101-
102-
case SolutionKind::Solved:
103-
solverState->retireConstraint(constraint);
104-
CG.removeConstraint(constraint);
105-
break;
106-
107-
case SolutionKind::Unsolved:
108-
InactiveConstraints.push_back(constraint);
109-
break;
110-
}
111-
93+
solverState->retireConstraint(constraint);
94+
CG.removeConstraint(constraint);
11295
constraint->setActive(false);
11396
}
114-
115-
return !failed;
11697
}
11798

11899
// Simplify any active constraints, returning true on success, false
@@ -218,21 +199,7 @@ bool ConstraintSystem::isBindOverloadConsistent(
218199
solverState->retireConstraint(bindConstraint);
219200
solverState->addGeneratedConstraint(bindConstraint);
220201

221-
auto passed = collectNeighboringBindOverloadDisjunctions(otherDisjunctions);
222-
if (!passed) {
223-
if (TC.getLangOpts().DebugConstraintSolver) {
224-
auto &log = getASTContext().TypeCheckerDebug->getStream();
225-
log << "Disabling bind constraint: ";
226-
bindConstraint->print(log, &TC.Context.SourceMgr);
227-
log << "\n";
228-
}
229-
230-
bindConstraint->setDisabled();
231-
for (auto *disjunction : otherDisjunctions)
232-
workList.insert(disjunction);
233-
234-
return false;
235-
}
202+
collectNeighboringBindOverloadDisjunctions(otherDisjunctions);
236203
}
237204

238205
// Test the our primary constraint against all of the members of

lib/Sema/ConstraintSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@ class ConstraintSystem {
23622362
void shrink(Expr *expr);
23632363

23642364
bool simplifyForConstraintPropagation();
2365-
bool collectNeighboringBindOverloadDisjunctions(
2365+
void collectNeighboringBindOverloadDisjunctions(
23662366
llvm::SetVector<Constraint *> &neighbors);
23672367
bool isBindOverloadConsistent(Constraint *bindConstraint,
23682368
llvm::SetVector<Constraint *> &workList);

0 commit comments

Comments
 (0)