Skip to content

Commit 102bc6a

Browse files
authored
[CS] A couple of minor -debug-constraints improvements (#31180)
[CS] A couple of minor `-debug-constraints` improvements
2 parents f7dd406 + 87577d2 commit 102bc6a

File tree

6 files changed

+58
-47
lines changed

6 files changed

+58
-47
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8049,7 +8049,7 @@ bool ConstraintSystem::simplifyAppliedOverloadsImpl(
80498049
ASTContext &ctx = getASTContext();
80508050
if (ctx.TypeCheckerOpts.DebugConstraintSolver) {
80518051
auto &log = ctx.TypeCheckerDebug->getStream();
8052-
log.indent(solverState ? solverState->depth * 2 + 2 : 0)
8052+
log.indent(solverState ? solverState->depth * 2 : 0)
80538053
<< "(common result type for $T" << fnTypeVar->getID() << " is "
80548054
<< commonResultType.getString()
80558055
<< ")\n";
@@ -9191,7 +9191,7 @@ bool ConstraintSystem::recordFix(ConstraintFix *fix, unsigned impact) {
91919191
auto &ctx = getASTContext();
91929192
if (ctx.TypeCheckerOpts.DebugConstraintSolver) {
91939193
auto &log = ctx.TypeCheckerDebug->getStream();
9194-
log.indent(solverState ? solverState->depth * 2 + 2 : 0)
9194+
log.indent(solverState ? solverState->depth * 2 : 0)
91959195
<< "(attempting fix ";
91969196
fix->print(log);
91979197
log << ")\n";
@@ -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: 3 additions & 15 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:
@@ -1424,7 +1412,7 @@ ConstraintSystem::filterDisjunction(
14241412

14251413
if (ctx.TypeCheckerOpts.DebugConstraintSolver) {
14261414
auto &log = ctx.TypeCheckerDebug->getStream();
1427-
log.indent(solverState ? solverState->depth * 2 + 2 : 0)
1415+
log.indent(solverState ? solverState->depth * 2 : 0)
14281416
<< "(disabled disjunction term ";
14291417
constraint->print(log, &ctx.SourceMgr);
14301418
log << ")\n";
@@ -1485,7 +1473,7 @@ ConstraintSystem::filterDisjunction(
14851473

14861474
if (ctx.TypeCheckerOpts.DebugConstraintSolver) {
14871475
auto &log = ctx.TypeCheckerDebug->getStream();
1488-
log.indent(solverState ? solverState->depth * 2 + 2 : 0)
1476+
log.indent(solverState ? solverState->depth * 2 : 0)
14891477
<< "(introducing single enabled disjunction term ";
14901478
choice->print(log, &ctx.SourceMgr);
14911479
log << ")\n";

lib/Sema/CSStep.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ StepResult DependentComponentSplitterStep::take(bool prevFailed) {
260260
// "split" is considered a failure if previous step failed,
261261
// or there is a failure recorded by constraint system, or
262262
// system can't be simplified.
263-
if (prevFailed || CS.failedConstraint || CS.simplify())
263+
if (prevFailed || CS.getFailedConstraint() || CS.simplify())
264264
return done(/*isSuccess=*/false);
265265

266266
// Figure out the sets of partial solutions that this component depends on.

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: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,8 +1613,6 @@ class ConstraintSystem {
16131613

16141614
class SolverScope;
16151615

1616-
Constraint *failedConstraint = nullptr;
1617-
16181616
/// Expressions that are known to be unevaluated.
16191617
/// Note: this is only used to support ObjCSelectorExpr at the moment.
16201618
llvm::SmallPtrSet<Expr *, 2> UnevaluatedRootExprs;
@@ -1623,6 +1621,10 @@ class ConstraintSystem {
16231621
unsigned CountDisjunctions = 0;
16241622

16251623
private:
1624+
/// A constraint that has failed along the current solver path.
1625+
/// Do not set directly, call \c recordFailedConstraint instead.
1626+
Constraint *failedConstraint = nullptr;
1627+
16261628
/// Current phase of the constraint system lifetime.
16271629
ConstraintSystemPhase Phase = ConstraintSystemPhase::ConstraintGeneration;
16281630

@@ -2098,6 +2100,10 @@ class ConstraintSystem {
20982100
};
20992101

21002102
public:
2103+
/// Retrieve the first constraint that has failed along the solver's path, or
2104+
/// \c nullptr if no constraint has failed.
2105+
Constraint *getFailedConstraint() const { return failedConstraint; }
2106+
21012107
ConstraintSystemPhase getPhase() const { return Phase; }
21022108

21032109
/// Move constraint system to a new phase of its lifetime.
@@ -2838,8 +2844,8 @@ class ConstraintSystem {
28382844
break;
28392845

28402846
case SolutionKind::Error:
2841-
if (shouldAddNewFailingConstraint()) {
2842-
addNewFailingConstraint(Constraint::createMemberOrOuterDisjunction(
2847+
if (shouldRecordFailedConstraint()) {
2848+
recordFailedConstraint(Constraint::createMemberOrOuterDisjunction(
28432849
*this, ConstraintKind::ValueMember, baseTy, memberTy, name, useDC,
28442850
functionRefKind, outerAlternatives, getConstraintLocator(locator)));
28452851
}
@@ -2869,8 +2875,8 @@ class ConstraintSystem {
28692875
break;
28702876

28712877
case SolutionKind::Error:
2872-
if (shouldAddNewFailingConstraint()) {
2873-
addNewFailingConstraint(
2878+
if (shouldRecordFailedConstraint()) {
2879+
recordFailedConstraint(
28742880
Constraint::createMember(*this, ConstraintKind::UnresolvedValueMember,
28752881
baseTy, memberTy, name,
28762882
useDC, functionRefKind,
@@ -2927,17 +2933,38 @@ class ConstraintSystem {
29272933
addUnsolvedConstraint(constraint);
29282934
}
29292935

2930-
/// Whether we should add a new constraint to capture a failure.
2931-
bool shouldAddNewFailingConstraint() const {
2932-
// Only do this at the top level.
2936+
/// Whether we should record the failure of a constraint.
2937+
bool shouldRecordFailedConstraint() const {
2938+
// If we're debugging, always note a failure so we can print it out.
2939+
if (getASTContext().TypeCheckerOpts.DebugConstraintSolver)
2940+
return true;
2941+
2942+
// Otherwise, only record it if we don't already have a failed constraint.
2943+
// This avoids allocating unnecessary constraints.
29332944
return !failedConstraint;
29342945
}
29352946

2936-
/// Add a new constraint that we know fails.
2937-
void addNewFailingConstraint(Constraint *constraint) {
2938-
assert(shouldAddNewFailingConstraint());
2947+
/// Note that a particular constraint has failed, setting \c failedConstraint
2948+
/// if necessary.
2949+
void recordFailedConstraint(Constraint *constraint) {
29392950
assert(!constraint->isActive());
2940-
failedConstraint = constraint;
2951+
if (!failedConstraint)
2952+
failedConstraint = constraint;
2953+
2954+
if (getASTContext().TypeCheckerOpts.DebugConstraintSolver) {
2955+
auto &log = getASTContext().TypeCheckerDebug->getStream();
2956+
log.indent(solverState ? solverState->depth * 2 : 0)
2957+
<< "(failed constraint ";
2958+
constraint->print(log, &getASTContext().SourceMgr);
2959+
log << ")\n";
2960+
}
2961+
}
2962+
2963+
/// Remove a constraint from the system that has failed, setting
2964+
/// \c failedConstraint if necessary.
2965+
void retireFailedConstraint(Constraint *constraint) {
2966+
retireConstraint(constraint);
2967+
recordFailedConstraint(constraint);
29412968
}
29422969

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

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3122,7 +3122,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
31223122
out << "\n";
31233123
}
31243124

3125-
if (solverState && !solverState->hasRetiredConstraints()) {
3125+
if (solverState && solverState->hasRetiredConstraints()) {
31263126
out << "\nRetired Constraints:\n";
31273127
solverState->forEachRetired([&](Constraint &constraint) {
31283128
out.indent(2);

0 commit comments

Comments
 (0)