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
Comments
Comment by Abel Sanchez (JIRA) Here I attach a "working" code I copied from Playground. |
I'm not seeing this in Xcode 9 beta 4. What version are you testing with? |
Comment by Abel Sanchez (JIRA) XCode Version 8.3.3 (8E3004b) |
Ah, that sounds like SR-3871. But the difference between NSObject and a Swift class seems surprising to me. |
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 |
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" |
Attachment: Download
Environment
Additional Detail from JIRA
md5: fb4d4c41f8dc355494d9047a72ba6b0a
duplicates:
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
The text was updated successfully, but these errors were encountered: