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-5590] Failed to cast to protocol #48162

Closed
swift-ci opened this issue Jul 31, 2017 · 7 comments
Closed

[SR-5590] Failed to cast to protocol #48162

swift-ci opened this issue Jul 31, 2017 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-5590
Radar None
Original Reporter asav (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

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

md5: fb4d4c41f8dc355494d9047a72ba6b0a

duplicates:

  • SR-3871 Protocol passing via objective-c / Any can't be cast back to protocol type

Issue Description:

Casting to a protocol fail when function is implemented in NSObject via extension but succeed when is Swift Class.

Output:

v is MyProtocol = true

  • start() entry = <NSObject: 0x600000011330>, value = Optional(__lldb_expr_5.MyEnum.b)
    NSObject.entry() with value = Optional(__lldb_expr_5.MyEnum.b) of type = Optional<Any>
    fail to cast Optional<Any> to MyProtocol

  • start() entry = **lldb_expr_5.SomeClass, value = Optional(_lldb_expr_5.MyEnum.b)
    SomeClass.entry() with value = Optional(
    _lldb_expr_5.MyEnum.b) of type = Optional<Any>
    OK 1

@swift-ci
Copy link
Collaborator Author

Comment by Abel Sanchez (JIRA)

Here I attach a "working" code I copied from Playground.

@belkadan
Copy link
Contributor

I'm not seeing this in Xcode 9 beta 4. What version are you testing with?

@swift-ci
Copy link
Collaborator Author

Comment by Abel Sanchez (JIRA)

XCode Version 8.3.3 (8E3004b)
More details if 'value' is struct o enum (As its in sample code 'MyEnum') its lose protocol witness information but if is class its cast properly.

@belkadan
Copy link
Contributor

Ah, that sounds like SR-3871. But the difference between NSObject and a Swift class seems surprising to me.

@belkadan
Copy link
Contributor

Aha, right, the NSObject version is going through Objective-C dispatch, and the Swift-only version is not. And I didn't see it because in Swift 4 you have to explicitly write @objc if you want an extension method on an Objective-C class to be exposed to Objective-C. Okay, this is SR-3871 again.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Aug 1, 2017

Comment by Abel Sanchez (JIRA)

Thanks @belkadan It looks like SR-3871.

@tbkka
Copy link
Contributor

tbkka commented Jan 29, 2020

Here's a trimmed-down reproducer:

import Foundation

protocol MyProtocol { }
enum MyEnum: Int, MyProtocol {
    case b
}

// ObjC method can't cast the value out of the Any?
extension NSObject {
    @objc func entry(value: Any?) -> Bool {
        value is MyProtocol
    }
}

// Native class for comparison; this works correctly.
class SomeClass {
    func entry(value: Any?) -> Bool {
        value is MyProtocol
    }
}

let v = MyEnum.b
print(NSObject().entry(value: v)) // Prints "false", should be "true"
print(SomeClass().entry(value: v)) // Prints "true", should be "true"

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

No branches or pull requests

3 participants