Skip to content

Commit a288b1e

Browse files
committed
[CSBindings] Key path cannot be bound to typ erased type even if its existential
Extend existing check to handle type erased versions when they are wrapped in an existential.
1 parent fe2f1ca commit a288b1e

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6389,6 +6389,11 @@ class TypeVarRefCollector : public ASTWalker {
63896389
/// for a key path `{Any, Partial, Writable, ReferenceWritable}KeyPath`.
63906390
bool isKnownKeyPathType(Type type);
63916391

6392+
/// Determine whether the given type is a PartialKeyPath and
6393+
/// AnyKeyPath or existential type thererof, for example,
6394+
/// `PartialKeyPath<...> & Sendable`.
6395+
bool isTypeErasedKeyPathType(Type type);
6396+
63926397
/// Determine whether given declaration is one for a key path
63936398
/// `{Writable, ReferenceWritable}KeyPath`.
63946399
bool isKnownKeyPathDecl(ASTContext &ctx, ValueDecl *decl);

lib/Sema/CSBindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2359,7 +2359,7 @@ bool TypeVarBindingProducer::computeNext() {
23592359
auto supertype = *simplifiedSuper;
23602360
// A key path type cannot be bound to type-erased key path variants.
23612361
if (TypeVar->getImpl().isKeyPathType() &&
2362-
(supertype->isPartialKeyPath() || supertype->isAnyKeyPath()))
2362+
isTypeErasedKeyPathType(supertype))
23632363
continue;
23642364

23652365
addNewBinding(binding.withType(supertype));

lib/Sema/ConstraintSystem.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6676,6 +6676,19 @@ bool constraints::isKnownKeyPathType(Type type) {
66766676
type->isAnyKeyPath();
66776677
}
66786678

6679+
bool constraints::isTypeErasedKeyPathType(Type type) {
6680+
assert(type);
6681+
6682+
if (type->isPartialKeyPath() || type->isAnyKeyPath())
6683+
return true;
6684+
6685+
if (!type->isExistentialType())
6686+
return false;
6687+
6688+
auto superclass = type->getSuperclass();
6689+
return superclass ? isTypeErasedKeyPathType(superclass) : false;
6690+
}
6691+
66796692
bool constraints::hasExplicitResult(ClosureExpr *closure) {
66806693
auto &ctx = closure->getASTContext();
66816694
return evaluateOrDefault(ctx.evaluator,

0 commit comments

Comments
 (0)