Commit Graph

1 Commits

Author SHA1 Message Date
tbkka
d92f1d58f8 Dynamic Casting: Properly unwrap existential metatype sources (#34469)
* Dynamic Casting: Properly unwrap existential metatype sources

Existential metatypes are really just existentials that hold metatypes.  As
such, they should be handled in the general casting logic in much the same way
as regular existentials: They should generally be ignored by most casting logic,
and unwrapped as necessary at the top level.

In particular, the previous code would fail to correctly handle the following
cast from an existential metatype (`AnyObject.Type`) to an existential
(`AnyObject`):
```
  class C {}
  let a = C.self as AnyObject.Type
  let b = a as! AnyObject
```
With the old code, `b` above would hold a reference to a `__SwiftValue` box
containing the type reference.  The correct result would simply store the type
reference directly in `b`.  These two are only really distinguishable in that
the correct form permits `a === b` to return `true`.

Fixes rdar://70582753

Note: This is not yet fully supported on Linux.  Basically, metatypes on Linux are not currently
fully compatible with reference-counted class pointers, which prevents us from
fully supporting metatype operations on Linux that we support on macOS.
2020-10-29 14:46:10 -07:00