swiftlang / swift

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

[SR-11470] Swift ignores "unavailable" attribute of initializer declared in an ObjC protocol for a Swift subclass of ObjC class #53870

Open swift-ci opened 5 years ago

swift-ci commented 5 years ago
Previous ID SR-11470
Radar rdar://problem/55401820
Original Reporter westacular (JIRA User)
Type Bug

Attachment: Download

Environment Tested with identical results in both - Xcode 10.3 / Swift 5.0 - Xcode 11 GM / Swift 5.1
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, ClangImporter | |Assignee | None | |Priority | Medium | md5: eafdeed1bca5c71df626c723596ac258

relates to:

Issue Description:

If you have

... the Swift compiler will incorrectly require that the unavailable init method be implemented in the Swift subclass.

This does not appear to be an issue for a Swift class conforming to the protocol directly, or a Swift subclass of a Swift class that conforms.

Example:

@protocol SomeObjcProtocol <NSObject>

- (instancetype)initWithFoo:(BOOL)foo __attribute__((unavailable("do not use initWithFoo")));

@end

@interface SomeObjcClass : NSObject <SomeObjcProtocol>

@end

with an empty implementation of SomeObjcClass, we see

class SomeSwiftSubclass: SomeObjcClass {

    init(bar: Bool) {
        super.init()
    }

    // Error: "'required' initializer 'init(foo:)' must be provided by subclass of 'SomeObjcClass'"
}

class AnotherSwiftClass: NSObject, SomeObjcProtocol {

    init(bar: Bool) {
        super.init()
    }

    // No error
}

class AnotherSwiftSubclass: AnotherSwiftClass {

    init(baz: Int) {
        super.init(bar: true)
    }

    // No error
}

Inserting the fixit of

    required init(foo: Bool) {
        fatalError("init(foo:) has not been implemented")
    }

allows SomeSwiftSubclass to build. (In fact, it builds without a warning, but that's a separate issue: [SR-2317])

swift-ci commented 5 years ago

Comment by Wes Campaigne (JIRA)

Possibly related: SR-6993?

belkadan commented 5 years ago

Yeah. This one's going to be tricky to deal with without breaking source compatibility.

@swift-ci create