Skip to content

Commit dd9c28b

Browse files
committed
[ConstraintSystem] Add proper printing (name + locator) for fixes
1 parent ebb9f86 commit dd9c28b

File tree

5 files changed

+53
-39
lines changed

5 files changed

+53
-39
lines changed

lib/Sema/CSFix.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
#include "ConstraintSystem.h"
2323
#include "swift/AST/Expr.h"
2424
#include "swift/AST/Type.h"
25+
#include "swift/Basic/SourceManager.h"
26+
#include "llvm/ADT/SmallString.h"
2527
#include "llvm/Support/raw_ostream.h"
28+
#include <string>
2629

2730
using namespace swift;
2831
using namespace constraints;
@@ -31,18 +34,30 @@ ConstraintFix::~ConstraintFix() {}
3134

3235
Expr *ConstraintFix::getAnchor() const { return getLocator()->getAnchor(); }
3336

34-
void ConstraintFix::dump() const { print(llvm::errs()); }
37+
void ConstraintFix::print(llvm::raw_ostream &Out, SourceManager *sm) const {
38+
Out << "[fix: ";
39+
Out << getName();
40+
Out << ']';
41+
Out << " @ ";
42+
getLocator()->dump(sm, Out);
43+
}
44+
45+
void ConstraintFix::dump(SourceManager *sm) const { print(llvm::errs(), sm); }
46+
47+
std::string ForceDowncast::getName() const {
48+
llvm::SmallString<16> name;
49+
name += "force downcast (as! ";
50+
name += DowncastTo->getString();
51+
name += ")";
52+
return name.c_str();
53+
}
3554

3655
bool ForceDowncast::diagnose(Expr *expr, const Solution &solution) const {
3756
MissingExplicitConversionFailure failure(expr, solution, getLocator(),
3857
DowncastTo);
3958
return failure.diagnose();
4059
}
4160

