You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[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.
0 commit comments