Skip to content

Commit d384872

Browse files
committed
[Sema] Update diagnostics.
1 parent a57140e commit d384872

File tree

6 files changed

+38
-29
lines changed

6 files changed

+38
-29
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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,11 +2021,12 @@ class AllowInvalidRefInKeyPath final : public ConstraintFix {
20212021
} Kind;
20222022

20232023
ValueDecl *Member;
2024+
Type BaseType;
20242025

2025-
AllowInvalidRefInKeyPath(ConstraintSystem &cs, RefKind kind,
2026+
AllowInvalidRefInKeyPath(ConstraintSystem &cs, Type baseType, RefKind kind,
20262027
ValueDecl *member, ConstraintLocator *locator)
20272028
: ConstraintFix(cs, FixKind::AllowInvalidRefInKeyPath, locator),
2028-
Kind(kind), Member(member) {}
2029+
Kind(kind), Member(member), BaseType(baseType) {}
20292030

20302031
public:
20312032
std::string getName() const override {
@@ -2048,8 +2049,9 @@ class AllowInvalidRefInKeyPath final : public ConstraintFix {
20482049
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override;
20492050

20502051
/// Determine whether give reference requires a fix and produce one.
2051-
static AllowInvalidRefInKeyPath *
2052-
forRef(ConstraintSystem &cs, ValueDecl *member, ConstraintLocator *locator);
2052+
static AllowInvalidRefInKeyPath *forRef(ConstraintSystem &cs, Type baseType,
2053+
ValueDecl *member,
2054+
ConstraintLocator *locator);
20532055

20542056
bool isEqual(const ConstraintFix *other) const;
20552057

@@ -2058,8 +2060,8 @@ class AllowInvalidRefInKeyPath final : public ConstraintFix {
20582060
}
20592061

20602062
private:
2061-
static AllowInvalidRefInKeyPath *create(ConstraintSystem &cs, RefKind kind,
2062-
ValueDecl *member,
2063+
static AllowInvalidRefInKeyPath *create(ConstraintSystem &cs, Type baseType,
2064+
RefKind kind, ValueDecl *member,
20632065
ConstraintLocator *locator);
20642066
};
20652067

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: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,42 +1249,43 @@ bool AllowInvalidRefInKeyPath::isEqual(const ConstraintFix *other) const {
12491249
}
12501250

12511251
AllowInvalidRefInKeyPath *
1252-
AllowInvalidRefInKeyPath::forRef(ConstraintSystem &cs, ValueDecl *member,
1252+
AllowInvalidRefInKeyPath::forRef(ConstraintSystem &cs, Type baseType,
1253+
ValueDecl *member,
12531254
ConstraintLocator *locator) {
12541255
// Referencing (instance or static) methods in key path is
12551256
// not currently allowed.
12561257
if (isa<FuncDecl>(member))
1257-
return AllowInvalidRefInKeyPath::create(cs, RefKind::Method, member,
1258-
locator);
1258+
return AllowInvalidRefInKeyPath::create(cs, baseType, RefKind::Method,
1259+
member, locator);
12591260

12601261
// Referencing enum cases in key path is not currently allowed.
12611262
if (isa<EnumElementDecl>(member)) {
1262-
return AllowInvalidRefInKeyPath::create(cs, RefKind::EnumCase, member,
1263-
locator);
1263+
return AllowInvalidRefInKeyPath::create(cs, baseType, RefKind::EnumCase,
1264+
member, locator);
12641265
}
12651266

12661267
// Referencing initializers in key path is not currently allowed.
12671268
if (isa<ConstructorDecl>(member))
1268-
return AllowInvalidRefInKeyPath::create(cs, RefKind::Initializer,
1269+
return AllowInvalidRefInKeyPath::create(cs, baseType, RefKind::Initializer,
12691270
member, locator);
12701271

12711272
if (auto *storage = dyn_cast<AbstractStorageDecl>(member)) {
12721273
// Referencing members with mutating getters in key path is not
12731274
// currently allowed.
12741275
if (storage->isGetterMutating())
1275-
return AllowInvalidRefInKeyPath::create(cs, RefKind::MutatingGetter,
1276-
member, locator);
1276+
return AllowInvalidRefInKeyPath::create(
1277+
cs, baseType, RefKind::MutatingGetter, member, locator);
12771278
}
12781279

12791280
return nullptr;
12801281
}
12811282

12821283
AllowInvalidRefInKeyPath *
1283-
AllowInvalidRefInKeyPath::create(ConstraintSystem &cs, RefKind kind,
1284-
ValueDecl *member,
1284+
AllowInvalidRefInKeyPath::create(ConstraintSystem &cs, Type baseType,
1285+
RefKind kind, ValueDecl *member,
12851286
ConstraintLocator *locator) {
12861287
return new (cs.getAllocator())
1287-
AllowInvalidRefInKeyPath(cs, kind, member, locator);
1288+
AllowInvalidRefInKeyPath(cs, baseType, kind, member, locator);
12881289
}
12891290

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

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10570,7 +10570,7 @@ static ConstraintFix *validateInitializerRef(ConstraintSystem &cs,
1057010570
baseType = MetatypeType::get(baseType);
1057110571
} else if (auto *keyPathExpr = getAsExpr<KeyPathExpr>(anchor)) {
1057210572
// Key path can't refer to initializers e.g. `\Type.init`
10573-
return AllowInvalidRefInKeyPath::forRef(cs, init, locator);
10573+
return AllowInvalidRefInKeyPath::forRef(cs, baseType, init, locator);
1057410574
}
1057510575

1057610576
if (!baseType)
@@ -10641,7 +10641,8 @@ static ConstraintFix *fixMemberRef(
1064110641
if (locator->isForKeyPathDynamicMemberLookup() ||
1064210642
locator->isForKeyPathComponent() ||
1064310643
locator->isKeyPathSubscriptComponent()) {
10644-
if (auto *fix = AllowInvalidRefInKeyPath::forRef(cs, decl, locator))
10644+
if (auto *fix =
10645+
AllowInvalidRefInKeyPath::forRef(cs, baseTy, decl, locator))
1064510646
return fix;
1064610647
}
1064710648
}

0 commit comments

Comments
 (0)