Skip to content

Commit 3c4fef1

Browse files
committed
Sema: Add some LLVM_DEBUG output to CSBindings.cpp
1 parent 25d9829 commit 3c4fef1

File tree

2 files changed

+127
-55
lines changed

2 files changed

+127
-55
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ struct PotentialBinding {
147147
return {placeholderTy, AllowedBindingKind::Exact,
148148
PointerUnion<Constraint *, ConstraintLocator *>()};
149149
}
150+
151+
void print(llvm::raw_ostream &out, const PrintOptions &PO) const;
150152
};
151153

152154
struct LiteralRequirement {

lib/Sema/CSBindings.cpp

Lines changed: 125 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,73 @@ bool PotentialBinding::isViableForJoin() const {
112112
!isDefaultableBinding();
113113
}
114114

115+
namespace {
116+
117+
struct PrintableBinding {
118+
private:
119+
enum class BindingKind { Exact, Subtypes, Supertypes, Literal };
120+
BindingKind Kind;
121+
Type BindingType;
122+
bool Viable;
123+
PrintableBinding(BindingKind kind, Type bindingType, bool viable)
124+
: Kind(kind), BindingType(bindingType), Viable(viable) {}
125+
126+
public:
127+
static PrintableBinding supertypesOf(Type binding) {
128+
return PrintableBinding{BindingKind::Supertypes, binding, true};
129+
}
130+
131+
static PrintableBinding subtypesOf(Type binding) {
132+
return PrintableBinding{BindingKind::Subtypes, binding, true};
133+
}
134+
135+
static PrintableBinding exact(Type binding) {
136+
return PrintableBinding{BindingKind::Exact, binding, true};
137+
}
138+
139+
static PrintableBinding literalDefaultType(Type binding, bool viable) {
140+
return PrintableBinding{BindingKind::Literal, binding, viable};
141+
}
142+
143+
void print(llvm::raw_ostream &out, const PrintOptions &PO,
144+
unsigned indent = 0) const {
145+
switch (Kind) {
146+
case BindingKind::Exact:
147+
break;
148+
case BindingKind::Subtypes:
149+
out << "(subtypes of) ";
150+
break;
151+
case BindingKind::Supertypes:
152+
out << "(supertypes of) ";
153+
break;
154+
case BindingKind::Literal:
155+
out << "(default type of literal) ";
156+
break;
157+
}
158+
if (BindingType)
159+
BindingType.print(out, PO);
160+
if (!Viable)
161+
out << " [literal not viable]";
162+
}
163+
};
164+
165+
}
166+
167+
void PotentialBinding::print(llvm::raw_ostream &out,
168+
const PrintOptions &PO) const {
169+
switch (Kind) {
170+
case AllowedBindingKind::Exact:
171+
PrintableBinding::exact(BindingType).print(out, PO);
172+
break;
173+
case AllowedBindingKind::Supertypes:
174+
PrintableBinding::supertypesOf(BindingType).print(out, PO);
175+
break;
176+
case AllowedBindingKind::Subtypes:
177+
PrintableBinding::subtypesOf(BindingType).print(out, PO);
178+
break;
179+
}
180+
}
181+
115182
bool BindingSet::isDelayed() const {
116183
if (auto *locator = TypeVar->getImpl().getLocator()) {
117184
if (locator->isLastElement<LocatorPathElt::MemberRefBase>()) {
@@ -1301,6 +1368,21 @@ void PotentialBindings::addPotentialBinding(TypeVariableType *TypeVar,
13011368
binding = binding.withType(binding.BindingType->getRValueType());
13021369
}
13031370

1371+
LLVM_DEBUG(
1372+
PrintOptions PO = PrintOptions::forDebugging();
1373+
llvm::dbgs() << "Recording ";
1374+
TypeVar->print(llvm::dbgs(), PO);
1375+
llvm::dbgs() << " ";
1376+
binding.print(llvm::dbgs(), PO);
1377+
llvm::dbgs() << " from ";
1378+
if (auto *constraint = dyn_cast<Constraint *>(binding.BindingSource)) {
1379+
constraint->print(llvm::dbgs(), &TypeVar->getASTContext().SourceMgr, 0);
1380+
} else {
1381+
auto *locator = cast<ConstraintLocator *>(binding.BindingSource);
1382+
locator->dump(&TypeVar->getASTContext().SourceMgr, llvm::dbgs());
1383+
}
1384+
llvm::dbgs() << "\n");
1385+
13041386
Bindings.push_back(std::move(binding));
13051387
}
13061388

@@ -1604,11 +1686,29 @@ PotentialBindings::inferFromRelational(ConstraintSystem &CS,
16041686
ConstraintClassification::Relational &&
16051687
"only relational constraints handled here");
16061688

1689+
LLVM_DEBUG(
1690+
llvm::dbgs() << "inferFromRelational(";
1691+
TypeVar->print(llvm::dbgs(), PrintOptions::forDebugging());
1692+
llvm::dbgs() << ", ";
1693+
constraint->print(llvm::dbgs(), &CS.getASTContext().SourceMgr, 0);
1694+
llvm::dbgs() << ")\n");
1695+
16071696
auto first = CS.simplifyType(constraint->getFirstType());
16081697
auto second = CS.simplifyType(constraint->getSecondType());
16091698

1610-
if (first->is<TypeVariableType>() && first->isEqual(second))
1699+
#define DEBUG_BAILOUT(msg) \
1700+
LLVM_DEBUG( \
1701+
PrintOptions PO = PrintOptions::forDebugging(); \
1702+
llvm::dbgs() << msg << " "; \
1703+
TypeVar->print(llvm::dbgs(), PO); \
1704+
llvm::dbgs() << " from "; \
1705+
constraint->print(llvm::dbgs(), &CS.getASTContext().SourceMgr); \
1706+
llvm::dbgs() << "\n");
1707+
1708+
if (first->is<TypeVariableType>() && first->isEqual(second)) {
1709+
DEBUG_BAILOUT("First is second");
16111710
return std::nullopt;
1711+
}
16121712

16131713
Type type;
16141714
AllowedBindingKind kind;
@@ -1629,6 +1729,7 @@ PotentialBindings::inferFromRelational(ConstraintSystem &CS,
16291729
// of bindings for them until closure's body is opened.
16301730
if (auto *typeVar = first->getAs<TypeVariableType>()) {
16311731
if (typeVar->getImpl().isClosureType()) {
1732+
DEBUG_BAILOUT("Delayed (1)");
16321733
DelayedBy.push_back(constraint);
16331734
return std::nullopt;
16341735
}
@@ -1662,8 +1763,10 @@ PotentialBindings::inferFromRelational(ConstraintSystem &CS,
16621763
}
16631764

16641765
// Do not attempt to bind to ErrorType.
1665-
if (type->hasError())
1766+
if (type->hasError()) {
1767+
DEBUG_BAILOUT("Has error");
16661768
return std::nullopt;
1769+
}
16671770

16681771
if (TypeVar->getImpl().isKeyPathType()) {
16691772
auto objectTy = type->lookThroughAllOptionalTypes();
@@ -1682,8 +1785,10 @@ PotentialBindings::inferFromRelational(ConstraintSystem &CS,
16821785
}
16831786
}
16841787

1685-
if (!(objectTy->isKnownKeyPathType() || objectTy->is<AnyFunctionType>()))
1788+
if (!(objectTy->isKnownKeyPathType() || objectTy->is<AnyFunctionType>())) {
1789+
DEBUG_BAILOUT("Bad key path type (1)");
16861790
return std::nullopt;
1791+
}
16871792
}
16881793

16891794
if (TypeVar->getImpl().isKeyPathSubscriptIndex()) {
@@ -1701,6 +1806,7 @@ PotentialBindings::inferFromRelational(ConstraintSystem &CS,
17011806
if (auto superclass = layout.explicitSuperclass) {
17021807
type = superclass;
17031808
} else if (!CS.shouldAttemptFixes()) {
1809+
DEBUG_BAILOUT("Bad key path type (2)");
17041810
return std::nullopt;
17051811
}
17061812
}
@@ -1724,23 +1830,28 @@ PotentialBindings::inferFromRelational(ConstraintSystem &CS,
17241830
// type of a chain, Result should always be a concrete type which conforms
17251831
// to the protocol inferred for the base.
17261832
if (constraint->getKind() == ConstraintKind::UnresolvedMemberChainBase &&
1727-
kind == AllowedBindingKind::Subtypes && type->is<ProtocolType>())
1833+
kind == AllowedBindingKind::Subtypes && type->is<ProtocolType>()) {
1834+
DEBUG_BAILOUT("Unresolved member chain base");
17281835
return std::nullopt;
1836+
}
17291837
}
17301838

17311839
if (constraint->getKind() == ConstraintKind::LValueObject) {
17321840
// Allow l-value type inference from its object type, but
17331841
// not the other way around, that would be handled by constraint
17341842
// simplification.
17351843
if (kind == AllowedBindingKind::Subtypes) {
1736-
if (type->isTypeVariableOrMember())
1844+
if (type->isTypeVariableOrMember()) {
1845+
DEBUG_BAILOUT("Disallowed l-value inference");
17371846
return std::nullopt;
1847+
}
17381848

17391849
type = LValueType::get(type);
17401850
} else {
17411851
// Right-hand side of the l-value object constraint can only
17421852
// be bound via constraint simplification when l-value type
17431853
// is inferred or contextually from other constraints.
1854+
DEBUG_BAILOUT("Delayed (2)");
17441855
DelayedBy.push_back(constraint);
17451856
return std::nullopt;
17461857
}
@@ -1781,6 +1892,7 @@ PotentialBindings::inferFromRelational(ConstraintSystem &CS,
17811892
if (!containsSelf)
17821893
DelayedBy.push_back(constraint);
17831894

1895+
DEBUG_BAILOUT("Dependent member");
17841896
return std::nullopt;
17851897
}
17861898

@@ -1805,8 +1917,10 @@ PotentialBindings::inferFromRelational(ConstraintSystem &CS,
18051917
if (!checkTypeOfBinding(TypeVar, type)) {
18061918
auto *bindingTypeVar = type->getRValueType()->getAs<TypeVariableType>();
18071919

1808-
if (!bindingTypeVar)
1920+
if (!bindingTypeVar) {
1921+
DEBUG_BAILOUT("Not a type variable");
18091922
return std::nullopt;
1923+
}
18101924

18111925
// If current type variable is associated with a code completion token
18121926
// it's possible that it doesn't have enough contextual information
@@ -1873,8 +1987,10 @@ PotentialBindings::inferFromRelational(ConstraintSystem &CS,
18731987
// lvalue-binding rules.
18741988
if (auto otherTypeVar = type->getAs<TypeVariableType>()) {
18751989
if (TypeVar->getImpl().canBindToLValue() !=
1876-
otherTypeVar->getImpl().canBindToLValue())
1990+
otherTypeVar->getImpl().canBindToLValue()) {
1991+
DEBUG_BAILOUT("LValue mismatch");
18771992
return std::nullopt;
1993+
}
18781994
}
18791995

18801996
if (type->is<InOutType>() && !TypeVar->getImpl().canBindToInOut())
@@ -1899,6 +2015,8 @@ PotentialBindings::inferFromRelational(ConstraintSystem &CS,
18992015
return PotentialBinding{type, kind, constraint};
19002016
}
19012017

2018+
#undef DEBUG_BAILOUT
2019+
19022020
/// Retrieve the set of potential type bindings for the given
19032021
/// representative type variable, along with flags indicating whether
19042022
/// those types should be opened.
@@ -2348,54 +2466,6 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
23482466
if (numDefaultable > 0)
23492467
out << "[defaultable bindings: " << numDefaultable << "] ";
23502468

2351-
struct PrintableBinding {
2352-
private:
2353-
enum class BindingKind { Exact, Subtypes, Supertypes, Literal };
2354-
BindingKind Kind;
2355-
Type BindingType;
2356-
bool Viable;
2357-
PrintableBinding(BindingKind kind, Type bindingType, bool viable)
2358-
: Kind(kind), BindingType(bindingType), Viable(viable) {}
2359-
2360-
public:
2361-
static PrintableBinding supertypesOf(Type binding) {
2362-
return PrintableBinding{BindingKind::Supertypes, binding, true};
2363-
}
2364-
2365-
static PrintableBinding subtypesOf(Type binding) {
2366-
return PrintableBinding{BindingKind::Subtypes, binding, true};
2367-
}
2368-
2369-
static PrintableBinding exact(Type binding) {
2370-
return PrintableBinding{BindingKind::Exact, binding, true};
2371-
}
2372-
2373-
static PrintableBinding literalDefaultType(Type binding, bool viable) {
2374-
return PrintableBinding{BindingKind::Literal, binding, viable};
2375-
}
2376-
2377-
void print(llvm::raw_ostream &out, const PrintOptions &PO,
2378-
unsigned indent = 0) const {
2379-
switch (Kind) {
2380-
case BindingKind::Exact:
2381-
break;
2382-
case BindingKind::Subtypes:
2383-
out << "(subtypes of) ";
2384-
break;
2385-
case BindingKind::Supertypes:
2386-
out << "(supertypes of) ";
2387-
break;
2388-
case BindingKind::Literal:
2389-
out << "(default type of literal) ";
2390-
break;
2391-
}
2392-
if (BindingType)
2393-
BindingType.print(out, PO);
2394-
if (!Viable)
2395-
out << " [literal not viable]";
2396-
}
2397-
};
2398-
23992469
out << "[potential bindings: ";
24002470
SmallVector<PrintableBinding, 2> potentialBindings;
24012471
for (const auto &binding : Bindings) {

0 commit comments

Comments
 (0)