Skip to content

Commit 9f6891a

Browse files
committed
[Diagnostics] Add asNote flag to FailureDiagnostic
The flag is used to indicate that caller is interested in diagnostic to be a note instead of an error, it's useful when trying to diagnose ambiguities.
1 parent dd2f3b4 commit 9f6891a

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ const DeclContext *RequirementFailure::getRequirementDC() const {
116116
return AffectedDecl->getAsGenericContext();
117117
}
118118

119-
bool RequirementFailure::diagnose() {
120-
if (!canDiagnoseFailure())
119+
bool RequirementFailure::diagnose(bool asNote) {
120+
if (!canDiagnoseFailure() || asNote)
121121
return false;
122122

123123
auto *anchor = getAnchor();
@@ -159,8 +159,8 @@ void RequirementFailure::emitRequirementNote(const Decl *anchor) const {
159159
req.getFirstType(), getLHS(), req.getSecondType(), getRHS());
160160
}
161161

162-
bool MissingConformanceFailure::diagnose() {
163-
if (!canDiagnoseFailure())
162+
bool MissingConformanceFailure::diagnose(bool asNote) {
163+
if (!canDiagnoseFailure() || asNote)
164164
return false;
165165

166166
auto *anchor = getAnchor();
@@ -223,15 +223,19 @@ bool MissingConformanceFailure::diagnose() {
223223
return RequirementFailure::diagnose();
224224
}
225225

226-
bool LabelingFailure::diagnose() {
226+
bool LabelingFailure::diagnose(bool asNote) {
227227
auto &cs = getConstraintSystem();
228228
auto *call = cast<CallExpr>(getAnchor());
229229
return diagnoseArgumentLabelError(cs.getASTContext(), call->getArg(),
230230
CorrectLabels,
231231
isa<SubscriptExpr>(call->getFn()));
232232
}
233233

234-
bool NoEscapeFuncToTypeConversionFailure::diagnose() {
234+
bool NoEscapeFuncToTypeConversionFailure::diagnose(bool asNote) {
235+
// Can't diagnose this problem as a note at the moment.
236+
if (asNote)
237+
return false;
238+
235239
auto *anchor = getAnchor();
236240

237241
if (ConvertTo) {
@@ -254,8 +258,9 @@ bool NoEscapeFuncToTypeConversionFailure::diagnose() {
254258
return true;
255259
}
256260

257-
bool MissingForcedDowncastFailure::diagnose() {
258-
if (hasComplexLocator())
261+
bool MissingForcedDowncastFailure::diagnose(bool asNote) {
262+
// Can't diagnose this problem as a note at the moment.
263+
if (hasComplexLocator() || asNote)
259264
return false;
260265

261266
auto &TC = getTypeChecker();
@@ -295,8 +300,9 @@ bool MissingForcedDowncastFailure::diagnose() {
295300
}
296301
}
297302

298-
bool MissingAddressOfFailure::diagnose() {
299-
if (hasComplexLocator())
303+
bool MissingAddressOfFailure::diagnose(bool asNote) {
304+
// Can't diagnose this problem as a note at the moment.
305+
if (hasComplexLocator() || asNote)
300306
return false;
301307

302308
auto *anchor = getAnchor();
@@ -306,8 +312,9 @@ bool MissingAddressOfFailure::diagnose() {
306312
return true;
307313
}
308314

309-
bool MissingExplicitConversionFailure::diagnose() {
310-
if (hasComplexLocator())
315+
bool MissingExplicitConversionFailure::diagnose(bool asNote) {
316+
// Can't diagnose this problem as a note at the moment.
317+
if (hasComplexLocator() || asNote)
311318
return false;
312319

313320
auto *DC = getDC();
@@ -363,8 +370,9 @@ bool MissingExplicitConversionFailure::diagnose() {
363370
return true;
364371
}
365372

366-
bool MemberAccessOnOptionalBaseFailure::diagnose() {
367-
if (hasComplexLocator())
373+
bool MemberAccessOnOptionalBaseFailure::diagnose(bool asNote) {
374+
// Can't diagnose this problem as a note at the moment.
375+
if (hasComplexLocator() || asNote)
368376
return false;
369377

370378
auto *anchor = getAnchor();
@@ -516,8 +524,9 @@ static bool diagnoseUnwrap(ConstraintSystem &CS, Expr *expr, Type type) {
516524
return true;
517525
}
518526

519-
bool MissingOptionalUnwrapFailure::diagnose() {
520-
if (hasComplexLocator())
527+
bool MissingOptionalUnwrapFailure::diagnose(bool asNote) {
528+
// Can't diagnose this problem as a note at the moment.
529+
if (hasComplexLocator() || asNote)
521530
return false;
522531

523532
auto *anchor = getAnchor();
@@ -533,7 +542,11 @@ bool MissingOptionalUnwrapFailure::diagnose() {
533542
return true;
534543
}
535544

536-
bool RValueTreatedAsLValueFailure::diagnose() {
545+
bool RValueTreatedAsLValueFailure::diagnose(bool asNote) {
546+
// Can't diagnose this problem as a note at the moment.
547+
if (asNote)
548+
return false;
549+
537550
Diag<StringRef> subElementDiagID;
538551
Diag<Type> rvalueDiagID;
539552
Expr *diagExpr = getLocator()->getAnchor();

lib/Sema/CSDiagnostics.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ class FailureDiagnostic {
5454
/// failure location, types and declarations deduced by
5555
/// constraint system, and other auxiliary information.
5656
///
57+
/// \param asNote In ambiguity cases it's beneficial to
58+
/// produce diagnostic as a note instead of an error if possible.
59+
///
5760
/// \returns true If the problem has been successfully diagnosed
5861
/// and diagnostic message emitted, false otherwise.
59-
virtual bool diagnose() = 0;
62+
virtual bool diagnose(bool asNote = false) = 0;
6063

6164
ConstraintSystem &getConstraintSystem() const {
6265
return CS;
@@ -170,7 +173,7 @@ class RequirementFailure : public FailureDiagnostic {
170173
virtual Type getLHS() const = 0;
171174
virtual Type getRHS() const = 0;
172175

173-
bool diagnose() override;
176+
bool diagnose(bool asNote = false) override;
174177

175178
protected:
176179
/// Retrieve declaration contextual where current
@@ -221,7 +224,7 @@ class MissingConformanceFailure final : public RequirementFailure {
221224
: RequirementFailure(expr, cs, locator),
222225
NonConformingType(conformance.first), Protocol(conformance.second) {}
223226

224-
bool diagnose() override;
227+
bool diagnose(bool asNote = false) override;
225228

226229
private:
227230
/// The type which was expected, by one of the generic requirements,
@@ -327,7 +330,7 @@ class LabelingFailure final : public FailureDiagnostic {
327330
ArrayRef<Identifier> labels)
328331
: FailureDiagnostic(nullptr, cs, locator), CorrectLabels(labels) {}
329332

330-
bool diagnose() override;
333+
bool diagnose(bool asNote = false) override;
331334
};
332335

333336
/// Diagnose errors related to converting function type which
@@ -341,7 +344,7 @@ class NoEscapeFuncToTypeConversionFailure final : public FailureDiagnostic {
341344
Type toType = Type())
342345
: FailureDiagnostic(expr, cs, locator), ConvertTo(toType) {}
343346

344-
bool diagnose() override;
347+
bool diagnose(bool asNote = false) override;
345348
};
346349

347350
class MissingForcedDowncastFailure final : public FailureDiagnostic {
@@ -350,7 +353,7 @@ class MissingForcedDowncastFailure final : public FailureDiagnostic {
350353
ConstraintLocator *locator)
351354
: FailureDiagnostic(expr, cs, locator) {}
352355

353-
bool diagnose() override;
356+
bool diagnose(bool asNote = false) override;
354357
};
355358

356359
/// Diagnose failures related to passing value of some type
@@ -361,7 +364,7 @@ class MissingAddressOfFailure final : public FailureDiagnostic {
361364
ConstraintLocator *locator)
362365
: FailureDiagnostic(expr, cs, locator) {}
363366

364-
bool diagnose() override;
367+
bool diagnose(bool asNote = false) override;
365368
};
366369

367370
/// Diagnose failures related attempt to implicitly convert types which
@@ -375,7 +378,7 @@ class MissingExplicitConversionFailure final : public FailureDiagnostic {
375378
ConstraintLocator *locator, Type toType)
376379
: FailureDiagnostic(expr, cs, locator), ConvertingTo(toType) {}
377380

378-
bool diagnose() override;
381+
bool diagnose(bool asNote = false) override;
379382

380383
private:
381384
bool exprNeedsParensBeforeAddingAs(Expr *expr) {
@@ -416,7 +419,7 @@ class MemberAccessOnOptionalBaseFailure final : public FailureDiagnostic {
416419
: FailureDiagnostic(expr, cs, locator), Member(memberName),
417420
ResultTypeIsOptional(resultOptional) {}
418421

419-
bool diagnose() override;
422+
bool diagnose(bool asNote = false) override;
420423
};
421424

422425
/// Diagnose failures related to use of the unwrapped optional types,
@@ -427,7 +430,7 @@ class MissingOptionalUnwrapFailure final : public FailureDiagnostic {
427430
ConstraintLocator *locator)
428431
: FailureDiagnostic(expr, cs, locator) {}
429432

430-
bool diagnose() override;
433+
bool diagnose(bool asNote = false) override;
431434
};
432435

433436
/// Diagnose errors associated with rvalues in positions
@@ -438,7 +441,7 @@ class RValueTreatedAsLValueFailure final : public FailureDiagnostic {
438441
RValueTreatedAsLValueFailure(ConstraintSystem &cs, ConstraintLocator *locator)
439442
: FailureDiagnostic(nullptr, cs, locator) {}
440443

441-
bool diagnose() override;
444+
bool diagnose(bool asNote = false) override;
442445
};
443446

444447
} // end namespace constraints

0 commit comments

Comments
 (0)