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-10337] Swift 5 regression: Incorrect type cast from AnyObject to incompatible NSObject subclass. #52737

Closed
swift-ci opened this issue Apr 8, 2019 · 8 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. regression runtime The Swift Runtime swift 5.0

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Apr 8, 2019

Previous ID SR-10337
Radar rdar://problem/49698830
Original Reporter Leitch (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment

Xcode 10.2 (10E125)

Apple Swift version 5.0 (swiftlang-1001.0.69.5 clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug, 5.0Regression, Runtime
Assignee None
Priority Medium

md5: 115bba5912ffd2e281835116b906c630

Issue Description:

The following code crashes in Xcode 10.2 as the model is incorrectly cast to an NSManagedObject instance:

Unrecognized selector -[__lldb_expr_52.MyModel objectID]

This is not a problem in Xcode 10.1.

import CoreData

public protocol MyProtocol: class {}
public class MyModel: MyProtocol {}

let model: AnyObject = MyModel()

func perform() {
    guard let managedModel = model as? (NSManagedObject & MyProtocol) else { return }
    print(managedModel.objectID)
}

perform()
@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 8, 2019

Comment by Ian Leitch (JIRA)

Actually this doesn't appear to be unique to just NSManagedObject, it's an issue for any NSObject subclass.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 8, 2019

Comment by Ian Leitch (JIRA)

AnyObject appears to be the culprit, the snippet works as expected when using just Any.

@theblixguy
Copy link
Collaborator

Reproduced on 5.1 as well:

2019-04-08 16:22:52.727 swift[75171:9957983] *** NSForwarding: warning: object 0x7fc281d4e2a0 of class 'whats_wrong.MyModel' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[whats_wrong.MyModel objectID]

@belkadan
Copy link
Contributor

belkadan commented Apr 8, 2019

cc @mikeash

@mikeash
Copy link
Contributor

mikeash commented Apr 8, 2019

Most curious! Thank you for the small reproducer.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 8, 2019

Comment by Ian Leitch (JIRA)

This is also an issue when running 4.2 compatibility mode.

@belkadan
Copy link
Contributor

belkadan commented Apr 8, 2019

Yeah, that's a compilation-time setting. You're still running with a Swift 5.0 runtime and standard library in that case.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 8, 2019

Comment by Ian Leitch (JIRA)

@belkadan Thanks for pointing that out, I didn't realize it was a compile-time only setting.

@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. regression runtime The Swift Runtime swift 5.0
Projects
None yet
Development

No branches or pull requests

5 participants