Skip to content

Commit f09192b

Browse files
committed
Sema: Add PotentialBindings::dump() and improve BindingSet::dump()
1 parent 05642d8 commit f09192b

File tree

2 files changed

+67
-28
lines changed

2 files changed

+67
-28
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ struct PotentialBindings {
301301
Constraint *constraint);
302302

303303
void reset();
304+
305+
void dump(ConstraintSystem &CS,
306+
TypeVariableType *TypeVar,
307+
llvm::raw_ostream &out,
308+
unsigned indent) const;
304309
};
305310

306311

lib/Sema/CSBindings.cpp

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,42 @@ void PotentialBindings::reset() {
20452045
AssociatedCodeCompletionToken = ASTNode();
20462046
}
20472047

2048+
void PotentialBindings::dump(ConstraintSystem &cs,
2049+
TypeVariableType *typeVar,
2050+
llvm::raw_ostream &out,
2051+
unsigned indent) const {
2052+
PrintOptions PO;
2053+
PO.PrintTypesForDebugging = true;
2054+
2055+
out << "Potential bindings for ";
2056+
typeVar->getImpl().print(out);
2057+
out << "\n";
2058+
2059+
out << "[constraints: ";
2060+
interleave(Constraints,
2061+
[&](Constraint *constraint) {
2062+
constraint->print(out, &cs.getASTContext().SourceMgr, indent,
2063+
/*skipLocator=*/true);
2064+
},
2065+
[&out]() { out << ", "; });
2066+
out << "] ";
2067+
2068+
if (!AdjacentVars.empty()) {
2069+
out << "[adjacent_vars: ";
2070+
interleave(AdjacentVars,
2071+
[&](std::pair<TypeVariableType *, Constraint *> pair) {
2072+
out << pair.first->getString(PO);
2073+
if (pair.first->getImpl().getFixedType(/*record=*/nullptr))
2074+
out << " (fixed)";
2075+
out << " via ";
2076+
pair.second->print(out, &cs.getASTContext().SourceMgr, indent,
2077+
/*skipLocator=*/true);
2078+
},
2079+
[&out]() { out << ", "; });
2080+
out << "] ";
2081+
}
2082+
}
2083+
20482084
void BindingSet::forEachLiteralRequirement(
20492085
llvm::function_ref<void(KnownProtocolKind)> callback) const {
20502086
for (const auto &literal : Literals) {
@@ -2184,22 +2220,15 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
21842220
if (!attributes.empty())
21852221
out << "] ";
21862222

2187-
if (involvesTypeVariables()) {
2188-
out << "[adjacent to: ";
2189-
if (AdjacentVars.empty()) {
2190-
out << "<none>";
2191-
} else {
2192-
SmallVector<TypeVariableType *> adjacentVars(AdjacentVars.begin(),
2193-
AdjacentVars.end());
2194-
llvm::sort(adjacentVars,
2195-
[](const TypeVariableType *lhs, const TypeVariableType *rhs) {
2196-
return lhs->getID() < rhs->getID();
2197-
});
2198-
interleave(
2199-
adjacentVars,
2200-
[&](const auto *typeVar) { out << typeVar->getString(PO); },
2201-
[&out]() { out << ", "; });
2202-
}
2223+
if (!AdjacentVars.empty()) {
2224+
out << "[adjacent_vars: ";
2225+
interleave(AdjacentVars,
2226+
[&](auto *typeVar) {
2227+
out << typeVar->getString(PO);
2228+
if (typeVar->getImpl().getFixedType(/*record=*/nullptr))
2229+
out << " (fixed)";
2230+
},
2231+
[&out]() { out << ", "; });
22032232
out << "] ";
22042233
}
22052234

@@ -2212,24 +2241,25 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
22122241
enum class BindingKind { Exact, Subtypes, Supertypes, Literal };
22132242
BindingKind Kind;
22142243
Type BindingType;
2215-
PrintableBinding(BindingKind kind, Type bindingType)
2216-
: Kind(kind), BindingType(bindingType) {}
2244+
bool Viable;
2245+
PrintableBinding(BindingKind kind, Type bindingType, bool viable)
2246+
: Kind(kind), BindingType(bindingType), Viable(viable) {}
22172247

22182248
public:
22192249
static PrintableBinding supertypesOf(Type binding) {
2220-
return PrintableBinding{BindingKind::Supertypes, binding};
2250+
return PrintableBinding{BindingKind::Supertypes, binding, true};
22212251
}
22222252

22232253
static PrintableBinding subtypesOf(Type binding) {
2224-
return PrintableBinding{BindingKind::Subtypes, binding};
2254+
return PrintableBinding{BindingKind::Subtypes, binding, true};
22252255
}
22262256

22272257
static PrintableBinding exact(Type binding) {
2228-
return PrintableBinding{BindingKind::Exact, binding};
2258+
return PrintableBinding{BindingKind::Exact, binding, true};
22292259
}
22302260

2231-
static PrintableBinding literalDefaultType(Type binding) {
2232-
return PrintableBinding{BindingKind::Literal, binding};
2261+
static PrintableBinding literalDefaultType(Type binding, bool viable) {
2262+
return PrintableBinding{BindingKind::Literal, binding, viable};
22332263
}
22342264

22352265
void print(llvm::raw_ostream &out, const PrintOptions &PO,
@@ -2247,7 +2277,10 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
22472277
out << "(default type of literal) ";
22482278
break;
22492279
}
2250-
BindingType.print(out, PO);
2280+
if (BindingType)
2281+
BindingType.print(out, PO);
2282+
if (!Viable)
2283+
out << " [literal not viable]";
22512284
}
22522285
};
22532286

@@ -2269,10 +2302,11 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
22692302
}
22702303
}
22712304
for (const auto &literal : Literals) {
2272-
if (literal.second.viableAsBinding()) {
2273-
potentialBindings.push_back(PrintableBinding::literalDefaultType(
2274-
literal.second.getDefaultType()));
2275-
}
2305+
potentialBindings.push_back(PrintableBinding::literalDefaultType(
2306+
literal.second.hasDefaultType()
2307+
? literal.second.getDefaultType()
2308+
: Type(),
2309+
literal.second.viableAsBinding()));
22762310
}
22772311
if (potentialBindings.empty()) {
22782312
out << "<none>";

0 commit comments

Comments
 (0)