mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
* [DynamicCast] Rely on runtime when casts can't be optimized The Swift compiler renders `as?` operations in Swift into variations of the `checked_cast_br` instructions in SIL. Subsequent optimization passes may alter or eliminate these instructions. Any remaining instructions after optimization are translated by IRGen into runtime calls. At least, that's the theory. Unfortunately, the current IRGen logic does not recognize all of the casting instruction variants and renders one particular unrecognized variant as a simple nil load. Specifically, this occurs for optimized casts of metatypes to AnyObject: ``` let a = Int.self let b = a as? AnyObject // b should not be nil here ``` This PR changes this case in IRGen to instead generate a call to `swift_dynamicCast`, deferring this case to the runtime. Future: Someday, the compiler should be taught to correctly optimize this cast away.