Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-2932] Swift unconditionally bridges obj-c parameters to blocks even when they're ignored #45526

Open
lilyball mannequin opened this issue Oct 12, 2016 · 3 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software regression run-time crash Bug → crash: Swift code crashed during execution swift 3.0

Comments

@lilyball
Copy link
Mannequin

lilyball mannequin commented Oct 12, 2016

Previous ID SR-2932
Radar None
Original Reporter @lilyball
Type Bug

Attachment: Download

Environment

Apple Swift version 3.0 (swiftlang-800.0.46.2 clang-800.0.38)
Target: x86_64-apple-macosx10.9

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 3.0Regression, RunTimeCrash
Assignee None
Priority Medium

md5: 2d06175c0679d4dcddde6c516d2572d6

Issue Description:

When Obj-C declares a completion block that takes e.g. an NSDictionary, and Swift code provides the closure, the compiler automatically bridges the parameters from Obj-C to Swift, turning that NSDictionary into [AnyHashable: Any] (or in Swift 2.3, [NSObject: AnyObject]). In Swift 2.3, if the block ignores the parameter, this bridging does not actually happen. In Swift 3.0, it happens even though the parameter is unused. Not only is this unnecessary work that can adversely affect performance, it's also a potential crash if the Obj-C code passes an object of the wrong type to the block (which is what actually happened in our app).

I've attached an Xcode project demonstrating this. It declares an Obj-C class that accepts a completion block, and it passes an object of the wrong type to the completion block. It has schemes for both Swift 2.3 and Swift 3.0. In Swift 2.3, the program executes just fine (since it ignores the parameter). In Swift 3.0 it crashes in Dictionary._unconditionallyBridgeFromObjectiveC because it's sending an unrecognized selector to NSData. The Swift code looks like

BadClass().crash({ _ in
    print("this block ignores its parameter")
})
@belkadan
Copy link
Contributor

I feel like this is not something that should be relied upon. Even if we were to optimize it, an unoptimized build would still do the bridging.

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Oct 12, 2016

I agree that this shouldn't be relied-upon for correctness, instead treated as an optimization, but it did bite us and it's probably going to bite someone else too.

Even if we were to optimize it, an unoptimized build would still do the bridging.

Even if the parameter isn't even given a name? I could see bridging named parameters so you can see them in the debugger even though they're not used, but if the parameter isn't even given a name then there's no way to reference it.

@belkadan
Copy link
Contributor

I could see the debugger providing something like $arg1 the way it does at the assembly level.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software regression run-time crash Bug → crash: Swift code crashed during execution swift 3.0
Projects
None yet
Development

No branches or pull requests

2 participants