Skip to content

Commit 51281c5

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

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-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: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,30 +346,44 @@ 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+
SmallString<64> potentialBindings;
350+
llvm::raw_svector_ostream bos(potentialBindings);
351+
352+
auto bestBindings = CS.determineBestBindings([&](const BindingSet &bindings) {
353+
if (CS.isDebugMode() && bindings.hasViableBindings()) {
354+
bindings.dump(bos, CS.solverState->getCurrentIndent() + 2);
355+
}
356+
});
357+
350358
auto *disjunction = CS.selectDisjunction();
351359
auto *conjunction = CS.selectConjunction();
352360

353361
if (CS.isDebugMode()) {
354362
PrintOptions PO;
355363
PO.PrintTypesForDebugging = true;
356364

365+
auto &log = getDebugLogger();
366+
if (!potentialBindings.empty()) {
367+
log << "(Potential Binding(s): " << '\n';
368+
log << potentialBindings;
369+
}
370+
log.indent(CS.solverState->getCurrentIndent());
371+
357372
if (disjunction) {
358-
auto &log = getDebugLogger();
359373
log.indent(2);
360374
log << "Disjunction(s) = [";
361375
auto constraints = disjunction->getNestedConstraints();
362376
log << constraints[0]->getFirstType()->getString(PO);
363-
log << "])\n";
377+
log << "]";
364378
}
365379
if (conjunction) {
366-
auto &log = getDebugLogger();
367380
log.indent(2);
368381
log << "Conjunction(s) = [";
369382
auto constraints = conjunction->getNestedConstraints();
370383
log << constraints[0]->getFirstType()->getString(PO);
371-
log << "])\n";
384+
log << "]";
372385
}
386+
log << ")\n";
373387
}
374388

375389
if (CS.shouldAttemptFixes()) {

0 commit comments

Comments
 (0)