[SR-10498] Warn about implementing @optional Objective-C protocol methods in a Swift subclass of a class that conforms to that protocol #52898
Labels
attributes
Feature: Declaration and type attributes
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler in itself
conformances
Feature → protocol: protocol conformances
duplicate
Resolution: Duplicates another issue
@objc
Feature → attributes: The @objc attribute
objective-c interop
Feature: Interoperability with Objective-C
regression
swift 5.0
type checker
Area → compiler: Semantic analysis
unexpected behavior
Bug: Unexpected behavior or incorrect output
whole module optimization only
Flag: An issue whose reproduction requires whole module optimization
Environment
Xcode Version 10.2.1 (10E1001)
macOS 10.14.4
Additional Detail from JIRA
md5: c866fe67ee173d629a2501ffc7f4ee23
duplicates:
Issue Description:
Description
If you have an abstract Swift class that conforms to an Objective-C protocol, in the test case here UITableViewDataSource, which has both @required and @optional methods, implementing any optional method of that protocol in a Swift subclass will fail silently on 'Release' runtime without any compile-time warning.
Actual results
If you implement something as the below test case, which is probably very common, the Objective-C implementation of UITableView in this case (or any Objective-C implementation) will fail to detect that you actually implement that Objc-C optional protocol. You will not get your section headers in this case. This only happens under certain compilation conditions.
Expected results
The compiler should warn you at compile time that a superclass conforms to a protocol that has the same method signature marked as an Objective-C optional and you need to declare it as `@objc` for it to be exposed at runtime.
Reproduction Steps:
I could only reproduce this bug with the standard Release configuration in Xcode for Swift projects. Optimization level or other Swift compiler settings are definitely at play with this interaction with UIKit and perhaps all similar Objective-C runtime optional checking implementations. It took me significant time to catch this bug because of this and because there is absolutely no compiler warning about this.
Please note the item below is not a full test case for UITableView, but it compiles and shows that Swift does not warn about implementing these method signatures that will do nothing at runtime.
A RDAR link is attached, for whatever it's worth.
Compilation Mode must be Whole Module Optimization.
Search metadata:
`UITableView titleForHeader not getting called`, `UITableViewDelegate Swift not getting called`, `UITableViewDataSource Swift not getting called`
The text was updated successfully, but these errors were encountered: