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-3505] Optional of a wrapped type with value nil is incorrectly considered to be an Optional of another wrapped type #46093

Closed
swift-ci opened this issue Dec 29, 2016 · 4 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. casting Feature: explicit casting (is, as, as? and as!) compiler The Swift compiler in itself Optional type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-3505
Radar None
Original Reporter goldsdad (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

OS X 10.11.6 (15G1212)
Xcode 8.2 (8C38)
IBM Swift Sandbox - Swift Ver. 3.0.2 (Release) - Platform: Linux (x86_64)

Additional Detail from JIRA
Votes 1
Component/s Standard Library
Labels Bug, TypeChecker
Assignee @slavapestov
Priority Medium

md5: e9915be07f69d31b7fe68a41773d1454

relates to:

  • SR-6192 Spurious "Cast from ... to unrelated type ... always fails" warning when implicitly converting an empty collection

Issue Description:

The output from the following code is reasonable to me except for the last line producing "true".

enum Foo<T> {  
    case some(T)  
    case none  
}  
  
print(  Foo<Int>.some(3) is Foo<Int>               ) // output: true  
print(  Foo<Int>.some(3) is Foo<String>            ) // output: false  
  
print(  Foo<Int>.none is Foo<Int>                  ) // output: true  
print(  Foo<Int>.none is Foo<String>               ) // output: false  
  
print(  Optional<Int>.some(3) is Optional<Int>     ) // output: true  
print(  Optional<Int>.some(3) is Optional<String>  ) // output: false  
  
print(  Optional<Int>.none is Optional<Int>        ) // output: true  
print(  Optional<Int>.none is Optional<String>     ) // output: true  

The compiler even issues warning "Cast from 'Optional<Int>' to unrelated type 'Optional<String>' always fails" for the last line, as it does for the lines producing "false", yet the result is "true".

@jtbandes
Copy link
Collaborator

Dug into this a little, but I'm out of my depth. By looking at the generated IR, I saw that swift_dynamicCast is called for the Optional conversion/check, but a call is not made in the Foo case. It looks like as of @atrick's changes in #204 this might actually be expected behavior for Optional (there's a comment saying // Allow Optional<T>.none -> Optional<U>.none.) However I agree this is counterintuitive.

@belkadan
Copy link
Contributor

belkadan commented Jan 6, 2017

@jckarter, we've seen this before, right?

@slavapestov
Copy link
Member

I thought this was intentional, and the Sema warning is wrong.

@slavapestov
Copy link
Member

#8876

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself casting Feature: explicit casting (is, as, as? and as!) Optional and removed standard library Area: Standard library umbrella labels Dec 13, 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. casting Feature: explicit casting (is, as, as? and as!) compiler The Swift compiler in itself Optional type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

5 participants