Skip to content

Commit 5b55428

Browse files
authored
Merge pull request swiftlang#63235 from hamishknight/printables
[CS] Debug: Print ASTNode for contextual type
2 parents ef9db60 + 76da268 commit 5b55428

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6854,6 +6854,9 @@ ASTNode findAsyncNode(ClosureExpr *closure);
68546854
/// empty type otherwise.
68556855
Type isPlaceholderVar(PatternBindingDecl *PB);
68566856

6857+
/// Dump an anchor node for a constraint locator or contextual type.
6858+
void dumpAnchor(ASTNode anchor, SourceManager *SM, raw_ostream &out);
6859+
68576860
} // end namespace constraints
68586861

68596862
template<typename ...Args>

lib/Sema/ConstraintLocator.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -630,19 +630,7 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) const {
630630

631631
out << "locator@" << (void*) this << " [";
632632

633-
if (auto *expr = anchor.dyn_cast<Expr *>()) {
634-
out << Expr::getKindName(expr->getKind());
635-
if (sm) {
636-
out << '@';
637-
expr->getLoc().print(out, *sm);
638-
}
639-
} else if (auto *pattern = anchor.dyn_cast<Pattern *>()) {
640-
out << Pattern::getKindName(pattern->getKind()) << "Pattern";
641-
if (sm) {
642-
out << '@';
643-
pattern->getLoc().print(out, *sm);
644-
}
645-
}
633+
constraints::dumpAnchor(anchor, sm, out);
646634

647635
for (auto elt : getPath()) {
648636
out << " -> ";

lib/Sema/ConstraintSystem.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7159,3 +7159,21 @@ ASTNode constraints::findAsyncNode(ClosureExpr *closure) {
71597159
return ASTNode();
71607160
return body->findAsyncNode();
71617161
}
7162+
7163+
void constraints::dumpAnchor(ASTNode anchor, SourceManager *SM,
7164+
raw_ostream &out) {
7165+
if (auto *expr = anchor.dyn_cast<Expr *>()) {
7166+
out << Expr::getKindName(expr->getKind());
7167+
if (SM) {
7168+
out << '@';
7169+
expr->getLoc().print(out, *SM);
7170+
}
7171+
} else if (auto *pattern = anchor.dyn_cast<Pattern *>()) {
7172+
out << Pattern::getKindName(pattern->getKind()) << "Pattern";
7173+
if (SM) {
7174+
out << '@';
7175+
pattern->getLoc().print(out, *SM);
7176+
}
7177+
}
7178+
// TODO(diagnostics): Implement the rest of the cases.
7179+
}

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,9 +1465,13 @@ void ConstraintSystem::print(raw_ostream &out) const {
14651465
if (!info.getType().isNull()) {
14661466
out << "\n";
14671467
out.indent(indent) << "Contextual Type: " << info.getType().getString(PO);
1468+
out << " at ";
1469+
1470+
auto &SM = getASTContext().SourceMgr;
14681471
if (TypeRepr *TR = info.typeLoc.getTypeRepr()) {
1469-
out << " at ";
1470-
TR->getSourceRange().print(out, getASTContext().SourceMgr, /*text*/false);
1472+
TR->getSourceRange().print(out, SM, /*text*/ false);
1473+
} else {
1474+
dumpAnchor(contextualTypeEntry.first, &SM, out);
14711475
}
14721476
}
14731477
}

0 commit comments

Comments
 (0)