Skip to content

Commit a86e1ee

Browse files
committed
[TypeChecker] Limit @dynamicMemberLookup parameter to compose only with Sendable
1 parent da052e5 commit a86e1ee

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

lib/Sema/CSApply.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,8 +2372,10 @@ namespace {
23722372
};
23732373

23742374
Type keyPathTy = argType;
2375-
if (auto *existential = keyPathTy->getAs<ExistentialType>())
2376-
keyPathTy = existential->getExistentialLayout().explicitSuperclass;
2375+
if (auto *existential = keyPathTy->getAs<ExistentialType>()) {
2376+
keyPathTy = existential->getSuperclass();
2377+
assert(isKnownKeyPathType(keyPathTy));
2378+
}
23772379

23782380
SmallVector<Component, 2> components;
23792381
auto *componentLoc = cs.getConstraintLocator(

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3487,8 +3487,10 @@ void ConstraintSystem::bindOverloadType(
34873487
// and U is a leaf type (aka member type).
34883488
auto paramTy = fnType->getParams()[0].getPlainType();
34893489

3490-
if (auto *existential = paramTy->getAs<ExistentialType>())
3491-
paramTy = existential->getExistentialLayout().explicitSuperclass;
3490+
if (auto *existential = paramTy->getAs<ExistentialType>()) {
3491+
paramTy = existential->getSuperclass();
3492+
assert(isKnownKeyPathType(paramTy));
3493+
}
34923494

34933495
auto keyPathTy = paramTy->castTo<BoundGenericType>();
34943496

lib/Sema/TypeCheckAttr.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,10 +1828,17 @@ bool swift::isValidKeyPathDynamicMemberLookup(SubscriptDecl *decl,
18281828

18291829
auto paramTy = decl->getIndices()->get(0)->getInterfaceType();
18301830

1831-
// Allow to compose key path type with a protocol as a way to express
1832-
// additional requirements on the parameter.
1831+
// Allow to compose key path type with a `Sendable` protocol as
1832+
// a way to express sendability requirement.
18331833
if (auto *existential = paramTy->getAs<ExistentialType>()) {
1834-
paramTy = existential->getExistentialLayout().explicitSuperclass;
1834+
auto layout = existential->getExistentialLayout();
1835+
1836+
auto protocols = layout.getProtocols();
1837+
if (!(protocols.size() == 1 &&
1838+
protocols[0] == ctx.getProtocol(KnownProtocolKind::Sendable)))
1839+
return false;
1840+
1841+
paramTy = layout.getSuperclass();
18351842
if (!paramTy)
18361843
return false;
18371844
}

0 commit comments

Comments
 (0)