Skip to content

[SR-7664] Inconsistent optional casting behaviour with generics #50204

@hamishknight

Description

@hamishknight
Previous ID SR-7664
Radar rdar://problem/40171034
Original Reporter @hamishknight
Type Bug
Status Resolved
Resolution Done
Environment

Swift version 4.2-dev (LLVM d9cd6a5c49, Clang ed8876508e, Swift c363dad)
Target: x86_64-apple-darwin17.5.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 241ea289c2d537b07e80e0f58cf55573

is duplicated by:

  • SR-8037 Method generic type inference is broken in Swift 4.2

Issue Description:

This cast succeeds:

func foo(_ t: Int??) -> Int? {
  return t as? Int
}
let x = foo(Int??(3))
print(x as Any) // Optional(3)

but it fails when we use a generic placeholder satisfied by Int rather than Int directly. This cast fails:

func bar<U>(_ t: Int??, _: U.Type) -> U? {
  return t as? U
}

let y = bar(Int??(3), Int.self)
print(y as Any) // nil

Metadata

Metadata

Assignees

No one assigned

    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