swiftlang/swift

[SR-11905] Objective-C interop allows creation of undefined behavior

Open

#54,322 建立於 2019年12月5日

在 GitHub 查看
 (5 留言) (0 反應) (0 負責人)Swift (69,989 star) (10,719 fork)batch import
bugcompilergood first issue

描述

Previous ID SR-11905
Radar rdar://problem/57711780
Original Reporter @DevAndArtist
Type Bug

Apple Swift version 5.1.2 (swiftlang-1100.0.278 clang-1100.0.33.9)

Votes 1
Component/s Compiler
Labels Bug, StarterBug
Assignee faical (JIRA)
Priority Medium

md5: df1a976b8eb12c28ba3696248c7605f6

Issue Description:

In pure Swift this wouldn't be possible because we shouldn't be able to override a property of a class we import and don't own.

extension UITabBarController {
  open override var shouldAutorotate: Bool {
    return true
  }
}

However due to Objective-C interop the swift compiler does not complain about this extension and let us create an undefined behavior at runtime.

If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime. This is less likely to be an issue if you’re using categories with your own classes, but can cause problems when using categories to add methods to standard Cocoa or Cocoa Touch classes.

Source: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html

貢獻者指南