Skip to content

[CSApply] Key path dynamic member lookup argument is Sendable only if its captures are #83348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2556,18 +2556,39 @@ namespace {
/// Build an implicit argument for keypath based dynamic lookup,
/// which consists of KeyPath expression and a single component.
///
/// \param argType The type of the keypath subscript argument.
/// \param paramType The type of the keypath subscript parameter
/// this argument is passed to.
/// \param dotLoc The location of the '.' preceding member name.
/// \param memberLoc The locator to be associated with new argument.
Expr *buildKeyPathDynamicMemberArgExpr(Type argType, SourceLoc dotLoc,
Expr *buildKeyPathDynamicMemberArgExpr(Type paramType, SourceLoc dotLoc,
ConstraintLocator *memberLoc) {
using Component = KeyPathExpr::Component;
auto *anchor = getAsExpr(memberLoc->getAnchor());

auto makeKeyPath = [&](ArrayRef<Component> components) -> Expr * {
Type keyPathTy = paramType;

// If parameter of a dynamic member lookup is `& Sendable` type
// we need to check key path captures to determine whether the
// argument could be `& Sendable` as well or not.
if (paramType->isExistentialType() && paramType->isSendableType()) {
auto allCapturesAreSendable = [&](const Component &component) {
auto *argList = component.getArgs();
if (!argList)
return true;

return llvm::all_of(*argList, [&](const auto &arg) {
return solution.getResolvedType(arg.getExpr())->isSendableType();
});
};

if (!llvm::all_of(components, allCapturesAreSendable))
keyPathTy = paramType->getSuperclass();
}

auto *kp = KeyPathExpr::createImplicit(ctx, /*backslashLoc*/ dotLoc,
components, anchor->getEndLoc());
kp->setType(argType);
kp->setType(keyPathTy);
cs.cacheExprTypes(kp);

// See whether there's an equivalent ObjC key path string we can produce
Expand All @@ -2576,7 +2597,7 @@ namespace {
return kp;
};

Type keyPathTy = argType;
Type keyPathTy = paramType;
if (auto *existential = keyPathTy->getAs<ExistentialType>()) {
keyPathTy = existential->getSuperclass();
assert(isKnownKeyPathType(keyPathTy));
Expand Down Expand Up @@ -3687,6 +3708,8 @@ namespace {
if (!argExpr)
return nullptr;

solution.recordSingleArgMatchingChoice(cs.getConstraintLocator(expr));

// Build an argument list.
auto *argList =
ArgumentList::forImplicitSingle(ctx, ctx.Id_dynamicMember, argExpr);
Expand Down
19 changes: 19 additions & 0 deletions test/Concurrency/sendable_keypaths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,22 @@ do {
// TODO(rdar://125948508): This shouldn't be ambiguous (@Sendable version should be preferred)
let _: () -> Void = forward(Test.fn) // expected-error {{conflicting arguments to generic parameter 'T' ('@Sendable () -> ()' vs. '() -> Void')}}
}

// https://github.com/swiftlang/swift/issues/77105
do {
@dynamicMemberLookup
struct S<T> {
subscript<U>(dynamicMember keyPath: KeyPath<T, U> & Sendable) -> U {
fatalError()
}
}

struct Foo {
subscript<T>(bar bar: T) -> Int { 42 }
}

func test(s: S<Foo>) {
_ = s[bar: NonSendable()]
// expected-warning@-1 {{type 'KeyPath<Foo, Int>' does not conform to the 'Sendable' protocol; this is an error in the Swift 6 language mode}}
}
}