Skip to content

Commit 1a95ab7

Browse files
committed
[Sema] Update diagnostics.
1 parent abc96ce commit 1a95ab7

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,8 @@ ERROR(expr_keypath_mutating_getter,none,
663663
"which has a mutating getter",
664664
(const ValueDecl *, bool))
665665
ERROR(expr_keypath_static_member,none,
666-
"%select{key path|dynamic key path member lookup}1 cannot refer to static member %0",
667-
(const ValueDecl *, bool))
666+
"static member %0 cannot be used on instance of type %1",
667+
(const ValueDecl *, Type))
668668
ERROR(expr_keypath_enum_case,none,
669669
"%select{key path|dynamic key path member lookup}1 cannot refer to enum case %0",
670670
(const ValueDecl *, bool))

include/swift/Sema/CSFix.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,11 +2023,13 @@ class AllowInvalidRefInKeyPath final : public ConstraintFix {
20232023
} Kind;
20242024

20252025
ValueDecl *Member;
2026+
Type BaseType;
20262027

20272028
AllowInvalidRefInKeyPath(ConstraintSystem &cs, RefKind kind,
2028-
ValueDecl *member, ConstraintLocator *locator)
2029+
ValueDecl *member, ConstraintLocator *locator,
2030+
Type baseType = Type())
20292031
: ConstraintFix(cs, FixKind::AllowInvalidRefInKeyPath, locator),
2030-
Kind(kind), Member(member) {}
2032+
Kind(kind), Member(member), BaseType(baseType) {}
20312033

20322034
public:
20332035
std::string getName() const override {
@@ -2064,7 +2066,8 @@ class AllowInvalidRefInKeyPath final : public ConstraintFix {
20642066
private:
20652067
static AllowInvalidRefInKeyPath *create(ConstraintSystem &cs, RefKind kind,
20662068
ValueDecl *member,
2067-
ConstraintLocator *locator);
2069+
ConstraintLocator *locator,
2070+
Type baseType = Type());
20682071
};
20692072

20702073
class RemoveReturn final : public ContextualMismatch {

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4975,7 +4975,8 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
49754975
// components, let's provide a tailored diagnostic and return because
49764976
// that is unsupported so there is no fix-it.
49774977
if (locator->isInKeyPathComponent()) {
4978-
InvalidStaticMemberRefInKeyPath failure(getSolution(), Member, locator);
4978+
InvalidStaticMemberRefInKeyPath failure(getSolution(), BaseType, Member,
4979+
locator);
49794980
return failure.diagnoseAsError();
49804981
}
49814982

@@ -6266,8 +6267,7 @@ SourceLoc InvalidMemberRefInKeyPath::getLoc() const {
62666267
}
62676268

62686269
bool InvalidStaticMemberRefInKeyPath::diagnoseAsError() {
6269-
emitDiagnostic(diag::expr_keypath_static_member, getMember(),
6270-
isForKeyPathDynamicMemberLookup());
6270+
emitDiagnostic(diag::expr_keypath_static_member, getMember(), getBaseType());
62716271
return true;
62726272
}
62736273

lib/Sema/CSDiagnostics.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,20 +1749,25 @@ class InvalidMemberRefInKeyPath : public FailureDiagnostic {
17491749
};
17501750

17511751
/// Diagnose an attempt to reference a static member as a key path component
1752-
/// e.g.
1752+
/// without .Type e.g.
17531753
///
17541754
/// ```swift
17551755
/// struct S {
17561756
/// static var foo: Int = 42
17571757
/// }
17581758
///
1759-
/// _ = \S.Type.foo
1759+
/// _ = \S.foo
17601760
/// ```
17611761
class InvalidStaticMemberRefInKeyPath final : public InvalidMemberRefInKeyPath {
1762+
Type BaseType;
1763+
17621764
public:
1763-
InvalidStaticMemberRefInKeyPath(const Solution &solution, ValueDecl *member,
1764-
ConstraintLocator *locator)
1765-
: InvalidMemberRefInKeyPath(solution, member, locator) {}
1765+
InvalidStaticMemberRefInKeyPath(const Solution &solution, Type baseType,
1766+
ValueDecl *member, ConstraintLocator *locator)
1767+
: InvalidMemberRefInKeyPath(solution, member, locator),
1768+
BaseType(baseType->getRValueType()) {}
1769+
1770+
Type getBaseType() const { return BaseType; }
17661771

17671772
bool diagnoseAsError() override;
17681773
};

lib/Sema/CSFix.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,8 @@ bool AllowInvalidRefInKeyPath::diagnose(const Solution &solution,
12061206
bool asNote) const {
12071207
switch (Kind) {
12081208
case RefKind::StaticMember: {
1209-
InvalidStaticMemberRefInKeyPath failure(solution, Member, getLocator());
1209+
InvalidStaticMemberRefInKeyPath failure(solution, BaseType, Member,
1210+
getLocator());
12101211
return failure.diagnose(asNote);
12111212
}
12121213

@@ -1286,10 +1287,10 @@ AllowInvalidRefInKeyPath::forRef(ConstraintSystem &cs, ValueDecl *member,
12861287

12871288
AllowInvalidRefInKeyPath *
12881289
AllowInvalidRefInKeyPath::create(ConstraintSystem &cs, RefKind kind,
1289-
ValueDecl *member,
1290-
ConstraintLocator *locator) {
1290+
ValueDecl *member, ConstraintLocator *locator,
1291+
Type baseType) {
12911292
return new (cs.getAllocator())
1292-
AllowInvalidRefInKeyPath(cs, kind, member, locator);
1293+
AllowInvalidRefInKeyPath(cs, kind, member, locator, baseType);
12931294
}
12941295

12951296
bool RemoveAddressOf::diagnose(const Solution &solution, bool asNote) const {

0 commit comments

Comments
 (0)