Skip to content

Commit 522be5e

Browse files
author
Amritpan Kaur
committed
[CSBindings] Move binding printing out of determineBestBindings and check that bindings exist before printing.
1 parent 861aa04 commit 522be5e

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5537,7 +5537,10 @@ class ConstraintSystem {
55375537
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
55385538
ConstraintKind matchKind, ConstraintLocatorBuilder locator);
55395539

5540-
Optional<BindingSet> determineBestBindings();
5540+
/// Determine whether given type variable with its set of bindings is viable
5541+
/// to be attempted on the next step of the solver.
5542+
Optional<BindingSet> determineBestBindings(
5543+
llvm::function_ref<void(const BindingSet &)> onCandidate);
55415544

55425545
/// Get bindings for the given type variable based on current
55435546
/// state of the constraint system.

lib/Sema/CSBindings.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,8 @@ BindingSet::BindingScore BindingSet::formBindingScore(const BindingSet &b) {
745745
-numNonDefaultableBindings);
746746
}
747747

748-
Optional<BindingSet> ConstraintSystem::determineBestBindings() {
748+
Optional<BindingSet> ConstraintSystem::determineBestBindings(
749+
llvm::function_ref<void(const BindingSet &)> onCandidate) {
749750
// Look for potential type variable bindings.
750751
Optional<BindingSet> bestBindings;
751752
llvm::SmallDenseMap<TypeVariableType *, BindingSet> cache;
@@ -807,9 +808,7 @@ Optional<BindingSet> ConstraintSystem::determineBestBindings() {
807808
if (!bindings || !isViable)
808809
continue;
809810

810-
if (isDebugMode()) {
811-
bindings.dump(llvm::errs(), solverState->getCurrentIndent());
812-
}
811+
onCandidate(bindings);
813812

814813
// If these are the first bindings, or they are better than what
815814
// we saw before, use them instead.

lib/Sema/CSStep.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,30 +346,45 @@ StepResult ComponentStep::take(bool prevFailed) {
346346

347347
/// Try to figure out what this step is going to be,
348348
/// after the scope has been established.
349-
auto bestBindings = CS.determineBestBindings();
349+
std::vector<BindingSet> currentBindings;
350+
auto bestBindings = CS.determineBestBindings([&](const BindingSet &bindings) {
351+
if (bindings.hasViableBindings()) {
352+
currentBindings.push_back(bindings);
353+
}
354+
});
355+
350356
auto *disjunction = CS.selectDisjunction();
351357
auto *conjunction = CS.selectConjunction();
352358

353359
if (CS.isDebugMode()) {
354360
PrintOptions PO;
355361
PO.PrintTypesForDebugging = true;
356362

363+
auto &log = getDebugLogger();
364+
if (!currentBindings.empty()) {
365+
auto heading = currentBindings.size() > 1 ? "(Current Bindings: "
366+
: "(Current Binding: ";
367+
log << heading << '\n';
368+
for (auto const &bindings : currentBindings)
369+
bindings.dump(llvm::errs(), CS.solverState->getCurrentIndent() + 2);
370+
}
371+
log.indent(CS.solverState->getCurrentIndent());
372+
357373
if (disjunction) {
358-
auto &log = getDebugLogger();
359374
log.indent(2);
360375
log << "Disjunction(s) = [";
361376
auto constraints = disjunction->getNestedConstraints();
362377
log << constraints[0]->getFirstType()->getString(PO);
363-
log << "])\n";
378+
log << "]";
364379
}
365380
if (conjunction) {
366-
auto &log = getDebugLogger();
367381
log.indent(2);
368382
log << "Conjunction(s) = [";
369383
auto constraints = conjunction->getNestedConstraints();
370384
log << constraints[0]->getFirstType()->getString(PO);
371-
log << "])\n";
385+
log << "]";
372386
}
387+
log << ")\n";
373388
}
374389

375390
if (CS.shouldAttemptFixes()) {

0 commit comments

Comments
 (0)