@@ -1206,6 +1206,10 @@ TreatKeyPathSubscriptIndexAsHashable::create(ConstraintSystem &cs, Type type,
1206
1206
bool AllowInvalidRefInKeyPath::diagnose (const Solution &solution,
1207
1207
bool asNote) const {
1208
1208
switch (Kind) {
1209
+ case RefKind::StaticMember: {
1210
+ InvalidStaticMemberRefInKeyPath failure (solution, BaseType, Member, ModuleName, getLocator ());
1211
+ return failure.diagnose (asNote);
1212
+ }
1209
1213
case RefKind::EnumCase: {
1210
1214
InvalidEnumCaseRefInKeyPath failure (solution, Member, getLocator ());
1211
1215
return failure.diagnose (asNote);
@@ -1257,36 +1261,51 @@ AllowInvalidRefInKeyPath::forRef(ConstraintSystem &cs, Type baseType,
1257
1261
// not currently allowed.
1258
1262
if (isa<FuncDecl>(member))
1259
1263
return AllowInvalidRefInKeyPath::create (cs, baseType, RefKind::Method,
1260
- member, locator);
1264
+ member, Identifier (), locator);
1261
1265
1262
1266
// Referencing enum cases in key path is not currently allowed.
1263
1267
if (isa<EnumElementDecl>(member)) {
1264
1268
return AllowInvalidRefInKeyPath::create (cs, baseType, RefKind::EnumCase,
1265
- member, locator);
1269
+ member, Identifier (), locator);
1266
1270
}
1267
1271
1268
1272
// Referencing initializers in key path is not currently allowed.
1269
1273
if (isa<ConstructorDecl>(member))
1270
1274
return AllowInvalidRefInKeyPath::create (cs, baseType, RefKind::Initializer,
1271
- member, locator);
1275
+ member, Identifier (), locator);
1276
+
1277
+ // Referencing static members in key path is not currently allowed if they are
1278
+ // declared in a module built with Swift 6.0 compiler version or older.
1279
+ if (cs.shouldAttemptFixes ()) {
1280
+ auto &ctx = cs.getASTContext ();
1281
+ auto langVersion = ctx.LangOpts .EffectiveLanguageVersion ;
1282
+ auto moduleDecl = member->getDeclContext ()
1283
+ ->getParentModule ();
1284
+ auto moduleName = moduleDecl->getExportAsName ();
1285
+ auto compatibilityVersion = moduleDecl
1286
+ ->getLanguageVersionBuiltWith ();
1287
+ if (member->isStatic () && (compatibilityVersion < langVersion))
1288
+ return AllowInvalidRefInKeyPath::create (
1289
+ cs, baseType, RefKind::StaticMember, member, moduleName, locator);
1290
+ }
1272
1291
1273
1292
if (auto *storage = dyn_cast<AbstractStorageDecl>(member)) {
1274
1293
// Referencing members with mutating getters in key path is not
1275
1294
// currently allowed.
1276
1295
if (storage->isGetterMutating ())
1277
1296
return AllowInvalidRefInKeyPath::create (
1278
- cs, baseType, RefKind::MutatingGetter, member, locator);
1297
+ cs, baseType, RefKind::MutatingGetter, member, Identifier (), locator);
1279
1298
}
1280
1299
1281
1300
return nullptr ;
1282
1301
}
1283
1302
1284
1303
AllowInvalidRefInKeyPath *
1285
1304
AllowInvalidRefInKeyPath::create (ConstraintSystem &cs, Type baseType,
1286
- RefKind kind, ValueDecl *member,
1305
+ RefKind kind, ValueDecl *member, Identifier moduleName,
1287
1306
ConstraintLocator *locator) {
1288
1307
return new (cs.getAllocator ())
1289
- AllowInvalidRefInKeyPath (cs, baseType, kind, member, locator);
1308
+ AllowInvalidRefInKeyPath (cs, baseType, kind, member, moduleName, locator);
1290
1309
}
1291
1310
1292
1311
bool RemoveAddressOf::diagnose (const Solution &solution, bool asNote) const {
0 commit comments