Skip to content

Commit eef82bc

Browse files
committed
Refine doesCastPreserveOwnershipForTypes.
Allow borrowed class-to-AnyObject scalar casts. (cherry picked from commit 16e5fe9)
1 parent 4343178 commit eef82bc

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lib/SIL/Utils/DynamicCasts.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,18 @@ bool swift::doesCastPreserveOwnershipForTypes(SILModule &module,
320320
if (!canIRGenUseScalarCheckedCastInstructions(module, sourceType, targetType))
321321
return false;
322322

323-
return !sourceType->isPotentiallyAnyObject()
324-
&& !targetType->isPotentiallyAnyObject();
323+
// (B2) unwrapping
324+
if (sourceType->isPotentiallyAnyObject())
325+
return false;
326+
327+
// (B1) wrapping
328+
if (targetType->isPotentiallyAnyObject()) {
329+
// A class type cannot be wrapped in __SwiftValue, so casting
330+
// from a class to AnyObject preserves ownership.
331+
return
332+
sourceType->mayHaveSuperclass() || sourceType->isClassExistentialType();
333+
}
334+
return true;
325335
}
326336

327337
bool SILDynamicCastInst::isRCIdentityPreserving() const {

0 commit comments

Comments
 (0)