[SR-7182] Ownership keyword removal from protocols breaks ObjC implementations #49730
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler in itself
regression
swift 4.1
Additional Detail from JIRA
md5: 8ce845df43dbbfb365a932dd18c900d2
Issue Description:
(Reporting this as requested by Jordan Rose in https://forums.swift.org/t/ownership-keyword-removal-in-protocols-source-breaking-for-objc-implementations/10743)
“SE-0176-0186: Remove ownership keyword support in protocols” (Pull Request, Proposal2, Discussion) made it a warning to specify
weak
on Swift properties (vars) in protocols.In other words this now generates a warning:
Instead, weak can be omitted from the protocol declaration, and Swift implementions of this protocol can redeclare the property as weak themselves:
However, this is a problem for ObjC implementations (as of the Xcode 9.3 betas which brought in this warning) since the compiler complains if you redeclare the property and change the strong/weakness:
Alternatively in a private category:
Alternatively synthesizing into a different ivar with __weak:
The only alternative that works is to override the setter and getter for the property and back it with a weak ivar/property. This is ugly and tedious.
Note that even when leaving
weak
on the Swift protocol property and ignoring the warning, the generated "-Swift.h" header still declares the property as strong:We build with “warnings as errors” for ObjC, so this is a source breaking change for our existing code. It doesn’t seem like it would be a good idea to ignore the Wproperty-attribute-mismatch warning. This is a problem for any existing ObjC code that implements Swift protocols like this.
Now, there are legitimate design discussions to be had over whether a delegate property belongs on an abstract protocol, but given how common the (weak) delegate pattern is in ObjC I believe there will be a significant amount of this kind of code in the wild (we ran into this at least).
The text was updated successfully, but these errors were encountered: