@@ -7445,6 +7445,7 @@ ConstraintSystem::inferKeyPathLiteralCapability(TypeVariableType *keyPathType) {
7445
7445
std::pair<bool , llvm::Optional<KeyPathCapability>>
7446
7446
ConstraintSystem::inferKeyPathLiteralCapability (KeyPathExpr *keyPath) {
7447
7447
bool didOptionalChain = false ;
7448
+ bool isSendable = true ;
7448
7449
7449
7450
auto fail = []() -> std::pair<bool , llvm::Optional<KeyPathCapability>> {
7450
7451
return std::make_pair (false , llvm::None);
@@ -7454,9 +7455,9 @@ ConstraintSystem::inferKeyPathLiteralCapability(KeyPathExpr *keyPath) {
7454
7455
return std::make_pair (true , llvm::None);
7455
7456
};
7456
7457
7457
- auto success = [](KeyPathMutability mutability)
7458
+ auto success = [](KeyPathMutability mutability, bool isSendable )
7458
7459
-> std::pair<bool , llvm::Optional<KeyPathCapability>> {
7459
- KeyPathCapability capability (mutability, /* isSendable= */ true );
7460
+ KeyPathCapability capability (mutability, isSendable);
7460
7461
return std::make_pair (true , capability);
7461
7462
};
7462
7463
@@ -7475,10 +7476,33 @@ ConstraintSystem::inferKeyPathLiteralCapability(KeyPathExpr *keyPath) {
7475
7476
case KeyPathExpr::Component::Kind::CodeCompletion: {
7476
7477
return fail ();
7477
7478
}
7479
+
7480
+ case KeyPathExpr::Component::Kind::UnresolvedSubscript:
7481
+ case KeyPathExpr::Component::Kind::Subscript: {
7482
+ if (Context.LangOpts .hasFeature (Feature::InferSendableFromCaptures)) {
7483
+ // Key path is sendable only when all of its captures are sendable.
7484
+ if (auto *args = component.getSubscriptArgs ()) {
7485
+ auto *sendable = Context.getProtocol (KnownProtocolKind::Sendable);
7486
+
7487
+ for (const auto &arg : *args) {
7488
+ auto argTy = simplifyType (getType (arg.getExpr ()));
7489
+
7490
+ // Sendability cannot be determined until the argument
7491
+ // is fully resolved.
7492
+ if (argTy->hasTypeVariable ())
7493
+ return delay ();
7494
+
7495
+ auto conformance = lookupConformance (argTy, sendable);
7496
+ isSendable &=
7497
+ bool (conformance) &&
7498
+ !conformance.hasMissingConformance (DC->getParentModule ());
7499
+ }
7500
+ }
7501
+ }
7502
+ LLVM_FALLTHROUGH;
7503
+ }
7478
7504
case KeyPathExpr::Component::Kind::Property:
7479
- case KeyPathExpr::Component::Kind::Subscript:
7480
- case KeyPathExpr::Component::Kind::UnresolvedProperty:
7481
- case KeyPathExpr::Component::Kind::UnresolvedSubscript: {
7505
+ case KeyPathExpr::Component::Kind::UnresolvedProperty: {
7482
7506
auto *componentLoc =
7483
7507
getConstraintLocator (keyPath, LocatorPathElt::KeyPathComponent (i));
7484
7508
auto *calleeLoc = getCalleeLocator (componentLoc);
@@ -7555,7 +7579,7 @@ ConstraintSystem::inferKeyPathLiteralCapability(KeyPathExpr *keyPath) {
7555
7579
if (didOptionalChain)
7556
7580
mutability = KeyPathMutability::ReadOnly;
7557
7581
7558
- return success (mutability);
7582
+ return success (mutability, isSendable );
7559
7583
}
7560
7584
7561
7585
TypeVarBindingProducer::TypeVarBindingProducer (BindingSet &bindings)
0 commit comments