Open swift-ci opened 7 years ago
I've observed a similar bug here: https://github.com/AliSoftware/Reusable/pull/40
In short, in an extension with a constraint Self: UIView
the compiler allows to call Self()
(aka Self.init()
) — which could make sense because Self
inherits UIView
which inherits NSObject
which declares init()
— but that crashes at runtime.
protocol NibOwnerLoadable { … }
public extension NibOwnerLoadable where Self: UIView {
@discardableResult
static func loadFromNib(owner: Self = Self()) -> Self {
…
return owner
}
}
class MyCustomWidget: UIView, NibOwnerLoadable {
init?(coder aDecoder: NSCoder) { … }
}
MyCustomWidget.loadFromNib() // This compiles but crashes at runtime because it tries to call MyCustomWidget.init() which isn't defined
Comment by Caleb Davenport (JIRA)
Going to start looking here (https://github.com/apple/swift/blob/master/lib/Sema/CSApply.cpp#L6273) when I get some free time.
Hm, right. Initializers are inherited in Objective-C, but not always in Swift. That means that <T: NSObject>
shouldn't be assumed to have init()
, even though NSObject does.
That said, fixing this would be a source-breaking change, so we'll have to limit it to Swift 4 mode.
Environment
Xcode 8.2.1, Swift 3.0.2Additional Detail from JIRA
| | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, AcceptsInvalid | |Assignee | None | |Priority | Medium | md5: 41db62f705952a6f4afb42f3ddb6d9afIssue Description:
This code snippet compiles successfully but causes a crash at runtime. It appears to only happen in the presence of NSObject as a pure Swift example emits the proper compile-time error.