Skip to content

Commit c39c4e8

Browse files
committed
[CS] Unify failed constraint recording
Rename `addNewFailingConstraint` to `recordFailedConstraint`, and call into it whenever a constraint fails, instead of setting `failedConstraint`. This ensures that `-debug-constraints` will always log the constraint that failed a given scope. In addition, introduce `retireFailedConstraint` to cover the common case of retiring a constraint that just failed.
1 parent 992f035 commit c39c4e8

File tree

4 files changed

+40
-39
lines changed

4 files changed

+40
-39
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9639,12 +9639,12 @@ ConstraintSystem::addKeyPathApplicationConstraint(Type keypath,
96399639
TMF_GenerateConstraints,
96409640
locator)) {
96419641
case SolutionKind::Error:
9642-
if (shouldAddNewFailingConstraint()) {
9642+
if (shouldRecordFailedConstraint()) {
96439643
auto c = Constraint::create(*this, ConstraintKind::KeyPathApplication,
96449644
keypath, root, value,
96459645
getConstraintLocator(locator));
96469646
if (isFavored) c->setFavored();
9647-
addNewFailingConstraint(c);
9647+
recordFailedConstraint(c);
96489648
}
96499649
return;
96509650

@@ -9668,13 +9668,13 @@ ConstraintSystem::addKeyPathConstraint(
96689668
TMF_GenerateConstraints,
96699669
locator)) {
96709670
case SolutionKind::Error:
9671-
if (shouldAddNewFailingConstraint()) {
9671+
if (shouldRecordFailedConstraint()) {
96729672
auto c = Constraint::create(*this, ConstraintKind::KeyPath,
96739673
keypath, root, value,
96749674
getConstraintLocator(locator),
96759675
componentTypeVars);
96769676
if (isFavored) c->setFavored();
9677-
addNewFailingConstraint(c);
9677+
recordFailedConstraint(c);
96789678
}
96799679
return;
96809680

@@ -9731,11 +9731,11 @@ void ConstraintSystem::addConstraint(ConstraintKind kind, Type first,
97319731
switch (addConstraintImpl(kind, first, second, locator, isFavored)) {
97329732
case SolutionKind::Error:
97339733
// Add a failing constraint, if needed.
9734-
if (shouldAddNewFailingConstraint()) {
9734+
if (shouldRecordFailedConstraint()) {
97359735
auto c = Constraint::create(*this, kind, first, second,
97369736
getConstraintLocator(locator));
97379737
if (isFavored) c->setFavored();
9738-
addNewFailingConstraint(c);
9738+
recordFailedConstraint(c);
97399739
}
97409740
return;
97419741

@@ -9838,11 +9838,11 @@ void ConstraintSystem::addFixConstraint(ConstraintFix *fix, ConstraintKind kind,
98389838
switch (simplifyFixConstraint(fix, first, second, kind, subflags, locator)) {
98399839
case SolutionKind::Error:
98409840
// Add a failing constraint, if needed.
9841-
if (shouldAddNewFailingConstraint()) {
9841+
if (shouldRecordFailedConstraint()) {
98429842
auto c = Constraint::createFixed(*this, kind, fix, first, second,
98439843
getConstraintLocator(locator));
98449844
if (isFavored) c->setFavored();
9845-
addNewFailingConstraint(c);
9845+
recordFailedConstraint(c);
98469846
}
98479847
return;
98489848

@@ -10085,8 +10085,7 @@ void ConstraintSystem::simplifyDisjunctionChoice(Constraint *choice) {
1008510085
// Simplify this term in the disjunction.
1008610086
switch (simplifyConstraint(*choice)) {
1008710087
case ConstraintSystem::SolutionKind::Error:
10088-
if (!failedConstraint)
10089-
failedConstraint = choice;
10088+
recordFailedConstraint(choice);
1009010089
break;
1009110090

1009210091
case ConstraintSystem::SolutionKind::Solved:

lib/Sema/CSSolver.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -290,19 +290,7 @@ bool ConstraintSystem::simplify(bool ContinueAfterFailures) {
290290
// Simplify this constraint.
291291
switch (simplifyConstraint(*constraint)) {
292292
case SolutionKind::Error:
293-
if (!failedConstraint) {
294-
failedConstraint = constraint;
295-
}
296-
297-
if (getASTContext().TypeCheckerOpts.DebugConstraintSolver) {
298-
auto &log = getASTContext().TypeCheckerDebug->getStream();
299-
log.indent(solverState ? solverState->depth * 2 : 0)
300-
<< "(failed constraint ";
301-
constraint->print(log, &getASTContext().SourceMgr);
302-
log << ")\n";
303-
}
304-
305-
retireConstraint(constraint);
293+
retireFailedConstraint(constraint);
306294
break;
307295

308296
case SolutionKind::Solved:

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,11 +1697,8 @@ void ConstraintSystem::addOverloadSet(ArrayRef<Constraint *> choices,
16971697
auto *disjunction =
16981698
Constraint::createDisjunction(*this, choices, locator, ForgetChoice);
16991699
addUnsolvedConstraint(disjunction);
1700-
if (simplifyAppliedOverloads(disjunction, locator)) {
1701-
retireConstraint(disjunction);
1702-
if (!failedConstraint)
1703-
failedConstraint = disjunction;
1704-
}
1700+
if (simplifyAppliedOverloads(disjunction, locator))
1701+
retireFailedConstraint(disjunction);
17051702
}
17061703

17071704
/// If we're resolving an overload set with a decl that has special type

lib/Sema/ConstraintSystem.h

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2838,8 +2838,8 @@ class ConstraintSystem {
28382838
break;
28392839

28402840
case SolutionKind::Error:
2841-
if (shouldAddNewFailingConstraint()) {
2842-
addNewFailingConstraint(Constraint::createMemberOrOuterDisjunction(
2841+
if (shouldRecordFailedConstraint()) {
2842+
recordFailedConstraint(Constraint::createMemberOrOuterDisjunction(
28432843
*this, ConstraintKind::ValueMember, baseTy, memberTy, name, useDC,
28442844
functionRefKind, outerAlternatives, getConstraintLocator(locator)));
28452845
}
@@ -2869,8 +2869,8 @@ class ConstraintSystem {
28692869
break;
28702870

28712871
case SolutionKind::Error:
2872-
if (shouldAddNewFailingConstraint()) {
2873-
addNewFailingConstraint(
2872+
if (shouldRecordFailedConstraint()) {
2873+
recordFailedConstraint(
28742874
Constraint::createMember(*this, ConstraintKind::UnresolvedValueMember,
28752875
baseTy, memberTy, name,
28762876
useDC, functionRefKind,
@@ -2927,17 +2927,34 @@ class ConstraintSystem {
29272927
addUnsolvedConstraint(constraint);
29282928
}
29292929

2930-
/// Whether we should add a new constraint to capture a failure.
2931-
bool shouldAddNewFailingConstraint() const {
2932-
// Only do this at the top level.
2930+
/// Whether we should record the failure of a constraint.
2931+
bool shouldRecordFailedConstraint() const {
2932+
// Only record it if we don't already have a failed constraint. This avoids
2933+
// allocating unnecessary constraints.
29332934
return !failedConstraint;
29342935
}
29352936

2936-
/// Add a new constraint that we know fails.
2937-
void addNewFailingConstraint(Constraint *constraint) {
2938-
assert(shouldAddNewFailingConstraint());
2937+
/// Note that a particular constraint has failed, setting \c failedConstraint
2938+
/// if necessary.
2939+
void recordFailedConstraint(Constraint *constraint) {
29392940
assert(!constraint->isActive());
2940-
failedConstraint = constraint;
2941+
if (!failedConstraint)
2942+
failedConstraint = constraint;
2943+
2944+
if (getASTContext().TypeCheckerOpts.DebugConstraintSolver) {
2945+
auto &log = getASTContext().TypeCheckerDebug->getStream();
2946+
log.indent(solverState ? solverState->depth * 2 : 0)
2947+
<< "(failed constraint ";
2948+
constraint->print(log, &getASTContext().SourceMgr);
2949+
log << ")\n";
2950+
}
2951+
}
2952+
2953+
/// Remove a constraint from the system that has failed, setting
2954+
/// \c failedConstraint if necessary.
2955+
void retireFailedConstraint(Constraint *constraint) {
2956+
retireConstraint(constraint);
2957+
recordFailedConstraint(constraint);
29412958
}
29422959

29432960
/// Add a newly-generated constraint that is known not to be solvable

0 commit comments

Comments
 (0)