Skip to content

Commit 685d4b8

Browse files
author
Amritpan Kaur
committed
[CSBindings] Move binding printing out of determineBestBindings and check that bindings exist before printing.
1 parent d750d38 commit 685d4b8

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
@@ -5557,7 +5557,10 @@ class ConstraintSystem {
55575557
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
55585558
ConstraintKind matchKind, ConstraintLocatorBuilder locator);
55595559

5560-
Optional<BindingSet> determineBestBindings();
5560+
/// Determine whether given type variable with its set of bindings is viable
5561+
/// to be attempted on the next step of the solver.
5562+
Optional<BindingSet> determineBestBindings(
5563+
llvm::function_ref<void(const BindingSet &)> onCandidate);
55615564

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

lib/Sema/CSBindings.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,8 @@ BindingSet::BindingScore BindingSet::formBindingScore(const BindingSet &b) {
742742
-numNonDefaultableBindings);
743743
}
744744

745-
Optional<BindingSet> ConstraintSystem::determineBestBindings() {
745+
Optional<BindingSet> ConstraintSystem::determineBestBindings(
746+
llvm::function_ref<void(const BindingSet &)> onCandidate) {
746747
// Look for potential type variable bindings.
747748
Optional<BindingSet> bestBindings;
748749
llvm::SmallDenseMap<TypeVariableType *, BindingSet> cache;
@@ -804,9 +805,7 @@ Optional<BindingSet> ConstraintSystem::determineBestBindings() {
804805
if (!bindings || !isViable)
805806
continue;
806807

807-
if (isDebugMode()) {
808-
bindings.dump(llvm::errs(), solverState->getCurrentIndent());
809-
}
808+
onCandidate(bindings);
810809

811810
// If these are the first bindings, or they are better than what
812811
// 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 ? "(Potential Bindings: "
366+
: "(Potential Binding: ";
367+
log << heading << '\n';
368+
for (auto const &bindings : currentBindings)
369+
bindings.dump(log, 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)