swiftlang/swift

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

開放

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

 (7 則留言) (0 個反應) (0 位負責人)Swift (10,719 個分叉)batch import
bugcompilergood first issue

倉庫指標

星標
 (69,989 顆星)
PR 合併指標
 (平均合併 8天 17小時) (30 天內合併 510 個 PR)

描述

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

貢獻者指南