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