Uploaded image for project: 'Swift'
  1. Swift
  2. SR-6192

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

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
      None
    • 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 30a8e15328)
      Target: x86_64-apple-darwin17.0.0

      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>) // []
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                originaluser2 Hamish Knight
              • Votes:
                1 Vote for this issue
                Watchers:
                8 Start watching this issue

                Dates

                • Created:
                  Updated: