Skip to content

Commit 876f446

Browse files
authored
Merge pull request #59855 from amritpan/improve-type-variable-printing
[ConstraintSystem] Improve type variable printing in the type inference algorithm debug output
2 parents 1821b62 + 35b0346 commit 876f446

File tree

4 files changed

+68
-22
lines changed

4 files changed

+68
-22
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,18 @@ class BindingSet {
558558
/// Check whether the given binding set covers any of the
559559
/// literal protocols associated with this type variable.
560560
void determineLiteralCoverage();
561+
562+
StringRef getLiteralBindingKind(LiteralBindingKind K) const {
563+
#define ENTRY(Kind, String) case LiteralBindingKind::Kind: return String
564+
switch (K) {
565+
ENTRY(None, "none");
566+
ENTRY(Collection, "collection");
567+
ENTRY(Float, "float");
568+
ENTRY(Atom, "atom");
569+
}
570+
#undef ENTRY
571+
}
572+
561573
};
562574

563575
} // end namespace inference

include/swift/Sema/ConstraintSystem.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,19 @@ class TypeVariableType::Implementation {
588588

589589
/// Print the type variable to the given output stream.
590590
void print(llvm::raw_ostream &OS);
591+
592+
private:
593+
StringRef getTypeVariableOptions(TypeVariableOptions TVO) const {
594+
#define ENTRY(Kind, String) case TypeVariableOptions::Kind: return String
595+
switch (TVO) {
596+
ENTRY(TVO_CanBindToLValue, "lvalue");
597+
ENTRY(TVO_CanBindToInOut, "inout");
598+
ENTRY(TVO_CanBindToNoEscape, "noescape");
599+
ENTRY(TVO_CanBindToHole, "hole");
600+
ENTRY(TVO_PrefersSubtypeBinding, "");
601+
}
602+
#undef ENTRY
603+
}
591604
};
592605

593606
namespace constraints {

lib/Sema/CSBindings.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,28 +1620,37 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
16201620
PO.PrintTypesForDebugging = true;
16211621

16221622
out.indent(indent);
1623+
std::vector<std::string> attributes;
16231624
if (isDirectHole())
1624-
out << "hole ";
1625+
attributes.push_back("hole");
16251626
if (isPotentiallyIncomplete())
1626-
out << "potentially_incomplete ";
1627+
attributes.push_back("potentially_incomplete");
16271628
if (isDelayed())
1628-
out << "delayed ";
1629+
attributes.push_back("delayed");
16291630
if (isSubtypeOfExistentialType())
1630-
out << "subtype_of_existential ";
1631+
attributes.push_back("subtype_of_existential");
16311632
auto literalKind = getLiteralKind();
1632-
if (literalKind != LiteralBindingKind::None)
1633-
out << "literal=" << static_cast<int>(literalKind) << " ";
1633+
if (literalKind != LiteralBindingKind::None) {
1634+
auto literalAttrStr = ("[literal: " + getLiteralBindingKind(literalKind)
1635+
+ "]").str();
1636+
attributes.push_back(literalAttrStr);
1637+
}
1638+
if (!attributes.empty()) {
1639+
out << "[attributes: ";
1640+
interleave(attributes, out, ", ");
1641+
out << "] ";
1642+
}
16341643
if (involvesTypeVariables()) {
1635-
out << "involves_type_vars=[";
1644+
out << "[involves_type_vars: ";
16361645
interleave(AdjacentVars,
16371646
[&](const auto *typeVar) { out << typeVar->getString(PO); },
1638-
[&out]() { out << " "; });
1647+
[&out]() { out << ", "; });
16391648
out << "] ";
16401649
}
16411650

16421651
auto numDefaultable = getNumViableDefaultableBindings();
16431652
if (numDefaultable > 0)
1644-
out << "#defaultable_bindings=" << numDefaultable << " ";
1653+
out << "#defaultable_bindings: " << numDefaultable << " ";
16451654

16461655
auto printBinding = [&](const PotentialBinding &binding) {
16471656
auto type = binding.BindingType;
@@ -1662,19 +1671,21 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
16621671
out << type.getString(PO);
16631672
};
16641673

1665-
out << "bindings={";
1674+
out << "[with possible bindings: ";
16661675
interleave(Bindings, printBinding, [&]() { out << "; "; });
1667-
out << "}";
1676+
if (Bindings.empty())
1677+
out << "<empty>";
1678+
out << "]";
16681679

16691680
if (!Defaults.empty()) {
1670-
out << " defaults={";
1681+
out << "[defaults: ";
16711682
for (const auto &entry : Defaults) {
16721683
auto *constraint = entry.second;
16731684
PotentialBinding binding{constraint->getSecondType(),
16741685
AllowedBindingKind::Exact, constraint};
16751686
printBinding(binding);
16761687
}
1677-
out << "}";
1688+
out << "] ";
16781689
}
16791690
}
16801691

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ using namespace constraints;
5555

5656
void TypeVariableType::Implementation::print(llvm::raw_ostream &OS) {
5757
getTypeVariable()->print(OS, PrintOptions());
58+
59+
SmallVector<TypeVariableOptions, 4> bindingOptions;
60+
if (canBindToLValue())
61+
bindingOptions.push_back(TypeVariableOptions::TVO_CanBindToLValue);
62+
if (canBindToInOut())
63+
bindingOptions.push_back(TypeVariableOptions::TVO_CanBindToInOut);
64+
if (canBindToNoEscape())
65+
bindingOptions.push_back(TypeVariableOptions::TVO_CanBindToNoEscape);
66+
if (canBindToHole())
67+
bindingOptions.push_back(TypeVariableOptions::TVO_CanBindToHole);
68+
if (!bindingOptions.empty()) {
69+
OS << " [allows bindings to: ";
70+
interleave(bindingOptions, OS,
71+
[&](TypeVariableOptions option) {
72+
(OS << getTypeVariableOptions(option));},
73+
", ");
74+
OS << "]";
75+
}
5876
}
5977

6078
SavedTypeVariableBinding::SavedTypeVariableBinding(TypeVariableType *typeVar)
@@ -1415,15 +1433,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
14151433
});
14161434
for (auto tv : typeVariables) {
14171435
out.indent(2);
1418-
Type(tv).print(out, PO);
1419-
if (tv->getImpl().canBindToLValue())
1420-
out << " [lvalue allowed]";
1421-
if (tv->getImpl().canBindToInOut())
1422-
out << " [inout allowed]";
1423-
if (tv->getImpl().canBindToNoEscape())
1424-
out << " [noescape allowed]";
1425-
if (tv->getImpl().canBindToHole())
1426-
out << " [hole allowed]";
1436+
tv->getImpl().print(out);
14271437
auto rep = getRepresentative(tv);
14281438
if (rep == tv) {
14291439
if (auto fixed = getFixedType(tv)) {

0 commit comments

Comments
 (0)