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-11072] Use of unimplemented initializer 'init(style:)' for class #53464

Open
swift-ci opened this issue Jul 5, 2019 · 1 comment
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jul 5, 2019

Previous ID SR-11072
Radar rdar://problem/52477337
Original Reporter AnotherJhonDoe (JIRA User)
Type Bug
Environment

Xcode 11 beta 2 and Xcode 11 beta 3

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 3b9e785a47170186bc85fa1a309f2d7f

Issue Description:

As seen in: https://stackoverflow.com/questions/56828037/use-of-unimplemented-initializer-initstyle-for-class-with-xcode-11-beta-2. Not sure if it's a Swift bug, or Xcode, but something is definitely going wrong. I copy paste the question from above link here:

I get the error

> Use of unimplemented initializer 'init(style🙂' for class
{{> }}
> Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

I did not get this error on every previous Xcode I have worked with. I should not get this error, because I am using my own designated initializer. The error can be reproduced by running this git repo: https://github.com/Jasperav/EasyCrash. The relevant code is:

import UIKit
 
 class ViewController: UIViewController {
 
     override func viewDidAppear(_ animated: Bool) {
         Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { (_) in
             let someInitializable: UIViewControllerInitializable.Type = SomeViewController.self
             self.present(someInitializable.init(), animated: true, completion: nil)
         }
     }
 }
 
 protocol Initializable {
     init()
 }
 
 protocol UIViewControllerInitializable: Initializable, UIViewController {}
 
 class BaseTableViewController: UITableViewController {
     let a: Int
 
     init(a: Int) {
         self.a = a
 
         super.init(style: .plain)
     }
 
 required init?(coder: NSCoder) {
     fatalError()
     }
 }
 
 final class SomeViewController: BaseTableViewController, UIViewControllerInitializable {
     init() {
     print("init")
 
     super.init(a: 10)
 }
 
 required init?(coder: NSCoder) {
     fatalError()
     }
 }

What have been changed with Xcode 11 beta 2? Is this a bug or should I change my code?
https://i.stack.imgur.com/JJeKt.png

@belkadan
Copy link
Contributor

The Swift compiler's preferring the initializer on the class over the initializer on the protocol, which is the right thing to do with concrete types but the wrong thing here (i.e. it's a compiler bug). You can work around it by adding a convenience helper to the protocol:

extension Initializable {
  static func initWorkaround() -> Self {
    return self.init()
  }
}

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants