@@ -1248,6 +1248,11 @@ bool AllowInvalidRefInKeyPath::diagnose(const Solution &solution,
1248
1248
getLocator ());
1249
1249
return failure.diagnose (asNote);
1250
1250
}
1251
+ case RefKind::Method:
1252
+ case RefKind::Initializer: {
1253
+ InvalidMethodRefInKeyPath failure (solution, Member, getLocator ());
1254
+ return failure.diagnose (asNote);
1255
+ }
1251
1256
case RefKind::MutatingMethod: {
1252
1257
InvalidMutatingMethodRefInKeyPath failure (solution, Member, getLocator ());
1253
1258
return failure.diagnose (asNote);
@@ -1320,22 +1325,41 @@ AllowInvalidRefInKeyPath::forRef(ConstraintSystem &cs, Type baseType,
1320
1325
cs, baseType, RefKind::MutatingGetter, member, locator);
1321
1326
}
1322
1327
1323
- // Referencing mutating, throws or async method members is not currently
1324
- // allowed.
1325
- if (auto method = dyn_cast<FuncDecl>(member)) {
1326
- if (method->isAsyncContext ())
1327
- return AllowInvalidRefInKeyPath::create (
1328
- cs, baseType, RefKind::AsyncOrThrowsMethod, member, locator);
1329
- if (auto methodType = method->getInterfaceType ()->getAs <AnyFunctionType>()) {
1330
- if (methodType->getResult ()->getAs <AnyFunctionType>()->isThrowing ())
1328
+ if (cs.getASTContext ().LangOpts .hasFeature (
1329
+ Feature::KeyPathWithMethodMembers)) {
1330
+ // Referencing mutating, throws or async method members is not currently
1331
+ // allowed.
1332
+ if (auto method = dyn_cast<FuncDecl>(member)) {
1333
+ if (method->isAsyncContext ())
1331
1334
return AllowInvalidRefInKeyPath::create (
1332
1335
cs, baseType, RefKind::AsyncOrThrowsMethod, member, locator);
1336
+ if (auto methodType =
1337
+ method->getInterfaceType ()->getAs <AnyFunctionType>()) {
1338
+ if (methodType->getResult ()->getAs <AnyFunctionType>()->isThrowing ())
1339
+ return AllowInvalidRefInKeyPath::create (
1340
+ cs, baseType, RefKind::AsyncOrThrowsMethod, member, locator);
1341
+ }
1342
+ if (method->isMutating ())
1343
+ return AllowInvalidRefInKeyPath::create (
1344
+ cs, baseType, RefKind::MutatingMethod, member, locator);
1345
+ return nullptr ;
1333
1346
}
1334
- if (method-> isMutating ())
1335
- return AllowInvalidRefInKeyPath::create (
1336
- cs, baseType, RefKind::MutatingMethod, member, locator) ;
1347
+
1348
+ if (isa<ConstructorDecl>(member))
1349
+ return nullptr ;
1337
1350
}
1338
1351
1352
+ // Referencing (instance or static) methods in key path is
1353
+ // not currently allowed.
1354
+ if (isa<FuncDecl>(member))
1355
+ return AllowInvalidRefInKeyPath::create (cs, baseType, RefKind::Method,
1356
+ member, locator);
1357
+
1358
+ // Referencing initializers in key path is not currently allowed.
1359
+ if (isa<ConstructorDecl>(member))
1360
+ return AllowInvalidRefInKeyPath::create (cs, baseType, RefKind::Initializer,
1361
+ member, locator);
1362
+
1339
1363
return nullptr ;
1340
1364
}
1341
1365
0 commit comments