[Swift] Function marked with @convention(block) not AnyObject

Originator:zwaldowski
Number:rdar://22679450 Date Originated:14-Sep-2015 01:47 AM
Status:Open Resolved:false
Product:Developer Tools Product Version:Xcode 7.0 (7A218)/Swift 2.0 (swiftlang-700.0.59 clang-700.0.72)
Classification:Other Bug Reproducible:Always
 
Summary:
The Swift 2 book comments the use of the parameter "block" to the decorator "@convention" as such:

> “The block argument is used to indicate an Objective-C compatible block reference. The function value is represented as a reference to the block object, which is an id-compatible Objective-C object that embeds its invocation function within the object. The invocation function uses the C calling convention.”

This is not currently true; a block type cannot be converted to "AnyObject"

Steps to Reproduce:
1. Typealias a function type to include "@convention(block)" in Swift 2
2. Create a function taking a parameter of that function type
3. Attempt to cast the block to "AnyObject" using "as"
4. Follow compiler warning to use "as!" instead
5. Code traps at runtime

Expected Results:
The block type is converted to "AnyObject".

Actual Results:
Precondition failure: "Could not cast value of type '@convention(block) () -> ()' (0xSOMEADDR) to 'Swift.AnyObject' (0xSOMEADDR)."

Version:
Xcode 7.0 (7A218)/Apple Swift 2.0 (swiftlang-700.0.59 clang-700.0.72)

Notes:
A block can be passed to/casted from "id" in Objective-C, or bridged to Core Foundation as appropriate. In non-ObjC C, blocks are always pointer types.

Configuration:
Occurs on all releases of Swift.

Attachments:

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!