Skip to content

[SR-8704] Cast from Any? to as? Value where Value == Any produces unexpected result in Xcode 10 #51216

@lilyball

Description

@lilyball
mannequin
Previous ID SR-8704
Radar rdar://problem/40916953
Original Reporter @lilyball
Type Bug
Status Closed
Resolution Done
Environment

Xcode 10.0 (10L221o)
Apple Swift version 4.2 (swiftlang-1000.0.32.1 clang-1000.10.39)
Target: x86_64-apple-darwin17.7.0

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

md5: 695e9648f10cd45d05e11cf6b2acc3b6

is duplicated by:

  • SR-8856 Optional downcast results in double optional during cast to generic value

relates to:

  • SR-7975 Fetching a nonexistent entry from a dictionary returns Optional when inferred type is Any
  • SR-4248 Runtime exception casting an Any? nil to a generic optional

Issue Description:

In a generic context, when casting from an Any? value to a generically-bound type using as? Value, if the generic type is equal to Any and the source value is nil, in Xcode 9 the result is nil but in Xcode 10 the result is .some(nil). This is true even when running with -swift-version 4.

Example:

func foo<Value>(_ x: Any?, as type: Value.Type) -> Value? {
    return x as? Value
}

dump(foo(nil, as: Any.self))

In Xcode 9.4.1 this prints

- nil

But in Xcode 10 beta 5 this prints

▿ Optional(nil)
  - some: 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