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-7187] False positive warning: cast always fails from '[Any]' to '[String?]' #49735

Closed
hamishknight opened this issue Mar 13, 2018 · 4 comments
Labels
compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-7187
Radar rdar://problem/50370862
Original Reporter @hamishknight
Type Sub-task
Status Closed
Resolution Done
Environment

Apple Swift version 4.2-dev (LLVM c4ec2ab808, Clang af436f313e, Swift 347ff6a)
Target: x86_64-apple-darwin17.4.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Sub-task, TypeChecker
Assignee None
Priority Medium

md5: 4ff77d5d9933586592b4e6c987ebccde

Parent-Task:

  • SR-13088 False positive warning "cast always fails"

is duplicated by:

  • SR-10589 Compiler warning about "cast always fails" but it doesn't

Issue Description:

The following code generates warnings that the casts will always fail, despite them succeeding at runtime:

let a: [Any] = [String?.some("hello") as Any, String?.none as Any]

// warning: Cast from '[Any]' to unrelated type '[String?]' always fails
print(a is [String?]) // true

// warning: Cast from '[Any]' to unrelated type '[String?]' always fails
print(a as? [String?] as Any) // Optional([Optional("hello"), nil])

Though if we are to make this consistent, we should reject the above code, as we currently reject type to more optional type casts:

let a: Any = String?.some("") as Any
print(a is String?) // error: Cannot downcast from 'Any' to a more optional type 'String?'

Either that or we should accept both cases without warnings (I'm not sure whether the downcast to more optional type error is intended in the case of existential and archetype operands).

@belkadan
Copy link
Contributor

cc @xedin, @rudkx

@xedin
Copy link
Member

xedin commented Mar 14, 2018

@rudkx It seems like this is only happening when optional types are involved.

@LucianoPAlmeida
Copy link
Collaborator

cc @hamishknight

@hamishknight
Copy link
Collaborator Author

Thanks again @LucianoPAlmeida!

@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
compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants