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-512] Cycle created when a convenience initializer that overrides a designated initializer invokes an inherited convenience initializer #43129

Open
swift-ci opened this issue Jan 10, 2016 · 2 comments

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Jan 10, 2016

Previous ID SR-512
Radar None
Original Reporter sflahave (JIRA User)
Type Bug

Attachment: Download

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

md5: eb715e88ccf3f5c8c3fc3862a39f6556

Issue Description:

The following code compiles, but fails at runtime. I tried in an XCode playground and REPL.
The Child class overrides all of the Parent class designated initializers, and therefore it automatically inherits all of the superclass convenience initializers. But the Parent convenience initializer reasonably delegates to it’s designated initializer, which has been overridden by the subclass. So it is not the Parent version of that 2-argument initializer that is invoked, but the overridden version. The overridden version takes advantage of the fact that the subclass has inherited Parent’s convenience initializer, and there it is - you have a cycle. I don’t know if this would be considered programmer-mistake or a compiler bug.

I'm using Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81).

class Parent {
  var parentProperty1:String
  var parentProperty2:String
  
  init(parentProperty1:String, parentProperty2:String) {
    self.parentProperty1 = parentProperty1
    self.parentProperty2 = parentProperty2
  }
  
  convenience init(parentProperty1:String) {
    print("in the Parent convenience initializer")
    self.init(parentProperty1:parentProperty1, parentProperty2:"nada")
  }
}

class Child : Parent {
  var childProperty:Double
  
  init(parentProperty1: String, parentProperty2:String, childProperty:Double) {
    self.childProperty = childProperty
    super.init(parentProperty1: parentProperty1, parentProperty2:parentProperty2)
  }
  
  // It seems that this trips up the compiler?
  convenience override init(parentProperty1:String, parentProperty2:String) {
    //this invokes the inherited convenience initializer, which invokes this overidden
    //version of the parent's designated initializer, which invokes the inherited convenience initializer,
    // cycle repeats until crash.
    print("in the subclass overridden convenience-version of the parent designated initializer")
    self.init(parentProperty1:parentProperty1)
  }
}

let child = Child(parentProperty1: "This is", parentProperty2: "the central scrutinizer")
print("\(child.parentProperty1)...\(child.parentProperty2). childProperty: \(child.childProperty)")
@belkadan
Copy link
Contributor

@belkadan belkadan commented Jan 11, 2016

@DougGregor, @slavapestov, something to consider if/when we refine the initializer model.

@Dante-Broggi
Copy link
Contributor

@Dante-Broggi Dante-Broggi commented Sep 20, 2018

This still occurs in the Xcode 10 toolchain.

@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
Projects
None yet
Development

No branches or pull requests

3 participants