Skip to content

Commit 294c05b

Browse files
committed
[Diagnostics] Split FailureDiagnostic::diagnose(bool asNote) into diagnoseAs{Error, Note}
1 parent 57b9ffd commit 294c05b

File tree

2 files changed

+53
-53
lines changed

2 files changed

+53
-53
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ using namespace constraints;
2929

3030
FailureDiagnostic::~FailureDiagnostic() {}
3131

32+
bool FailureDiagnostic::diagnose(bool asNote) {
33+
return asNote ? diagnoseAsNote() : diagnoseAsError();
34+
}
35+
36+
bool FailureDiagnostic::diagnoseAsNote() {
37+
return false;
38+
}
39+
3240
std::pair<Expr *, bool> FailureDiagnostic::computeAnchor() const {
3341
auto &cs = getConstraintSystem();
3442

@@ -116,21 +124,14 @@ const DeclContext *RequirementFailure::getRequirementDC() const {
116124
return AffectedDecl->getAsGenericContext();
117125
}
118126

119-
bool RequirementFailure::diagnose(bool asNote) {
127+
bool RequirementFailure::diagnoseAsError() {
120128
if (!canDiagnoseFailure())
121129
return false;
122130

123131
auto *anchor = getAnchor();
124132
const auto *reqDC = getRequirementDC();
125133
auto *genericCtx = AffectedDecl->getAsGenericContext();
126134

127-
if (asNote) {
128-
const auto &req = getRequirement();
129-
emitDiagnostic(reqDC->getAsDecl(), getDiagnosticAsNote(), getLHS(),
130-
getRHS(), req.getFirstType(), req.getSecondType(), "");
131-
return true;
132-
}
133-
134135
if (reqDC != genericCtx) {
135136
auto *NTD = reqDC->getSelfNominalTypeDecl();
136137
emitDiagnostic(anchor->getLoc(), getDiagnosticInRereference(),
@@ -147,6 +148,15 @@ bool RequirementFailure::diagnose(bool asNote) {
147148
return true;
148149
}
149150

151+
bool RequirementFailure::diagnoseAsNote() {
152+
const auto &req = getRequirement();
153+
const auto *reqDC = getRequirementDC();
154+
155+
emitDiagnostic(reqDC->getAsDecl(), getDiagnosticAsNote(), getLHS(), getRHS(),
156+
req.getFirstType(), req.getSecondType(), "");
157+
return true;
158+
}
159+
150160
void RequirementFailure::emitRequirementNote(const Decl *anchor) const {
151161
auto &req = getRequirement();
152162

@@ -166,13 +176,10 @@ void RequirementFailure::emitRequirementNote(const Decl *anchor) const {
166176
req.getFirstType(), getLHS(), req.getSecondType(), getRHS());
167177
}
168178

169-
bool MissingConformanceFailure::diagnose(bool asNote) {
179+
bool MissingConformanceFailure::diagnoseAsError() {
170180
if (!canDiagnoseFailure())
171181
return false;
172182

173-
if (asNote)
174-
return RequirementFailure::diagnose(asNote);
175-
176183
auto *anchor = getAnchor();
177184
auto ownerType = getOwnerType();
178185
auto nonConformingType = getLHS();
@@ -230,25 +237,18 @@ bool MissingConformanceFailure::diagnose(bool asNote) {
230237

231238
// If none of the special cases could be diagnosed,
232239
// let's fallback to the most general diagnostic.
233-
return RequirementFailure::diagnose();
240+
return RequirementFailure::diagnoseAsError();
234241
}
235242

236-
bool LabelingFailure::diagnose(bool asNote) {
237-
if (asNote)
238-
return false;
239-
243+
bool LabelingFailure::diagnoseAsError() {
240244
auto &cs = getConstraintSystem();
241245
auto *call = cast<CallExpr>(getAnchor());
242246
return diagnoseArgumentLabelError(cs.getASTContext(), call->getArg(),
243247
CorrectLabels,
244248
isa<SubscriptExpr>(call->getFn()));
245249
}
246250

247-
bool NoEscapeFuncToTypeConversionFailure::diagnose(bool asNote) {
248-
// Can't diagnose this problem as a note at the moment.
249-
if (asNote)
250-
return false;
251-
251+
bool NoEscapeFuncToTypeConversionFailure::diagnoseAsError() {
252252
auto *anchor = getAnchor();
253253

254254
if (ConvertTo) {
@@ -271,9 +271,8 @@ bool NoEscapeFuncToTypeConversionFailure::diagnose(bool asNote) {
271271
return true;
272272
}
273273

274-
bool MissingForcedDowncastFailure::diagnose(bool asNote) {
275-
// Can't diagnose this problem as a note at the moment.
276-
if (hasComplexLocator() || asNote)
274+
bool MissingForcedDowncastFailure::diagnoseAsError() {
275+
if (hasComplexLocator())
277276
return false;
278277

279278
auto &TC = getTypeChecker();
@@ -313,9 +312,8 @@ bool MissingForcedDowncastFailure::diagnose(bool asNote) {
313312
}
314313
}
315314

316-
bool MissingAddressOfFailure::diagnose(bool asNote) {
317-
// Can't diagnose this problem as a note at the moment.
318-
if (hasComplexLocator() || asNote)
315+
bool MissingAddressOfFailure::diagnoseAsError() {
316+
if (hasComplexLocator())
319317
return false;
320318

321319
auto *anchor = getAnchor();
@@ -325,9 +323,8 @@ bool MissingAddressOfFailure::diagnose(bool asNote) {
325323
return true;
326324
}
327325

328-
bool MissingExplicitConversionFailure::diagnose(bool asNote) {
329-
// Can't diagnose this problem as a note at the moment.
330-
if (hasComplexLocator() || asNote)
326+
bool MissingExplicitConversionFailure::diagnoseAsError() {
327+
if (hasComplexLocator())
331328
return false;
332329

333330
auto *DC = getDC();
@@ -383,9 +380,8 @@ bool MissingExplicitConversionFailure::diagnose(bool asNote) {
383380
return true;
384381
}
385382

386-
bool MemberAccessOnOptionalBaseFailure::diagnose(bool asNote) {
387-
// Can't diagnose this problem as a note at the moment.
388-
if (hasComplexLocator() || asNote)
383+
bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() {
384+
if (hasComplexLocator())
389385
return false;
390386

391387
auto *anchor = getAnchor();
@@ -537,9 +533,8 @@ static bool diagnoseUnwrap(ConstraintSystem &CS, Expr *expr, Type type) {
537533
return true;
538534
}
539535

540-
bool MissingOptionalUnwrapFailure::diagnose(bool asNote) {
541-
// Can't diagnose this problem as a note at the moment.
542-
if (hasComplexLocator() || asNote)
536+
bool MissingOptionalUnwrapFailure::diagnoseAsError() {
537+
if (hasComplexLocator())
543538
return false;
544539

545540
auto *anchor = getAnchor();
@@ -555,11 +550,7 @@ bool MissingOptionalUnwrapFailure::diagnose(bool asNote) {
555550
return true;
556551
}
557552

558-
bool RValueTreatedAsLValueFailure::diagnose(bool asNote) {
559-
// Can't diagnose this problem as a note at the moment.
560-
if (asNote)
561-
return false;
562-
553+
bool RValueTreatedAsLValueFailure::diagnoseAsError() {
563554
Diag<StringRef> subElementDiagID;
564555
Diag<Type> rvalueDiagID;
565556
Expr *diagExpr = getLocator()->getAnchor();

lib/Sema/CSDiagnostics.h

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ class FailureDiagnostic {
5959
///
6060
/// \returns true If the problem has been successfully diagnosed
6161
/// and diagnostic message emitted, false otherwise.
62-
virtual bool diagnose(bool asNote = false) = 0;
62+
bool diagnose(bool asNote = false);
63+
64+
/// Try to produce an error diagnostic for the problem at hand.
65+
virtual bool diagnoseAsError() = 0;
66+
67+
/// Instead of producing an error diagnostic, attempt to
68+
/// produce a "note" to complement some other diagnostic
69+
/// e.g. ambiguity error.
70+
virtual bool diagnoseAsNote();
6371

6472
ConstraintSystem &getConstraintSystem() const {
6573
return CS;
@@ -174,7 +182,8 @@ class RequirementFailure : public FailureDiagnostic {
174182
virtual Type getLHS() const = 0;
175183
virtual Type getRHS() const = 0;
176184

177-
bool diagnose(bool asNote = false) override;
185+
bool diagnoseAsError() override;
186+
bool diagnoseAsNote() override;
178187

179188
protected:
180189
/// Retrieve declaration contextual where current
@@ -226,7 +235,7 @@ class MissingConformanceFailure final : public RequirementFailure {
226235
: RequirementFailure(expr, cs, locator),
227236
NonConformingType(conformance.first), Protocol(conformance.second) {}
228237

229-
bool diagnose(bool asNote = false) override;
238+
bool diagnoseAsError() override;
230239

231240
private:
232241
/// The type which was expected, by one of the generic requirements,
@@ -344,7 +353,7 @@ class LabelingFailure final : public FailureDiagnostic {
344353
ArrayRef<Identifier> labels)
345354
: FailureDiagnostic(nullptr, cs, locator), CorrectLabels(labels) {}
346355

347-
bool diagnose(bool asNote = false) override;
356+
bool diagnoseAsError() override;
348357
};
349358

350359
/// Diagnose errors related to converting function type which
@@ -358,7 +367,7 @@ class NoEscapeFuncToTypeConversionFailure final : public FailureDiagnostic {
358367
Type toType = Type())
359368
: FailureDiagnostic(expr, cs, locator), ConvertTo(toType) {}
360369

361-
bool diagnose(bool asNote = false) override;
370+
bool diagnoseAsError() override;
362371
};
363372

364373
class MissingForcedDowncastFailure final : public FailureDiagnostic {
@@ -367,7 +376,7 @@ class MissingForcedDowncastFailure final : public FailureDiagnostic {
367376
ConstraintLocator *locator)
368377
: FailureDiagnostic(expr, cs, locator) {}
369378

370-
bool diagnose(bool asNote = false) override;
379+
bool diagnoseAsError() override;
371380
};
372381

373382
/// Diagnose failures related to passing value of some type
@@ -378,7 +387,7 @@ class MissingAddressOfFailure final : public FailureDiagnostic {
378387
ConstraintLocator *locator)
379388
: FailureDiagnostic(expr, cs, locator) {}
380389

381-
bool diagnose(bool asNote = false) override;
390+
bool diagnoseAsError() override;
382391
};
383392

384393
/// Diagnose failures related attempt to implicitly convert types which
@@ -392,7 +401,7 @@ class MissingExplicitConversionFailure final : public FailureDiagnostic {
392401
ConstraintLocator *locator, Type toType)
393402
: FailureDiagnostic(expr, cs, locator), ConvertingTo(toType) {}
394403

395-
bool diagnose(bool asNote = false) override;
404+
bool diagnoseAsError() override;
396405

397406
private:
398407
bool exprNeedsParensBeforeAddingAs(Expr *expr) {
@@ -433,7 +442,7 @@ class MemberAccessOnOptionalBaseFailure final : public FailureDiagnostic {
433442
: FailureDiagnostic(expr, cs, locator), Member(memberName),
434443
ResultTypeIsOptional(resultOptional) {}
435444

436-
bool diagnose(bool asNote = false) override;
445+
bool diagnoseAsError() override;
437446
};
438447

439448
/// Diagnose failures related to use of the unwrapped optional types,
@@ -444,7 +453,7 @@ class MissingOptionalUnwrapFailure final : public FailureDiagnostic {
444453
ConstraintLocator *locator)
445454
: FailureDiagnostic(expr, cs, locator) {}
446455

447-
bool diagnose(bool asNote = false) override;
456+
bool diagnoseAsError() override;
448457
};
449458

450459
/// Diagnose errors associated with rvalues in positions
@@ -455,7 +464,7 @@ class RValueTreatedAsLValueFailure final : public FailureDiagnostic {
455464
RValueTreatedAsLValueFailure(ConstraintSystem &cs, ConstraintLocator *locator)
456465
: FailureDiagnostic(nullptr, cs, locator) {}
457466

458-
bool diagnose(bool asNote = false) override;
467+
bool diagnoseAsError() override;
459468
};
460469

461470
} // end namespace constraints

0 commit comments

Comments
 (0)