Fix dynamic runtime casts of Optionals.

Fixes <rdar://23122310> Runtime dynamic casts...

This makes runtime dynamic casts consistent with language rules, and
consequently makes specialization of generic code consistent with an
equivalent nongeneric implementation.

The runtime now supports casts from Optional<T> to U. Naturally the
cast fails on nil source, but otherwise succeeds if T is convertible to
U.

When casting T to Optional<U> the runtime succeeds whenever T is
convertible to U and simply wraps the result in an Optional.

To greatly simplify the runtime, I am assuming that
target-type-specific runtime cast entry points
(e.g. swift_dynamicCastClass) are never invoked with an optional
source. This assumption is valid for the following reasons. At the
language level optionals must be unwrapped before downcasting (via
as[?!]), so we only need to worry about SIL and IR lowering.  This
implementation assumes (with asserts) that:

- SIL promotion from an address cast to a value casts should only happen
  when the source is nonoptional. Handling optional unwrapping in SIL
  would be too complicated because we need to check for Optional's own
  conformances. (I added a test case to ensure this promotion does not
  happen). This is not an issue for unchecked_ref_cast, which
  implicitly unwraps optionals, so we can promote those!

- IRGen lowers unchecked_ref_cast (Builtin.castReference) directly to
  a bitcast (will be caught by asserts).

- IRGen continues to emit the generic dynamicCast entry point for
  address-casts (will be caught by asserts).
This commit is contained in:
Andrew Trick
2015-12-09 16:07:31 -08:00
parent a8a49afd9e
commit 35cb1afab8
5 changed files with 208 additions and 1 deletions

View File

@@ -1990,13 +1990,59 @@ static id dynamicCastValueToNSError(OpaqueValue *src,
}
#endif
static bool canCastToExistential(OpaqueValue *dest, OpaqueValue *src,
const Metadata *srcType,
const Metadata *targetType) {
if (targetType->getKind() != MetadataKind::Existential)
return false;
return _dynamicCastToExistential(dest, src, srcType,
cast<ExistentialTypeMetadata>(targetType),
DynamicCastFlags::Default);
}
/// Perform a dynamic cast to an arbitrary type.
bool swift::swift_dynamicCast(OpaqueValue *dest,
OpaqueValue *src,
const Metadata *srcType,
const Metadata *targetType,
DynamicCastFlags flags) {
// Check if the cast source is Optional and the target is not an existential
// that Optional conforms to. Unwrap one level of Optional and continue.
if (srcType->getKind() == MetadataKind::Optional
&& !canCastToExistential(dest, src, srcType, targetType)) {
const Metadata *payloadType =
cast<EnumMetadata>(srcType)->getGenericArgs()[0];
int enumCase =
swift_getEnumCaseSinglePayload(src, payloadType, 1 /*emptyCases=*/);
if (enumCase != -1) {
// Allow Optional<T>.None -> Optional<U>.None
if (targetType->getKind() != MetadataKind::Optional)
return _fail(src, srcType, targetType, flags);
// Inject the .None tag
swift_storeEnumTagSinglePayload(dest, payloadType, enumCase,
1 /*emptyCases=*/);
return _succeed(dest, src, srcType, flags);
}
// .Some
// Single payload enums are guaranteed layout compatible with their
// payload. Only the source's payload needs to be taken or destroyed.
srcType = payloadType;
}
switch (targetType->getKind()) {
// Handle wrapping an Optional target.
case MetadataKind::Optional: {
// Recursively cast into the layout compatible payload area.
const Metadata *payloadType =
cast<EnumMetadata>(targetType)->getGenericArgs()[0];
if (swift_dynamicCast(dest, src, srcType, payloadType, flags)) {
swift_storeEnumTagSinglePayload(dest, payloadType, -1 /*case*/,
1 /*emptyCases*/);
return true;
}
return false;
}
// Casts to class type.
case MetadataKind::Class:
@@ -2089,7 +2135,6 @@ bool swift::swift_dynamicCast(OpaqueValue *dest,
case MetadataKind::Struct:
case MetadataKind::Enum:
case MetadataKind::Optional:
switch (srcType->getKind()) {
case MetadataKind::Class:
case MetadataKind::ObjCClassWrapper: