Skip to content

Commit 17d46d1

Browse files
committed
[Diagnostics] NFC: Make sure that requirement kind matches locator
When constructing requirement failures make sure that requirement kind provided by the caller matches information about requirement stored in the locator.
1 parent 290bd04 commit 17d46d1

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lib/Sema/CSDiagnostics.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,23 @@ class RequirementFailure : public FailureDiagnostic {
160160
const ApplyExpr *Apply = nullptr;
161161

162162
public:
163-
RequirementFailure(Expr *expr, ConstraintSystem &cs,
163+
RequirementFailure(ConstraintSystem &cs, Expr *expr, RequirementKind kind,
164164
ConstraintLocator *locator)
165165
: FailureDiagnostic(expr, cs, locator), AffectedDecl(getDeclRef()) {
166+
assert(locator);
166167
assert(AffectedDecl);
168+
169+
auto path = locator->getPath();
170+
assert(!path.empty());
171+
172+
auto &last = path.back();
173+
assert(last.getKind() == ConstraintLocator::TypeParameterRequirement);
174+
assert(static_cast<RequirementKind>(last.getValue2()) == kind);
175+
176+
// It's possible sometimes not to have no base expression.
177+
if (!expr)
178+
return;
179+
167180
auto *anchor = getAnchor();
168181
expr->forEachChildExpr([&](Expr *subExpr) -> Expr * {
169182
auto *AE = dyn_cast<ApplyExpr>(subExpr);
@@ -246,7 +259,7 @@ class MissingConformanceFailure final : public RequirementFailure {
246259
MissingConformanceFailure(Expr *expr, ConstraintSystem &cs,
247260
ConstraintLocator *locator,
248261
std::pair<Type, ProtocolDecl *> conformance)
249-
: RequirementFailure(expr, cs, locator),
262+
: RequirementFailure(cs, expr, RequirementKind::Conformance, locator),
250263
NonConformingType(conformance.first), Protocol(conformance.second) {}
251264

252265
bool diagnoseAsError() override;
@@ -294,7 +307,8 @@ class SameTypeRequirementFailure final : public RequirementFailure {
294307
public:
295308
SameTypeRequirementFailure(Expr *expr, ConstraintSystem &cs, Type lhs,
296309
Type rhs, ConstraintLocator *locator)
297-
: RequirementFailure(expr, cs, locator), LHS(lhs), RHS(rhs) {}
310+
: RequirementFailure(cs, expr, RequirementKind::SameType, locator),
311+
LHS(lhs), RHS(rhs) {}
298312

299313
Type getLHS() const override { return LHS; }
300314
Type getRHS() const override { return RHS; }
@@ -332,7 +346,8 @@ class SuperclassRequirementFailure final : public RequirementFailure {
332346
public:
333347
SuperclassRequirementFailure(Expr *expr, ConstraintSystem &cs, Type lhs,
334348
Type rhs, ConstraintLocator *locator)
335-
: RequirementFailure(expr, cs, locator), LHS(lhs), RHS(rhs) {}
349+
: RequirementFailure(cs, expr, RequirementKind::Superclass, locator),
350+
LHS(lhs), RHS(rhs) {}
336351

337352
Type getLHS() const override { return LHS; }
338353
Type getRHS() const override { return RHS; }

0 commit comments

Comments
 (0)