Skip to content

Commit dd11305

Browse files
authored
Merge pull request #20609 from rjmccall/pretty-stack-trace-conformance
[NFC] Add PrettyStackTraceConformance and use it
2 parents 3ab5a73 + 6ffeb4d commit dd11305

File tree

8 files changed

+73
-41
lines changed

8 files changed

+73
-41
lines changed

include/swift/AST/PrettyStackTrace.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace swift {
3333
class TypeRepr;
3434

3535
void printSourceLocDescription(llvm::raw_ostream &out, SourceLoc loc,
36-
ASTContext &Context);
36+
ASTContext &Context, bool addNewline = true);
3737

3838
/// PrettyStackTraceLocation - Observe that we are doing some
3939
/// processing starting at a fixed location.
@@ -48,7 +48,7 @@ class PrettyStackTraceLocation : public llvm::PrettyStackTraceEntry {
4848
};
4949

5050
void printDeclDescription(llvm::raw_ostream &out, const Decl *D,
51-
ASTContext &Context);
51+
ASTContext &Context, bool addNewline = true);
5252

5353
/// PrettyStackTraceDecl - Observe that we are processing a specific
5454
/// declaration.
@@ -62,7 +62,7 @@ class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
6262
};
6363

6464
void printExprDescription(llvm::raw_ostream &out, Expr *E,
65-
ASTContext &Context);
65+
ASTContext &Context, bool addNewline = true);
6666

6767
/// PrettyStackTraceExpr - Observe that we are processing a specific
6868
/// expression.
@@ -77,7 +77,7 @@ class PrettyStackTraceExpr : public llvm::PrettyStackTraceEntry {
7777
};
7878

7979
void printStmtDescription(llvm::raw_ostream &out, Stmt *S,
80-
ASTContext &Context);
80+
ASTContext &Context, bool addNewline = true);
8181

8282
/// PrettyStackTraceStmt - Observe that we are processing a specific
8383
/// statement.
@@ -92,7 +92,7 @@ class PrettyStackTraceStmt : public llvm::PrettyStackTraceEntry {
9292
};
9393

9494
void printPatternDescription(llvm::raw_ostream &out, Pattern *P,
95-
ASTContext &Context);
95+
ASTContext &Context, bool addNewline = true);
9696

9797
/// PrettyStackTracePattern - Observe that we are processing a
9898
/// specific pattern.
@@ -107,7 +107,7 @@ class PrettyStackTracePattern : public llvm::PrettyStackTraceEntry {
107107
};
108108

109109
void printTypeDescription(llvm::raw_ostream &out, Type T,
110-
ASTContext &Context);
110+
ASTContext &Context, bool addNewline = true);
111111

112112
/// PrettyStackTraceType - Observe that we are processing a specific type.
113113
class PrettyStackTraceType : public llvm::PrettyStackTraceEntry {
@@ -131,6 +131,23 @@ class PrettyStackTraceTypeRepr : public llvm::PrettyStackTraceEntry {
131131
virtual void print(llvm::raw_ostream &OS) const;
132132
};
133133

134+
/// PrettyStackTraceConformance - Observe that we are processing a
135+
/// specific protocol conformance.
136+
class PrettyStackTraceConformance : public llvm::PrettyStackTraceEntry {
137+
ASTContext &Context;
138+
const ProtocolConformance *Conformance;
139+
const char *Action;
140+
public:
141+
PrettyStackTraceConformance(ASTContext &C, const char *action,
142+
const ProtocolConformance *conformance)
143+
: Context(C), Conformance(conformance), Action(action) {}
144+
virtual void print(llvm::raw_ostream &OS) const;
145+
};
146+
147+
void printConformanceDescription(llvm::raw_ostream &out,
148+
const ProtocolConformance *conformance,
149+
ASTContext &Context, bool addNewline = true);
150+
134151
class PrettyStackTraceGenericSignature : public llvm::PrettyStackTraceEntry {
135152
const char *Action;
136153
GenericSignature *GenericSig;

lib/AST/PrettyStackTrace.cpp

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/AST/GenericSignature.h"
2222
#include "swift/AST/Module.h"
2323
#include "swift/AST/Pattern.h"
24+
#include "swift/AST/ProtocolConformance.h"
2425
#include "swift/AST/Stmt.h"
2526
#include "swift/AST/PrettyStackTrace.h"
2627
#include "swift/AST/TypeVisitor.h"
@@ -40,7 +41,7 @@ void PrettyStackTraceDecl::print(llvm::raw_ostream &out) const {
4041
}
4142

4243
void swift::printDeclDescription(llvm::raw_ostream &out, const Decl *D,
43-
ASTContext &Context) {
44+
ASTContext &Context, bool addNewline) {
4445
SourceLoc loc = D->getStartLoc();
4546
bool hasPrintedName = false;
4647
if (auto *named = dyn_cast<ValueDecl>(D)) {
@@ -94,12 +95,13 @@ void swift::printDeclDescription(llvm::raw_ostream &out, const Decl *D,
9495
out << "declaration " << (const void *)D;
9596

9697
if (loc.isValid()) {
97-
out << " at ";
98+
out << " (at ";
9899
loc.print(out, Context.SourceMgr);
100+
out << ')';
99101
} else {
100-
out << " in module '" << D->getModuleContext()->getName() << '\'';
102+
out << " (in module '" << D->getModuleContext()->getName() << "')";
101103
}
102-
out << '\n';
104+
if (addNewline) out << '\n';
103105
}
104106

105107
void PrettyStackTraceExpr::print(llvm::raw_ostream &out) const {
@@ -112,10 +114,10 @@ void PrettyStackTraceExpr::print(llvm::raw_ostream &out) const {
112114
}
113115

114116
void swift::printExprDescription(llvm::raw_ostream &out, Expr *E,
115-
ASTContext &Context) {
117+
ASTContext &Context, bool addNewline) {
116118
out << "expression at ";
117119
E->getSourceRange().print(out, Context.SourceMgr);
118-
out << '\n';
120+
if (addNewline) out << '\n';
119121
}
120122

121123
void PrettyStackTraceStmt::print(llvm::raw_ostream &out) const {
@@ -128,10 +130,10 @@ void PrettyStackTraceStmt::print(llvm::raw_ostream &out) const {
128130
}
129131

130132
void swift::printStmtDescription(llvm::raw_ostream &out, Stmt *S,
131-
ASTContext &Context) {
133+
ASTContext &Context, bool addNewline) {
132134
out << "statement at ";
133135
S->getSourceRange().print(out, Context.SourceMgr);
134-
out << '\n';
136+
if (addNewline) out << '\n';
135137
}
136138

137139
void PrettyStackTracePattern::print(llvm::raw_ostream &out) const {
@@ -144,10 +146,10 @@ void PrettyStackTracePattern::print(llvm::raw_ostream &out) const {
144146
}
145147

146148
void swift::printPatternDescription(llvm::raw_ostream &out, Pattern *P,
147-
ASTContext &Context) {
149+
ASTContext &Context, bool addNewline) {
148150
out << "pattern at ";
149151
P->getSourceRange().print(out, Context.SourceMgr);
150-
out << '\n';
152+
if (addNewline) out << '\n';
151153
}
152154

153155
namespace {
@@ -183,7 +185,7 @@ void PrettyStackTraceType::print(llvm::raw_ostream &out) const {
183185
}
184186

185187
void swift::printTypeDescription(llvm::raw_ostream &out, Type type,
186-
ASTContext &Context) {
188+
ASTContext &Context, bool addNewline) {
187189
out << "type '" << type << '\'';
188190
if (Decl *decl = InterestingDeclForType().visit(type)) {
189191
if (decl->getSourceRange().isValid()) {
@@ -192,7 +194,7 @@ void swift::printTypeDescription(llvm::raw_ostream &out, Type type,
192194
out << ')';
193195
}
194196
}
195-
out << '\n';
197+
if (addNewline) out << '\n';
196198
}
197199

198200
void PrettyStackTraceTypeRepr::print(llvm::raw_ostream &out) const {
@@ -205,10 +207,31 @@ void PrettyStackTraceTypeRepr::print(llvm::raw_ostream &out) const {
205207
out << '\n';
206208
}
207209

210+
void PrettyStackTraceConformance::print(llvm::raw_ostream &out) const {
211+
out << "While " << Action << ' ';
212+
printConformanceDescription(out, Conformance, Context);
213+
}
214+
215+
void swift::printConformanceDescription(llvm::raw_ostream &out,
216+
const ProtocolConformance *conformance,
217+
ASTContext &ctxt, bool addNewline) {
218+
if (!conformance) {
219+
out << "NULL protocol conformance!";
220+
if (addNewline) out << '\n';
221+
return;
222+
}
223+
224+
out << "protocol conformance to ";
225+
printDeclDescription(out, conformance->getProtocol(), ctxt, /*newline*/false);
226+
out << " for ";
227+
printTypeDescription(out, conformance->getType(), ctxt, addNewline);
228+
}
229+
208230
void swift::printSourceLocDescription(llvm::raw_ostream &out,
209-
SourceLoc loc, ASTContext &ctx) {
231+
SourceLoc loc, ASTContext &ctx,
232+
bool addNewline) {
210233
loc.print(out, ctx.SourceMgr);
211-
out << '\n';
234+
if (addNewline) out << '\n';
212235
}
213236

214237
void PrettyStackTraceLocation::print(llvm::raw_ostream &out) const {

lib/ClangImporter/ImportDecl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7788,9 +7788,8 @@ void ClangImporter::Implementation::finishNormalConformance(
77887788
(void)unused;
77897789

77907790
auto *proto = conformance->getProtocol();
7791-
PrettyStackTraceType trace(SwiftContext, "completing conformance for",
7792-
conformance->getType());
7793-
PrettyStackTraceDecl traceTo("... to", proto);
7791+
PrettyStackTraceConformance trace(SwiftContext, "completing import of",
7792+
conformance);
77947793

77957794
if (!proto->isRequirementSignatureComputed())
77967795
proto->computeRequirementSignature();

lib/IRGen/GenProto.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,9 +2203,7 @@ void IRGenModule::emitSILWitnessTable(SILWitnessTable *wt) {
22032203
return;
22042204

22052205
auto conf = wt->getConformance();
2206-
PrettyStackTraceType stackTraceRAII(Context, "emitting witness table for",
2207-
conf->getType());
2208-
PrettyStackTraceDecl stackTraceRAII2("...conforming to", conf->getProtocol());
2206+
PrettyStackTraceConformance _st(Context, "emitting witness table for", conf);
22092207

22102208
// Build the witness table.
22112209
ConstantInitBuilder builder(*this);

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4321,9 +4321,7 @@ void TypeChecker::useBridgedNSErrorConformances(DeclContext *dc, Type type) {
43214321
}
43224322

43234323
void TypeChecker::checkConformance(NormalProtocolConformance *conformance) {
4324-
PrettyStackTraceType trace1(Context, "checking conformance of",
4325-
conformance->getType());
4326-
PrettyStackTraceDecl trace2("...to", conformance->getProtocol());
4324+
PrettyStackTraceConformance trace(Context, "type-checking", conformance);
43274325

43284326
MultiConformanceChecker checker(*this);
43294327
checker.addConformance(conformance);
@@ -4336,9 +4334,8 @@ void TypeChecker::checkConformanceRequirements(
43364334
if (conformance->isInvalid())
43374335
return;
43384336

4339-
PrettyStackTraceType trace1(Context, "checking conformance requirements of",
4340-
conformance->getType());
4341-
PrettyStackTraceDecl trace2("...to", conformance->getProtocol());
4337+
PrettyStackTraceConformance trace(Context, "checking requirements of",
4338+
conformance);
43424339

43434340
conformance->setSignatureConformances({ });
43444341

lib/Serialization/Deserialization.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5183,10 +5183,9 @@ void ModuleFile::finishNormalConformance(NormalProtocolConformance *conformance,
51835183
using namespace decls_block;
51845184

51855185
PrettyStackTraceModuleFile traceModule("While reading from", *this);
5186-
PrettyStackTraceType trace(getAssociatedModule()->getASTContext(),
5187-
"finishing conformance for",
5188-
conformance->getType());
5189-
PrettyStackTraceDecl traceTo("... to", conformance->getProtocol());
5186+
PrettyStackTraceConformance trace(getAssociatedModule()->getASTContext(),
5187+
"finishing conformance for",
5188+
conformance);
51905189
++NumNormalProtocolConformancesCompleted;
51915190

51925191
assert(conformance->isComplete());

lib/Serialization/DeserializeSIL.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,10 +2947,9 @@ SILWitnessTable *SILDeserializer::readWitnessTable(DeclID WId,
29472947
auto theConformance = cast<RootProtocolConformance>(
29482948
MF->readConformance(SILCursor).getConcrete());
29492949

2950-
PrettyStackTraceType trace(SILMod.getASTContext(),
2951-
"deserializing SIL witness table for",
2952-
theConformance->getType());
2953-
PrettyStackTraceDecl trace2("... to", theConformance->getProtocol());
2950+
PrettyStackTraceConformance trace(SILMod.getASTContext(),
2951+
"deserializing SIL witness table for",
2952+
theConformance);
29542953

29552954
if (!existingWt)
29562955
existingWt = SILMod.lookUpWitnessTable(theConformance, false);

test/Serialization/Recovery/crash-recovery.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ public class Sub: Base {
1717
// CHECK-CRASH-4_2: note: compiling as Swift 4.2, with 'Lib' built as Swift 4.1.50
1818
// CHECK-CRASH-LABEL: *** DESERIALIZATION FAILURE (please include this section in any bug report) ***
1919
// CHECK-CRASH: could not find 'disappearingMethod()' in parent class
20-
// CHECK-CRASH: While loading members for 'Sub' in module 'Lib'
20+
// CHECK-CRASH: While loading members for 'Sub' (in module 'Lib')

0 commit comments

Comments
 (0)