Skip to content

[SR-6126] Unexpected casting result in Array/Optional. #48681

@swift-ci

Description

@swift-ci
Previous ID SR-6126
Radar None
Original Reporter tarunon (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Xcode Version 9.0 (9A235)

Apple Swift version 4.0 (swiftlang-900.0.65 clang-900.0.37)

Target: x86_64-apple-macosx10.9

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee @tbkka
Priority Medium

md5: d7cc88bec6113716050efba120064739

duplicates:

  • SR-7358 Surprising inconsistency between optional conversion and array conversion

Issue Description:

I can check unexpected casting behavior combine Array and Optional.

func check(_ arg: Int??) -> String {
    switch arg {
    case .none: return ".none"
    case .some(.none): return ".some(.none)"
    case .some(.some): return ".some(.some)"
    }
}

let x: Int? = .none
let y: [Int?] = [.none]

let a = x as Int??
let b = (x as? Int??)!
let c = Int?.none as Int??
let d = (Int?.none as? Int??)!
let e = (y as [Int??]).first!
let f = (y as? [Int??])!.first!
let g = ([Int?.none] as [Int??]).first!
let h = ([Int?.none] as? [Int??])!.first!

// Every suspected results are .some(.none) but,

check(a) // .some(.none)
check(b) // .some(.none)
check(c) // .some(.none)
check(d) // .some(.none)
check(e) // .none
check(f) // .none
check(g) // .some(.none)
check(h) // .none

// Maybe its happen from this behavior

let z = String?.none as? Int? // .some(.none)
let zz = [String]() as? [Int] // []
let zzz = [String?.none] as? [Int?] // [nil]

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions