Skip to content

Commit 863bb7c

Browse files
committed
[TypeChecker] Make couldDynamicallyConformToProtocol more leanient
Current check is too strict for stdlib collection types because it checks conditional conformances which leads to erroneous warnings. To determine whether a type dynamically conforms to a protocol, for stdlib collection types, the method is employing `Module::lookupConformance` check which has `allowMissing` conformances set to `false` by default. That directly contradicts the comment which states that the conditional conformances are to be skipped and doesn't fit well with `TypeChecker::conformsToProtocol` check below it that defaults `allowMissing` to `true`. Resolves: rdar://115603144
1 parent 85b5267 commit 863bb7c

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5904,7 +5904,8 @@ TypeChecker::couldDynamicallyConformToProtocol(Type type, ProtocolDecl *Proto,
59045904
// as an intermediate collection cast can dynamically change if the conditions
59055905
// are met or not.
59065906
if (type->isKnownStdlibCollectionType())
5907-
return !M->lookupConformance(type, Proto).isInvalid();
5907+
return !M->lookupConformance(type, Proto, /*allowMissing=*/true)
5908+
.isInvalid();
59085909
return !conformsToProtocol(type, Proto, M).isInvalid();
59095910
}
59105911

test/Constraints/casts.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,3 +751,10 @@ do {
751751
_ = a is String // OK
752752
}
753753
}
754+
755+
// rdar://115603144 - casting `any Sendable` to a collection warns about cast failure although the cast could succeed at runtime
756+
func test_existential_sendable_cast(v: any Sendable) {
757+
let _ = v as! [Any] // Ok
758+
let _ = v as! [String: Any] // Ok
759+
let _ = v as! Set<Int> // Ok
760+
}

0 commit comments

Comments
 (0)