@@ -2195,6 +2195,10 @@ static bool tryDynamicCastBoxedSwiftValue(OpaqueValue *dest,
2195
2195
const Metadata *srcType,
2196
2196
const Metadata *targetType,
2197
2197
DynamicCastFlags flags) {
2198
+ // These flag combinations are not handled here.
2199
+ assert (!(flags & DynamicCastFlags::Unconditional));
2200
+ assert (!(flags & DynamicCastFlags::DestroyOnFailure));
2201
+
2198
2202
// Swift type should be AnyObject or a class type.
2199
2203
if (!srcType->isAnyClass ()) {
2200
2204
auto existential = dyn_cast<ExistentialTypeMetadata>(srcType);
@@ -2234,7 +2238,8 @@ static bool tryDynamicCastBoxedSwiftValue(OpaqueValue *dest,
2234
2238
2235
2239
// Maybe we can cast the boxed value to our destination type somehow.
2236
2240
auto innerFlags = flags - DynamicCastFlags::TakeOnSuccess
2237
- - DynamicCastFlags::DestroyOnFailure;
2241
+ - DynamicCastFlags::DestroyOnFailure
2242
+ - DynamicCastFlags::Unconditional;
2238
2243
if (swift_dynamicCast (dest, const_cast <OpaqueValue*>(boxedValue),
2239
2244
boxedType, targetType, innerFlags)) {
2240
2245
// Release the box if we need to.
@@ -2525,10 +2530,19 @@ bool swift::swift_dynamicCast(OpaqueValue *dest, OpaqueValue *src,
2525
2530
return unwrapResult.success ;
2526
2531
2527
2532
#if SWIFT_OBJC_INTEROP
2528
- // A class or AnyObject reference may point at a boxed _SwiftValue.
2529
- if (tryDynamicCastBoxedSwiftValue (dest, src, srcType,
2530
- targetType, flags)) {
2531
- return true ;
2533
+ // A class or AnyObject reference may point to a _SwiftValue box.
2534
+ {
2535
+ auto innerFlags = flags - DynamicCastFlags::Unconditional
2536
+ - DynamicCastFlags::DestroyOnFailure;
2537
+ if (tryDynamicCastBoxedSwiftValue (dest, src, srcType,
2538
+ targetType, innerFlags)) {
2539
+ // TakeOnSuccess was handled inside tryDynamicCastBoxedSwiftValue().
2540
+ return true ;
2541
+ } else {
2542
+ // Couldn't cast boxed value to targetType.
2543
+ // Fall through and try to cast the _SwiftValue box itself to targetType.
2544
+ // (for example, casting _SwiftValue to NSObject will be successful)
2545
+ }
2532
2546
}
2533
2547
#endif
2534
2548
0 commit comments