[CSFix] Add a new kind of invalid ref in key path - unsupported static members

Replaces feature flag check with module compiler version check
which implements the behavior discussed in the SE-0438.
This commit is contained in:
Pavel Yaskevich
2024-10-28 15:04:30 -07:00
parent 07583f5ce4
commit e68e4ea1b1
3 changed files with 36 additions and 21 deletions

View File

@@ -6337,22 +6337,15 @@ SourceLoc InvalidMemberRefInKeyPath::getLoc() const {
}
bool InvalidStaticMemberRefInKeyPath::diagnoseAsError() {
auto *KPE = getAsExpr<KeyPathExpr>(getRawAnchor());
auto rootTyRepr = KPE->getExplicitRootType();
auto isProtocol = getBaseType()->isExistentialType();
if (!getConstraintSystem().getASTContext().LangOpts.hasFeature(
Feature::KeyPathWithStaticMembers)) {
emitDiagnostic(diag::expr_keypath_static_member, getMember(),
isForKeyPathDynamicMemberLookup());
} else {
if (rootTyRepr && !isProtocol) {
emitDiagnostic(diag::could_not_use_type_member_on_instance, getBaseType(),
DeclNameRef(getMember()->getName()))
.fixItInsert(rootTyRepr->getEndLoc(), ".Type");
} else {
auto diagnostic =
emitDiagnostic(diag::could_not_use_type_member_on_instance, getBaseType(),
DeclNameRef(getMember()->getName()));
// Suggest adding `.Type` to an explicit root if possible.
if (auto *keyPath = getAsExpr<KeyPathExpr>(getRawAnchor())) {
if (auto *explicitRoot = keyPath->getExplicitRootType()) {
if (explicitRoot && !getBaseType()->isExistentialType())
diagnostic.fixItInsert(explicitRoot->getEndLoc(), ".Type");
}
}