Skip to content

Commit 0a6b810

Browse files
committed
[Diagnostics] NFC: Store requirement types directly in ReqirementFailure
Since all of the specific diagnostics which constitute requirement failure now operate on types, their interfaces could be simplified and associated requirement types could be stored in RequirementFailure.
1 parent effd0d0 commit 0a6b810

File tree

2 files changed

+17
-34
lines changed

2 files changed

+17
-34
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ bool RequirementFailure::diagnoseAsError() {
436436
const auto *reqDC = getRequirementDC();
437437
auto *genericCtx = getGenericContext();
438438

439-
auto lhs = resolveType(getLHS());
440-
auto rhs = resolveType(getRHS());
439+
auto lhs = getLHS();
440+
auto rhs = getRHS();
441441

442442
if (auto *OTD = dyn_cast<OpaqueTypeDecl>(AffectedDecl)) {
443443
auto *namingDecl = OTD->getNamingDecl();

lib/Sema/CSDiagnostics.h

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,17 @@ class RequirementFailure : public FailureDiagnostic {
222222
/// to diagnose failures related to arguments.
223223
const ApplyExpr *Apply = nullptr;
224224

225+
/// Types associated with requirement constraint this
226+
/// failure originates from.
227+
Type LHS, RHS;
228+
225229
public:
226230
RequirementFailure(ConstraintSystem &cs, Expr *expr, RequirementKind kind,
227-
ConstraintLocator *locator)
231+
Type lhs, Type rhs, ConstraintLocator *locator)
228232
: FailureDiagnostic(expr, cs, locator),
229233
Conformance(getConformanceForConditionalReq(locator)),
230-
Signature(getSignature(locator)), AffectedDecl(getDeclRef()) {
234+
Signature(getSignature(locator)), AffectedDecl(getDeclRef()),
235+
LHS(resolveType(lhs)), RHS(resolveType(rhs)) {
231236
assert(locator);
232237
assert(isConditional() || Signature);
233238
assert(AffectedDecl);
@@ -267,8 +272,8 @@ class RequirementFailure : public FailureDiagnostic {
267272
/// Generic requirement associated with the failure.
268273
const Requirement &getRequirement() const;
269274

270-
virtual Type getLHS() const = 0;
271-
virtual Type getRHS() const = 0;
275+
Type getLHS() const { return LHS; }
276+
Type getRHS() const { return RHS; }
272277

273278
bool diagnoseAsError() override;
274279
bool diagnoseAsNote() override;
@@ -358,27 +363,15 @@ class RequirementFailure : public FailureDiagnostic {
358363
/// foo(S())
359364
/// ```
360365
class MissingConformanceFailure final : public RequirementFailure {
361-
Type NonConformingType;
362-
Type ProtocolType;
363-
364366
public:
365367
MissingConformanceFailure(Expr *expr, ConstraintSystem &cs,
366368
ConstraintLocator *locator,
367369
std::pair<Type, Type> conformance)
368-
: RequirementFailure(cs, expr, RequirementKind::Conformance, locator),
369-
NonConformingType(conformance.first), ProtocolType(conformance.second) {
370-
}
370+
: RequirementFailure(cs, expr, RequirementKind::Conformance,
371+
conformance.first, conformance.second, locator) {}
371372

372373
bool diagnoseAsError() override;
373374

374-
private:
375-
/// The type which was expected, by one of the generic requirements,
376-
/// to conform to associated protocol.
377-
Type getLHS() const override { return NonConformingType; }
378-
379-
/// The protocol generic requirement expected associated type to conform to.
380-
Type getRHS() const override { return ProtocolType; }
381-
382375
protected:
383376
DiagOnDecl getDiagnosticOnDecl() const override {
384377
return diag::type_does_not_conform_decl_owner;
@@ -448,16 +441,11 @@ class GenericArgumentsMismatchFailure final : public FailureDiagnostic {
448441
///
449442
/// `S.T` is not the same type as `Int`, which is required by `foo`.
450443
class SameTypeRequirementFailure final : public RequirementFailure {
451-
Type LHS, RHS;
452-
453444
public:
454445
SameTypeRequirementFailure(Expr *expr, ConstraintSystem &cs, Type lhs,
455446
Type rhs, ConstraintLocator *locator)
456-
: RequirementFailure(cs, expr, RequirementKind::SameType, locator),
457-
LHS(lhs), RHS(rhs) {}
458-
459-
Type getLHS() const override { return LHS; }
460-
Type getRHS() const override { return RHS; }
447+
: RequirementFailure(cs, expr, RequirementKind::SameType, lhs, rhs,
448+
locator) {}
461449

462450
protected:
463451
DiagOnDecl getDiagnosticOnDecl() const override {
@@ -487,16 +475,11 @@ class SameTypeRequirementFailure final : public RequirementFailure {
487475
///
488476
/// `A` is not the superclass of `B`, which is required by `foo<T>`.
489477
class SuperclassRequirementFailure final : public RequirementFailure {
490-
Type LHS, RHS;
491-
492478
public:
493479
SuperclassRequirementFailure(Expr *expr, ConstraintSystem &cs, Type lhs,
494480
Type rhs, ConstraintLocator *locator)
495-
: RequirementFailure(cs, expr, RequirementKind::Superclass, locator),
496-
LHS(lhs), RHS(rhs) {}
497-
498-
Type getLHS() const override { return LHS; }
499-
Type getRHS() const override { return RHS; }
481+
: RequirementFailure(cs, expr, RequirementKind::Superclass, lhs, rhs,
482+
locator) {}
500483

501484
protected:
502485
DiagOnDecl getDiagnosticOnDecl() const override {

0 commit comments

Comments
 (0)