[cxx-interop] C++ reference types are not AnyObjects

C++ foreign reference types have custom reference counting mechanisms, so they cannot conform to `AnyObject`.

Currently Swift's type system treats C++ FRTs as `AnyObject`s on non-Darwin platforms, which is incorrect. This change makes sure the behavior is consistent with Darwin platform, i.e. a cast of C++ FRT to `AnyObject` is rejected by the typechecker.

rdar://136664617
This commit is contained in:
Egor Zhdan
2024-06-06 19:55:36 +01:00
parent 5925df7ca0
commit 82c076c9d3
2 changed files with 5 additions and 11 deletions

View File

@@ -7580,6 +7580,10 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
// Class and protocol metatypes are interoperable with certain Objective-C
// runtime classes, but only when ObjC interop is enabled.
// Foreign reference types do *not* conform to AnyObject.
if (type1->isForeignReferenceType() && type2->isAnyObject())
return getTypeMatchFailure(locator);
if (getASTContext().LangOpts.EnableObjCInterop) {
// These conversions are between concrete types that don't need further
// resolution, so we can consider them immediately solved.
@@ -7589,10 +7593,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
type1, type2, locator);
return getTypeMatchSuccess();
};
// Foreign reference types do *not* conform to AnyObject.
if (type1->isForeignReferenceType() && type2->isAnyObject())
return getTypeMatchFailure(locator);
if (auto meta1 = type1->getAs<MetatypeType>()) {
if (meta1->getInstanceType()->mayHaveSuperclass()