Skip to content

Commit 9dc1403

Browse files
committed
[CSDiagnostics] SE-0438: Add a tailored diagnostic for unsupported static member references
Libraries of modules built with older compilers (< 6.1) don't have symbols required to enable staitc member support in key path context.
1 parent e68e4ea commit 9dc1403

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,13 @@ ERROR(expr_keypath_mutating_getter,none,
666666
ERROR(expr_keypath_static_member,none,
667667
"%select{key path|dynamic key path member lookup}1 cannot refer to static member %0",
668668
(const ValueDecl *, bool))
669+
ERROR(keypath_static_member_access_from_unsupported_module,none,
670+
"%select{key path|dynamic key path member lookup}3 cannot refer "
671+
"to static member %1 of type %0 from module %2",
672+
(Type, const ValueDecl *, const ModuleDecl *, bool))
673+
NOTE(keypath_static_member_access_from_unsupported_module_note,none,
674+
"rebuild %0 to enable static member use in key paths",
675+
(const ModuleDecl *))
669676
ERROR(expr_keypath_enum_case,none,
670677
"%select{key path|dynamic key path member lookup}1 cannot refer to enum case %0",
671678
(const ValueDecl *, bool))

lib/Sema/CSDiagnostics.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6352,6 +6352,18 @@ bool InvalidStaticMemberRefInKeyPath::diagnoseAsError() {
63526352
return true;
63536353
}
63546354

6355+
bool UnsupportedStaticMemberRefInKeyPath::diagnoseAsError() {
6356+
auto *member = getMember();
6357+
auto *module = member->getDeclContext()->getParentModule();
6358+
6359+
emitDiagnostic(diag::keypath_static_member_access_from_unsupported_module,
6360+
BaseType->getMetatypeInstanceType(), member, module,
6361+
getLocator()->isForKeyPathDynamicMemberLookup());
6362+
emitDiagnostic(
6363+
diag::keypath_static_member_access_from_unsupported_module_note, module);
6364+
return true;
6365+
}
6366+
63556367
bool InvalidEnumCaseRefInKeyPath::diagnoseAsError() {
63566368
emitDiagnostic(diag::expr_keypath_enum_case, getMember(),
63576369
isForKeyPathDynamicMemberLookup());

lib/Sema/CSDiagnostics.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1767,13 +1767,31 @@ class InvalidStaticMemberRefInKeyPath final : public InvalidMemberRefInKeyPath {
17671767
InvalidStaticMemberRefInKeyPath(const Solution &solution, Type baseType,
17681768
ValueDecl *member, ConstraintLocator *locator)
17691769
: InvalidMemberRefInKeyPath(solution, member, locator),
1770-
BaseType(baseType->getRValueType()) {}
1770+
BaseType(resolveType(baseType)->getRValueType()) {}
17711771

17721772
Type getBaseType() const { return BaseType; }
17731773

17741774
bool diagnoseAsError() override;
17751775
};
17761776

1777+
/// Diagnose an attempt to reference a static member from an unsupported module.
1778+
///
1779+
/// Only modules built either from source with 6.1+ compiler or
1780+
/// from .swiftinterface that was generated by 6.1+ compiler are supported.
1781+
class UnsupportedStaticMemberRefInKeyPath final
1782+
: public InvalidMemberRefInKeyPath {
1783+
Type BaseType;
1784+
1785+
public:
1786+
UnsupportedStaticMemberRefInKeyPath(const Solution &solution, Type baseType,
1787+
ValueDecl *member,
1788+
ConstraintLocator *locator)
1789+
: InvalidMemberRefInKeyPath(solution, member, locator),
1790+
BaseType(resolveType(baseType)->getRValueType()) {}
1791+
1792+
bool diagnoseAsError() override;
1793+
};
1794+
17771795
/// Diagnose an attempt to reference an enum case as a key path component
17781796
/// e.g.
17791797
///

lib/Sema/CSFix.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,9 @@ bool AllowInvalidRefInKeyPath::diagnose(const Solution &solution,
12371237
}
12381238

12391239
case RefKind::UnsupportedStaticMember: {
1240-
return false;
1240+
UnsupportedStaticMemberRefInKeyPath failure(solution, BaseType, Member,
1241+
getLocator());
1242+
return failure.diagnose(asNote);
12411243
}
12421244

12431245
case RefKind::EnumCase: {

0 commit comments

Comments
 (0)