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-7882] Runtime crash "class does not implement methodSignatureForSelector:" in swift class #50417

Closed
swift-ci opened this issue Jun 5, 2018 · 6 comments
Assignees
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 regression run-time crash Bug → crash: Swift code crashed during execution runtime The Swift Runtime swift 4.2

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jun 5, 2018

Previous ID SR-7882
Radar rdar://problem/40819319
Original Reporter Lebedev (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Version 10.0 beta (10L176w)

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, 4.2Regression, RunTimeCrash, Runtime
Assignee @DougGregor
Priority Medium

md5: 7eba6441ab82265b80ef487da07ef525

is duplicated by:

  • SR-7971 Swift 4.2 beta | @objc protocol conformance check can incorrectly fail
  • SR-8131 4.2 Regression: Subclass of non-NSObject class with at-objc method not emitting ObjC class metadata

Issue Description:

extension Notification.Name {
    static let testNotification = Notification.Name("test")
}

class Foo {
    // 1. Changing type to Int, String, etc. prevents crash
    var whyDoIcrashOnlyWithDate: Date?

    init() {
        NotificationCenter.default.addObserver(
          self, 
          selector: #selector(handleNotification), 
          name: .testNotification, 
          object: nil
        )
    }
}
// 2. Having handling in extension is important, doing it in class prevents crash
extension Foo {
    @objc func handleNotification() {}
}

// execution
NotificationCenter.default.post(name: .testNotification, object: nil)

Produces runtime crash:

NSForwarding: warning: object 0x60000113eb00 of class 'RuntimeCrash.Foo' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[RuntimeCrash.Foo handleNotification]

Another workaround I've found is to inherit the class from `NSObject`.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 5, 2018

Comment by Denis Lebedev (JIRA)

/cc @xedin

@belkadan
Copy link
Contributor

belkadan commented Jun 5, 2018

Can you share your source? Just so we're sure we're looking at the same bug.

@swift-ci create

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 5, 2018

Comment by Denis Lebedev (JIRA)

@belkadan my initial description certainly lacks some important bits as I could not reproduce it in test project yet, I'll attach the source once I finish.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 6, 2018

Comment by Denis Lebedev (JIRA)

I've managed to reduce attached example to absolute minimum: turns out `@objc` was not as important as having 2 things:

  1. a variable in the class (in my project's code it was heavily nested struct but I was able to delete everything and leave only Date)

  2. Handler method implemented in extension

@mikeash
Copy link
Contributor

mikeash commented Jun 6, 2018

Thanks a lot, that reduced case is great, and I got it to crash here.

@slavapestov
Copy link
Member

#17197

@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
This issue was closed.
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 regression run-time crash Bug → crash: Swift code crashed during execution runtime The Swift Runtime swift 4.2
Projects
None yet
Development

No branches or pull requests

5 participants