swiftlang / swift

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

[SR-9456] ObjC init marked NS_UNAVAILABLE is called from Swift when a 0-ary factory method is added #51919

Open swift-ci opened 5 years ago

swift-ci commented 5 years ago
Previous ID SR-9456
Radar rdar://problem/45304416
Original Reporter lucatorella (JIRA User)
Type Bug

Attachment: Download

Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, ClangImporter | |Assignee | None | |Priority | Medium | md5: 36bc2e8ec7bacc7ccce7f21da3dc15a7

Issue Description:

If I have an Objective-C class where I have an init method marked as NS_UNAVAILABLE and a factory method with no arguments and if I try to call that factory method from Swift, then the init method is invoked instead.

Steps to Reproduce:
Create the following Objective-C class:

@interface SCNObject : NSObject
+ (instancetype)object;
- (instancetype)init NS_UNAVAILABLE;
@end

From Swift do:

_ = SCNObject()

It's easy to check with the debugger that the init method is called instead of the factory method.

If I remove the factory method, then the code won't compile and the proper error message is displayed: "'init()' is unavailable"

hamishknight commented 5 years ago

Interestingly enough, swapping the decls makes Swift call the factory method, e.g

@interface SCNObject : NSObject
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)object;
@end
swift-ci commented 5 years ago

Comment by Luca Torella (JIRA)

Wow, I didn't notice that 🙂

belkadan commented 5 years ago

Hmph, I thought we fixed this. Thanks, Luca.