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-4552] Implicitly Unwrapped Optional after casting to Any, can't be casted to Protocol it conforms to #47129

Closed
swift-ci opened this issue Apr 10, 2017 · 6 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself runtime The Swift Runtime

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-4552
Radar rdar://problem/31535455
Original Reporter lprzytula (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 8.3, Xcode 8.3.1

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Runtime
Assignee @tbkka
Priority Medium

md5: ef0c9e8e254e8f3add523707285a627b

relates to:

  • SR-8964 Type check operator (is) fails for Any! variable holding an Error (struct) value

Issue Description:

I have a protocol, and class:

protocol MyProtocol {
    func myFunc() -> Bool
}

class MyClass: MyProtocol {
    func myFunc() -> Bool {
        return true
    }
}

Then I create an implicitly unwrapped optional of this type, and cast it to 'Any':

let myObject: MyClass! = MyClass() // MyClass
let myAny: Any = myObject // MyClass

Now trying to cast the 'myAny' instance to the protocol 'MyProtocol' that it conforms to, produces 'nil', while casting 'myObject' to the same protocol returns proper object:

NSLog("\(myAny)") // MyClass
NSLog("\(myAny is MyClass)") // true
NSLog("\(myAny is MyProtocol)") // false
NSLog("\(myObject is MyProtocol)") // true
let myCasted1 = myAny as? MyProtocol // nil
let myCasted2 = myObject as? MyProtocol // MyClass
let myCasted3 = (myObject as Any) as? MyProtocol // nil
let myCasted4 = myAny as? MyClass // MyClass

Same code works properly with Swift 3.0

@belkadan
Copy link
Contributor

Hm, it does seem inconsistent to extract the class but not the protocol. Casting magister @jckarter?

@jckarter
Copy link
Member

@swift-ci create

@tbkka
Copy link
Contributor

tbkka commented Dec 17, 2019

This is similar to SR-3871, but not quite the same. SR-3871 specifically is about nested Any/AnyObject existential containers. This is similar but with Optionals in the mix. It interacts with the current `print()` implementation in complex ways, so it will take me a little while to work out a good approach here.

@tbkka
Copy link
Contributor

tbkka commented Jan 3, 2020

Related: PR #28994 fixes a bug in `print()` that was being covered up by this bug.

@tbkka
Copy link
Contributor

tbkka commented Aug 27, 2020

#33561 should fix this for the non-optimized case (where the cast is being handled by the runtime). There may still be issues with optimized casts, though.

@tbkka
Copy link
Contributor

tbkka commented Nov 12, 2020

Protocol conformance checks of objects stored in `Any` containers has been fixed in the `main` branch.

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

No branches or pull requests

4 participants