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-10823] type(of:) cannot extract the type erased with Any #53213

Open
DevAndArtist mannequin opened this issue Jun 3, 2019 · 1 comment
Open

[SR-10823] type(of:) cannot extract the type erased with Any #53213

DevAndArtist mannequin opened this issue Jun 3, 2019 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@DevAndArtist
Copy link
Mannequin

DevAndArtist mannequin commented Jun 3, 2019

Previous ID SR-10823
Radar None
Original Reporter @DevAndArtist
Type Bug
Environment

Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)
Target: x86_64-apple-darwin18.6.0

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

md5: 28e4c63a4392471a79a2bbe4ebaf35e0

Issue Description:

fileprivate protocol Wrapper {
  static func baseType(level: inout Int) -> Any.Type
  func baseType(level: inout Int) -> Any.Type
}

extension Optional: Wrapper {
  fileprivate static func baseType(level: inout Int) -> Any.Type {
    level += 1
    switch Wrapped.self {
    case let wrapper as Wrapper.Type:
      return wrapper.baseType(level: &level)
    default:
      return Wrapped.self
    }
  }

  func baseType(level: inout Int) -> Any.Type {
    level += 1
    switch self {
    case .some(let wrapper as Wrapper):
      return wrapper.baseType(level: &level)
    case .some(let wrapped):
      return type(of: wrapped)
    case .none:
      return Optional.baseType(level: &level)
    }
  }
}

let wrappedValue: String? = "Swift"
let test_1: Any?? = wrappedValue
let test_2: Any?? = Optional(wrappedValue as Any)

var level = 0
// THIS SHOULD BE `String 2` not `Any 2`
print(test_1.baseType(level: &level), level) // prints `Any 2`

level = 0
print(test_2.baseType(level: &level), level) // prints `String 3`

Discussion on the forums where the bug first appeared: https://forums.swift.org/t/challenge-finding-base-type-of-nested-optionals/25096

@belkadan
Copy link
Contributor

belkadan commented Jun 3, 2019

@jckarter, there's a dup of this that explains the behavior, right?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

1 participant