42-
void ForceDowncast::print(llvm::raw_ostream &Out) const {
43-
Out << "[fix: force downcast (as! " << DowncastTo->getString() << ")]";
44-
}
45-
4661
ForceDowncast *ForceDowncast::create(ConstraintSystem &cs, Type toType,
4762
ConstraintLocator *locator) {
4863
return new (cs.getAllocator()) ForceDowncast(toType, locator);

lib/Sema/CSFix.h

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
#include "llvm/ADT/ArrayRef.h"
2525
#include "llvm/ADT/SmallVector.h"
2626
#include "llvm/Support/TrailingObjects.h"
27+
#include <string>
2728

2829
namespace llvm {
2930
class raw_ostream;
3031
}
3132

3233
namespace swift {
34+
35+
class SourceManager;
36+
3337
namespace constraints {
3438

3539
class ConstraintSystem;
@@ -78,12 +82,16 @@ class ConstraintFix {
7882

7983
FixKind getKind() const { return Kind; }
8084

85+
virtual std::string getName() const = 0;
86+
8187
/// Diagnose a failure associated with this fix given
8288
/// root expression and information from well-formed solution.
8389
virtual bool diagnose(Expr *root, const Solution &solution) const = 0;
84-
virtual void print(llvm::raw_ostream &Out) const = 0;
8590

86-
LLVM_ATTRIBUTE_DEPRECATED(void dump() const LLVM_ATTRIBUTE_USED,
91+
void print(llvm::raw_ostream &Out, SourceManager *sm) const;
92+
93+
LLVM_ATTRIBUTE_DEPRECATED(void dump(SourceManager *sm)
94+
const LLVM_ATTRIBUTE_USED,
8795
"only for use within the debugger");
8896

8997
/// Retrieve anchor expression associated with this fix.
@@ -101,8 +109,8 @@ class ForceDowncast final : public ConstraintFix {
101109
: ConstraintFix(FixKind::ForceDowncast, locator), DowncastTo(toType) {}
102110

103111
public:
112+
std::string getName() const override;
104113
bool diagnose(Expr *root, const Solution &solution) const override;
105-
void print(llvm::raw_ostream &Out) const override;
106114

107115
static ForceDowncast *create(ConstraintSystem &cs, Type toType,
108116
ConstraintLocator *locator);
@@ -114,10 +122,9 @@ class ForceOptional final : public ConstraintFix {
114122
: ConstraintFix(FixKind::ForceOptional, locator) {}
115123

116124
public:
125+
std::string getName() const override { return "force optional"; }
126+
117127
bool diagnose(Expr *root, const Solution &solution) const override;
118-
void print(llvm::raw_ostream &Out) const override {
119-
Out << "[fix: force optional]";
120-
}
121128

122129
static ForceOptional *create(ConstraintSystem &cs,
123130
ConstraintLocator *locator);
@@ -134,11 +141,12 @@ class UnwrapOptionalBase final : public ConstraintFix {
134141
}
135142

136143
public:
137-
bool diagnose(Expr *root, const Solution &solution) const override;
138-
void print(llvm::raw_ostream &Out) const override {
139-
Out << "[fix: unwrap optional base of member lookup]";
144+
std::string getName() const override {
145+
return "unwrap optional base of member lookup";
140146
}
141147

148+
bool diagnose(Expr *root, const Solution &solution) const override;
149+
142150
static UnwrapOptionalBase *create(ConstraintSystem &cs, DeclName member,
143151
ConstraintLocator *locator);
144152

@@ -153,10 +161,9 @@ class AddAddressOf final : public ConstraintFix {
153161
: ConstraintFix(FixKind::AddressOf, locator) {}
154162

155163
public:
164+
std::string getName() const override { return "add address-of"; }
165+
156166
bool diagnose(Expr *root, const Solution &solution) const override;
157-
void print(llvm::raw_ostream &Out) const override {
158-
Out << "[fix: add address-of]";
159-
}
160167

161168
static AddAddressOf *create(ConstraintSystem &cs, ConstraintLocator *locator);
162169
};
@@ -167,10 +174,9 @@ class CoerceToCheckedCast final : public ConstraintFix {
167174
: ConstraintFix(FixKind::CoerceToCheckedCast, locator) {}
168175

169176
public:
177+
std::string getName() const override { return "as to as!"; }
178+
170179
bool diagnose(Expr *root, const Solution &solution) const override;
171-
void print(llvm::raw_ostream &Out) const override {
172-
Out << "[fix: as to as!]";
173-
}
174180

175181
static CoerceToCheckedCast *create(ConstraintSystem &cs,
176182
ConstraintLocator *locator);
@@ -187,10 +193,9 @@ class MarkExplicitlyEscaping final : public ConstraintFix {
187193
ConvertTo(convertingTo) {}
188194

189195
public:
196+
std::string getName() const override { return "add @escaping"; }
197+
190198
bool diagnose(Expr *root, const Solution &solution) const override;
191-
void print(llvm::raw_ostream &Out) const override {
192-
Out << "[fix: add @escaping]";
193-
}
194199

195200
static MarkExplicitlyEscaping *create(ConstraintSystem &cs,
196201
ConstraintLocator *locator,
@@ -215,14 +220,13 @@ class RelabelArguments final
215220
}
216221

217222
public:
223+
std::string getName() const override { return "re-label argument(s)"; }
224+
218225
ArrayRef<Identifier> getLabels() const {
219226
return {getTrailingObjects<Identifier>(), NumLabels};
220227
}
221228

222229
bool diagnose(Expr *root, const Solution &solution) const override;
223-
void print(llvm::raw_ostream &Out) const override {
224-
Out << "[fix: re-label argument(s)]";
225-
}
226230

227231
static RelabelArguments *create(ConstraintSystem &cs,
228232
llvm::ArrayRef<Identifier> correctLabels,
@@ -245,11 +249,12 @@ class MissingConformance final : public ConstraintFix {
245249
NonConformingType(type), Protocol(protocol) {}
246250

247251
public:
248-
bool diagnose(Expr *root, const Solution &solution) const override;
249-
void print(llvm::raw_ostream &Out) const override {
250-
Out << "[fix: add missing protocol conformance]";
252+
std::string getName() const override {
253+
return "add missing protocol conformance";
251254
}
252255

256+
bool diagnose(Expr *root, const Solution &solution) const override;
257+
253258
static MissingConformance *create(ConstraintSystem &cs, Type type,
254259
ProtocolDecl *protocol,
255260
ConstraintLocator *locator);

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4908,9 +4908,7 @@ bool ConstraintSystem::recordFix(ConstraintFix *fix) {
49084908
auto &log = ctx.TypeCheckerDebug->getStream();
49094909
log.indent(solverState ? solverState->depth * 2 + 2 : 0)
49104910
<< "(attempting fix ";
4911-
fix->print(log);
4912-
log << " @";
4913-
getConstraintLocator(fix->getLocator())->dump(&ctx.SourceMgr, log);
4911+
fix->print(log, &ctx.SourceMgr);
49144912
log << ")\n";
49154913
}
49164914

lib/Sema/Constraint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm) const {
390390

391391
if (auto *fix = getFix()) {
392392
Out << ' ';
393-
fix->print(Out);
393+
fix->print(Out, sm);
394394
}
395395

396396
if (Locator) {

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3357,9 +3357,7 @@ void Solution::dump(raw_ostream &out) const {
33573357
out << "\nFixes:\n";
33583358
for (auto *fix : Fixes) {
33593359
out.indent(2);
3360-
fix->print(out);
3361-
out << " @ ";
3362-
fix->getLocator()->dump(sm, out);
3360+
fix->print(out, &ctx.SourceMgr);
33633361
out << "\n";
33643362
}
33653363
}
@@ -3554,9 +3552,7 @@ void ConstraintSystem::print(raw_ostream &out) {
35543552
out << "\nFixes:\n";
35553553
for (auto *fix : Fixes) {
35563554
out.indent(2);
3557-
fix->print(out);
3558-
out << " @ ";
3559-
fix->getLocator()->dump(&getTypeChecker().Context.SourceMgr, out);
3555+
fix->print(out, &getTypeChecker().Context.SourceMgr);
35603556
out << "\n";
35613557
}
35623558
}

0 commit comments

Comments
 (0)