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-6192] Spurious "Cast from ... to unrelated type ... always fails" warning when implicitly converting an empty collection #48744

Closed
hamishknight opened this issue Oct 20, 2017 · 8 comments
Labels
compiler The Swift compiler in itself

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-6192
Radar None
Original Reporter @hamishknight
Type Sub-task
Status Closed
Resolution Done
Environment

Apple Swift version 4.0 (swiftlang-900.0.65.2 clang-900.0.37)
Target: x86_64-apple-macosx10.9

Swift version 4.1-dev (LLVM a85c7fe628, Clang 12a3c42f5f, Swift 30a8e15)
Target: x86_64-apple-darwin17.0.0

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

md5: 37ede6070a91cc1f085f6a2ddc028af2

Parent-Task:

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

is duplicated by:

  • SR-13277 Misleading warning when casting Array to unrelated Array

relates to:

  • SR-3505 Optional of a wrapped type with value nil is incorrectly considered to be an Optional of another wrapped type
  • SR-7738 Empty array type casting always succeeds

Issue Description:

The following generates warnings, despite the implicit conversions succeeding at runtime:

let items = [String]()
let dict = [String: Any]()
let set = Set<String>()

// warning: Cast from '[String]' to unrelated type '[Int]' always fails
print(items is [Int]) // true
print(items as? [Int] as Any) // Optional([])
print(items as! [Int]) // []

// warning: Cast from '[String : Any]' to unrelated type '[Int : Any]' always fails
print(dict is [Int: Any]) // true
print(dict as? [Int: Any] as Any) // Optional([:])
print(dict as! [Int: Any] as Any) // [:]

// warning: Cast from 'Set<String>' to unrelated type 'Set<Int>' always fails
print(set is Set<Int>) // true
print(set as? Set<Int> as Any) // Optional(Set([]))
print(set as! Set<Int>) // []
@belkadan
Copy link
Contributor

cc @slavapestov, @atrick

@slavapestov
Copy link
Member

Arguably the cast should fail even if the collection is empty. Otherwise we cannot provide useful diagnostics here at all.

We already have a silly corner case where casts between nil of unrelated types succeed...

@atrick
Copy link
Member

atrick commented Oct 21, 2017

The runtime calls back to a Swift function that implements the cast. See _arrayConditionalCast in ArrayCast.swift.

return try? source.map { try ($0 as? TargetElement).unwrappedOrError() }

Looks like we need to check not only the the element values are convertible, but also that the element types are somehow compatible. I'm not sure off hand how to do that.

@belkadan
Copy link
Contributor

Ah, the Optional case was admitting we can't fix this; this is pretty much the same. @jckarter probably remembers better than me.

@belkadan
Copy link
Contributor

(Sorry, I mean we can't have the casts fail. We can certainly fix the inconsistency.)

@atrick
Copy link
Member

atrick commented Oct 21, 2017

I think @slavapestov's point was that either we make the casts fail or we never diagnose collection mis-casts.

@LucianoPAlmeida
Copy link
Collaborator

@hamishknight

@hamishknight
Copy link
Collaborator Author

Fixed by #32716 which disables the warning.

@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
Projects
None yet
Development

No branches or pull requests

5 participants