Skip to content

Commit c33b726

Browse files
committed
[Diagnostics] Extract logic common to all key path invalid ref diagnostics
1 parent 2e93165 commit c33b726

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,9 +2449,8 @@ bool KeyPathSubscriptIndexHashableFailure::diagnoseAsError() {
24492449
return true;
24502450
}
24512451

2452-
bool InvalidStaticMemberRefInKeyPath::diagnoseAsError() {
2452+
SourceLoc InvalidMemberRefInKeyPath::getLoc() const {
24532453
auto *anchor = getRawAnchor();
2454-
auto loc = anchor->getLoc();
24552454

24562455
if (auto *KPE = dyn_cast<KeyPathExpr>(anchor)) {
24572456
auto *locator = getLocator();
@@ -2461,9 +2460,13 @@ bool InvalidStaticMemberRefInKeyPath::diagnoseAsError() {
24612460
});
24622461

24632462
assert(component != locator->getPath().end());
2464-
loc = KPE->getComponents()[component->getValue()].getLoc();
2463+
return KPE->getComponents()[component->getValue()].getLoc();
24652464
}
24662465

2467-
emitDiagnostic(loc, diag::expr_keypath_static_member, Member->getBaseName());
2466+
return anchor->getLoc();
2467+
}
2468+
2469+
bool InvalidStaticMemberRefInKeyPath::diagnoseAsError() {
2470+
emitDiagnostic(getLoc(), diag::expr_keypath_static_member, getName());
24682471
return true;
24692472
}

lib/Sema/CSDiagnostics.h

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,26 @@ class KeyPathSubscriptIndexHashableFailure final : public FailureDiagnostic {
10311031
bool diagnoseAsError() override;
10321032
};
10331033

1034+
class InvalidMemberRefInKeyPath : public FailureDiagnostic {
1035+
ValueDecl *Member;
1036+
1037+
public:
1038+
InvalidMemberRefInKeyPath(Expr *root, ConstraintSystem &cs, ValueDecl *member,
1039+
ConstraintLocator *locator)
1040+
: FailureDiagnostic(root, cs, locator), Member(member) {
1041+
assert(member->hasName());
1042+
assert(locator->isForKeyPathComponent());
1043+
}
1044+
1045+
DeclName getName() const { return Member->getFullName(); }
1046+
1047+
bool diagnoseAsError() override = 0;
1048+
1049+
protected:
1050+
/// Compute location of the failure for diagnostic.
1051+
SourceLoc getLoc() const;
1052+
};
1053+
10341054
/// Diagnose an attempt to reference a static member as a key path component
10351055
/// e.g.
10361056
///
@@ -1041,16 +1061,11 @@ class KeyPathSubscriptIndexHashableFailure final : public FailureDiagnostic {
10411061
///
10421062
/// _ = \S.Type.foo
10431063
/// ```
1044-
class InvalidStaticMemberRefInKeyPath final : public FailureDiagnostic {
1045-
ValueDecl *Member;
1046-
1064+
class InvalidStaticMemberRefInKeyPath final : public InvalidMemberRefInKeyPath {
10471065
public:
10481066
InvalidStaticMemberRefInKeyPath(Expr *root, ConstraintSystem &cs,
10491067
ValueDecl *member, ConstraintLocator *locator)
1050-
: FailureDiagnostic(root, cs, locator), Member(member) {
1051-
assert(member->hasName());
1052-
assert(locator->isForKeyPathComponent());
1053-
}
1068+
: InvalidMemberRefInKeyPath(root, cs, member, locator) {}
10541069

10551070
bool diagnoseAsError() override;
10561071
};

0 commit comments

Comments
 (0)