Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[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 this issue Sep 13, 2019 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

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:

  • SR-6993 Swift imports @optional ObjC methods from superclass protocol as if implemented

Issue Description:

If you have

  • a Swift subclass of an ObjC class

  • where the ObjC class conforms to an ObjC protocol,

  • the protocol declares an init method marked as unavailable,

  • the ObjC class (correctly) does not implement the unavailable init method,

  • and the Swift subclass declares its own init method

... 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
Copy link
Collaborator Author

Comment by Wes Campaigne (JIRA)

Possibly related: SR-6993?

@belkadan
Copy link
Contributor

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

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants