swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.29k stars 10.33k forks source link

[SR-12402] Add warning about overriding ObjC variables and funds through extensions in Swift #54840

Open swift-ci opened 4 years ago

swift-ci commented 4 years ago
Previous ID SR-12402
Radar rdar://problem/62200983
Original Reporter marcetcheverry (JIRA User)
Type Improvement
Environment Swift 5.2, Xcode 11.4 MAS
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | | |Labels | Improvement | |Assignee | None | |Priority | Medium | md5: 51a14dc2e0b979e10e1e74bb2ae8e290

Issue Description:

This code works in 5.1.3 (Xcode 11.3.1), but does not work in Swift 5.2 (Xcode 11.4, iOS 13.4)

extension UINavigationController {
    open var topViewController: UIViewController? {
        NSLog("topViewController is overriden")
        return nil
    }

    open override var childForStatusBarStyle: UIViewController? {
        NSLog("childForStatusBarStyle is overriden")
        return nil
    }

Note that the `topViewController` example is just an example. The second case, of `childForStatusBarStyle` is used widely and in many StackOverflow answers.

Regardless of the safety and how wise it is to use this pattern, there should not be a silent regression from one Swift version to the other.

Please, let’s add a warning either at compile time or runtime that this unsafe behavior and only available for compatibility reasons.

swift-ci commented 4 years ago

Comment by Ondrej Hanslik (JIRA)

You are misusing the language. Such a thing wouldn't be safe to do even in Objective-C. Use a subclass instead. The fact that some StackOverflow answers recommend such a thing is not a valid reason.

swift-ci commented 4 years ago

Comment by Marc Etcheverry (JIRA)

As I said on the ticket, I know this. Doing this with a category in ObjC is wrong, and Swift possibly implements this through categories if extending ObjC classes. The issue is about the regression, either in Swift 5.2 or iOS 13.4, and the lack of a warning. The fact that everybody does this without being warned at compile time or runtime is a bug.

beccadax commented 4 years ago

@swift-ci create

beccadax commented 4 years ago

marcetcheverry (JIRA User) I can't seem to make this fail to compile. Could you attach a sample project, or at least give the text of the error you're getting so I can look into this change?