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-1024] Type of optionals cannot be inferred correctly in swift 2.2 #43636

Closed
swift-ci opened this issue Mar 22, 2016 · 6 comments
Closed

[SR-1024] Type of optionals cannot be inferred correctly in swift 2.2 #43636

swift-ci opened this issue Mar 22, 2016 · 6 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself optimized only Flag: An issue whose reproduction requires optimized compilation regression swift 2.2

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1024
Radar rdar://problem/25451728
Original Reporter sts2055 (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 7.3
Swift 2.2

Additional Detail from JIRA
Votes 7
Component/s Compiler
Labels Bug, 2.2Regression, OptimizedOnly
Assignee @jckarter
Priority Medium

md5: 2db3fa276c0751b3d29d768627bffb8d

is duplicated by:

  • SR-1113 Incorrect runtime dynamic cast of Optional.none to Optional.none when A&B are statically incompatible.

Issue Description:

Problem:

When running the following code under Xcode 7.3 with swift 2.2, the compiler is unable to correctly infer the type of the optional:

 import Foundation
    
    func whatAmI<T>(inout property:T?)
    {
        switch property {
        case is Int?:
            print("I am an Int?")
        case is String?:
            print("I am a String?")
        default:
            print("I don't know what I am")
        }
    }
    
    var string : String?
    whatAmI(&string)

On my side with Xcode 7.3 this will print `I am an Int?`

However, when I initialize the variable with an empty string before passing it to the function, the switch infers it to be a String?.

This would print `I am a String?` in the previous Xcode version.

Another example:

func genericMethod<T>(property: T?) {
    print(T) // String

    let stringNil = Optional<String>.None

    print(stringNil is String?) // true (warning - always true)    
    print(stringNil is T?) // true

    let intNil = Optional<Int>.None

    print(intNil is String?) // false (warning - always fails)
    print(intNil is T?) // true - BUG
}

genericMethod("")

Stackoverflow question: http://stackoverflow.com/questions/36160934/type-of-optionals-cannot-be-inferred-correctly-in-swift-2-2

@jckarter
Copy link
Member

The 7.3 behavior is correct, IMO. An optional containing `nil` can be cast to any other optional type, and the result is a nil of the destination type.

@jckarter
Copy link
Member

The 7.3 behavior is correct. A nil in an optional can be cast to a nil in any other optional type.

@swift-ci
Copy link
Collaborator Author

Comment by Ondrej Hanslik (JIRA)

@jckarter Please, look at the second example. The problem is not whether `nil` can be cast to any optional type (which is not actually true), the problem is inconsistency when the type is a generic type.

@jckarter
Copy link
Member

I see, you're right, it looks like there's an inconsistency there between the runtime cast for generics and the compiler-generated open-coded cast for Int? to String?. It should always succeed in either case.

@jckarter
Copy link
Member

Reopening to fix compiler case.

@slavapestov
Copy link
Member

The code example now exhibits consistent behavior between static and dynamic casts.

The bogus warnings are covered by https://bugs.swift.org/browse/SR-3505.

@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 optimized only Flag: An issue whose reproduction requires optimized compilation regression swift 2.2
Projects
None yet
Development

No branches or pull requests

4 participants