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-10285] Subclass of a class that conforms to a protocol and extends NSObject is not instantiate correctly #52685

Closed
swift-ci opened this issue Apr 3, 2019 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 5.0

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Apr 3, 2019

Previous ID SR-10285
Radar rdar://problem/49560721
Original Reporter ilmig (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Xcode Version 10.2 (10E125)

macOS Mojave 10.14.4 (18E226)

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Bug, 5.0Regression, Miscompile
Assignee None
Priority Medium

md5: 09d4b39f35911f43b25fc7ed84660b4a

is duplicated by:

  • SR-10338 Swift 5 regression: initializing NSObject returns wrong instance
  • SR-10457 Xcode 10.2/Swift 5.0 init() behavior change => Unexpected type initialized at runtime.
  • SR-10558 Failed to cast to a concrete class type after sequence's init(elements: Sequence)
  • SR-10617 polymorphism breaks when a generic instantiates its protocol-constrained parameterized type
  • SR-10849 (fixed in Xcode 11) Type metadata dispatches to wrong init
  • SR-10958 Swift 5 regression with init delegation
  • SR-11416 Protocol behavior in Swift 5, regression?

Issue Description:

Hi, I have this following code

import Foundation

protocol MyProtocol {
    init()
}

@objc(ClassA)
class ClassA: NSObject, MyProtocol {
    
    override required init() {
        super.init()
    }
}

@objc(ClassB)
class ClassB: ClassA {

}

private func myMethod(name: String) -> MyProtocol {
    guard let klass = NSClassFromString(name) as? MyProtocol.Type else {
        fatalError()
    }
    return klass.init()
}

let myClass = myMethod(name: "ClassB")

print(myClass)

The result of myMethod is an instance of ClassA and not ClassB as expected. This code worked compiled with Xcode 10.1, but now it doesn't. Any advice? Thanks

@belkadan
Copy link
Contributor

belkadan commented Apr 3, 2019

Yikes! Thanks, Fabio.

@slavapestov
Copy link
Member

#24343

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Aug 9, 2019

ilmig (JIRA User), Could you verify if the problem is fixed and if so move the JIRA to "Closed"?

Thanks!
Anna

@swift-ci
Copy link
Collaborator Author

swift-ci commented Sep 7, 2019

Comment by Fabio Mignogna (JIRA)

@AnnaZaks
Apologies for this long delay.
I have verified on the latest Xcode 11 beta 7 and it's fixed. Thanks for the quick turnaround.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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 regression swift 5.0
Projects
None yet
Development

No branches or pull requests

4 participants