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-10102] Objective-C stub method not created for non-override of designated initializer #52504

Open
belkadan opened this issue Mar 13, 2019 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution

Comments

@belkadan
Copy link
Contributor

Previous ID SR-10102
Radar None
Original Reporter @belkadan
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, RunTimeCrash
Assignee None
Priority Medium

md5: f91d810d092893ba098c73f8965b0053

Issue Description:

In this contrived example using an underscored attribute, I can get into trouble with properties left uninitialized:

// Foo.swift
import Foundation

class Base: NSObject {
  @objc init(designated _: Int) {
    print("base designated")
    super.init()
  }
  @objc convenience init(convenience value: Int) {
    print("base convenience")
    self.init(designated: value)
  }
  @objc func doTheThing() {}
}

class Sub: Base {
  var value: Int
  @nonobjc @_nonoverride init(designated value: Int) {
    self.value = value
    super.init(designated: value)
  }
  override func doTheThing() {
    print("value = \(value)")
  }
}
// main.m
#import "Foo-Swift.h"

int main(void) {
  @autoreleasepool {
    Sub *sub = [[Sub alloc] initWithConvenience:42];
    [sub doTheThing]; // will print 0!
  }
}

If Sub declared a designated initializer with a different name from init(designated:), this would produce an error at run time saying that Sub did not implement init(designated:). However, the non-overriding non-ObjC declaration shadows that potential stub and keeps it from getting generated, which leads to memory unsafety.

We should fix this before productizing @_nonoverriding, or we should forbid @_nonoverriding on designated initializers (possibly just in @objc hierarchies).

@belkadan
Copy link
Contributor Author

@DougGregor: obviously not urgent but you might have an opinion.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 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 crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

2 participants