Refine doesCastPreserveOwnershipForTypes.

Allow borrowed class-to-AnyObject scalar casts.
This commit is contained in:
Andrew Trick
2022-04-12 22:22:07 -07:00
parent 20192125d6
commit 16e5fe9459

View File

@@ -320,8 +320,18 @@ bool swift::doesCastPreserveOwnershipForTypes(SILModule &module,
if (!canIRGenUseScalarCheckedCastInstructions(module, sourceType, targetType))
return false;
return !sourceType->isPotentiallyAnyObject()
&& !targetType->isPotentiallyAnyObject();
// (B2) unwrapping
if (sourceType->isPotentiallyAnyObject())
return false;
// (B1) wrapping
if (targetType->isPotentiallyAnyObject()) {
// A class type cannot be wrapped in __SwiftValue, so casting
// from a class to AnyObject preserves ownership.
return
sourceType->mayHaveSuperclass() || sourceType->isClassExistentialType();
}
return true;
}
bool SILDynamicCastInst::isRCIdentityPreserving() const {