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-912] Runtime exception casting an Any nil to an Optional. #43524

Closed
swift-ci opened this issue Mar 10, 2016 · 5 comments
Closed

[SR-912] Runtime exception casting an Any nil to an Optional. #43524

swift-ci opened this issue Mar 10, 2016 · 5 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution runtime The Swift Runtime standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-912
Radar None
Original Reporter astmus (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

xcode 7.3 beta 5
os x 10.11.3

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug, RunTimeCrash, Runtime
Assignee @atrick
Priority Medium

md5: dfee632a8b003f40faceb3a233e6d950

Issue Description:

Example

Code below demonstrate type casting of 'Any' object to generic type. Conversion completed successful if optional types have concrete not nil value. If object value equal to nil then conversion to Int32? type by "as!" operator will throw exception.

Code Example

public class Cast<T>
{
    public func printConcrete(obj:Any)
    {
        print(obj as! T)
    }   
    public func printOptional(obj:Any)
    {
        print(obj as? T)
    }
}

let cast = Cast<Int32?>()
var oin : Int32? = 1
cast.printOptional(oin) // Optional(Optional(1))
cast.printConcrete(oin) // Optional(1)
oin = nil
cast.printOptional(oin) // nil
cast.printConcrete(oin) // Could not cast value of type 'Swift.Optional<Swift.Int32>' (0x10f475148) to 'Swift.Int32' (0x105eafd60).

Result for xCode Version 7.2 (7C68) / osX 10.10.5 (14F1509)

as expected code above gave next result

Optional(Optional(1))
Optional(1)
Optional(nil)
nil

@belkadan
Copy link
Contributor

@jckarter, any ideas?

@jckarter
Copy link
Member

@atrick, you fixed this in 7.3, right?

@atrick
Copy link
Member

atrick commented Mar 10, 2016

I handled the cast of casting Optional -> Optional, but did not handle the case in this example. This case casts an optional boxed within existential to another optional.

My implementation first checks if the source of the cast is optional. If not, it strips the optional from the target and recursively attempts the cast. I thought this was OK because Optional would never directly conform to a protocol. I don't have a better idea here than adding another case to handle Any existentials...

I can try to look at it sometime in the next few days, but any help or suggestions are appreciated.

@jckarter
Copy link
Member

No rush, just checking to see if this was already fixed.

@atrick
Copy link
Member

atrick commented Mar 23, 2016

Fixed in
#1799
atrick merged commit 043a85e into apple:master

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 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. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution runtime The Swift Runtime standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

5 participants