[swift-users] Assigning [Int] to [Any] crashes Swift 2.2

Joe Groff jgroff at apple.com
Thu Apr 14 11:00:25 CDT 2016


> On Apr 13, 2016, at 9:09 PM, Jens Alfke via swift-users <swift-users at swift.org> wrote:
> 
> Just ran into a weird crash with Swift 2.2 in Xcode 7.3. It reproduces in a playground:
> 
> import Foundation
> let a = [88]
> let b: [Any] = a   // CRASH
> 
> In my real program, the top of the crash backtrace is:
> 
> * thread #1: tid = 0x460b62, 0x000000010669988b libswiftCore.dylib`swift_unknownRetain + 27, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x58)
>     frame #0: 0x000000010669988b libswiftCore.dylib`swift_unknownRetain + 27
>     frame #1: 0x0000000106488565 libswiftCore.dylib`Swift._arrayConditionalBridgeElements <A, B> (Swift.Array<A>) -> Swift.Optional<Swift.Array<B>> + 1029
>     frame #2: 0x00000001064875a7 libswiftCore.dylib`Swift._arrayForceCast <A, B> (Swift.Array<A>) -> Swift.Array<B> + 263
> 
> It’s interesting that the memory address causing the crash is 0x58, which in decimal is 88, the first item in the array. If you change the first item of the array to a different number, the crash address changes to match. So it’s misinterpreting the integer as a pointer.
> 
> Also interestingly, if you remove “import Foundation”, it no longer compiles — the last line gets an error “Cannot convert value of type ‘[Int]’ to expected argument type ‘[Any]’”. Is boxing of integers really dependent on Obj-C bridging?
> 
> —Jens
> 
> PS: Should I file the bug report with Apple or at swift.org?

This is a known bug. Converting arrays of value type to arrays of protocol type is not supported—only class covariance is supported with containers—but the compiler fails to catch some cases. 

-Joe



More information about the swift-users mailing list