diff --git a/stdlib/runtime/Casting.cpp b/stdlib/runtime/Casting.cpp index 54ed2039dab60..19b033a163e87 100644 --- a/stdlib/runtime/Casting.cpp +++ b/stdlib/runtime/Casting.cpp @@ -765,6 +765,11 @@ static bool _dynamicCastToExistential(OpaqueValue *dest, const Metadata *srcType, const ExistentialTypeMetadata *targetType, DynamicCastFlags flags) { +#if SWIFT_OBJC_INTEROP + // This variable's lifetime needs to be for the whole function, but is + // only valid with Objective-C interop enabled. + id tmp; +#endif // Find the actual type of the source. OpaqueValue *srcDynamicValue; const Metadata *srcDynamicType; @@ -785,9 +790,10 @@ static bool _dynamicCastToExistential(OpaqueValue *dest, // Class metadata can be used as an object when ObjC interop is available. auto metatypePtr = reinterpret_cast(src); auto metatype = *metatypePtr; + tmp = swift_dynamicCastMetatypeToObjectConditional(metatype); // If the cast succeeded, use the result value as the class instance // below. - if (id tmp = swift_dynamicCastMetatypeToObjectConditional(metatype)) { + if (tmp) { srcDynamicValue = reinterpret_cast(&tmp); srcDynamicType = reinterpret_cast(tmp); break;