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-481] Can not retrieve Optinals from [String:Any] Dictionary #43098

Closed
swift-ci opened this issue Jan 6, 2016 · 8 comments
Closed

[SR-481] Can not retrieve Optinals from [String:Any] Dictionary #43098

swift-ci opened this issue Jan 6, 2016 · 8 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jan 6, 2016

Previous ID SR-481
Radar None
Original Reporter deggert (JIRA User)
Type Bug
Environment

% swift --version
Apple Swift version 2.2-dev (LLVM 3ebdbb2c7e, Clang f66c5bb67b, Swift 1f2908b)
Target: x86_64-apple-macosx10.9

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

md5: c2eb4f13cf4cd3284a0acf11ec69bd95

Issue Description:

After storing nil as Int? into a [String:Any] the retrieved value can not be downcast (back) to Int?:

Welcome to Apple Swift version 2.2-dev (LLVM 3ebdbb2c7e, Clang f66c5bb67b, Swift 1f2908b4f7). Type :help for assistance.
  1> let d: [String:Any] = ["foo": (nil as Int?)]
d: [String : Any] = 1 key/value pair {
  [0] = {
    key = "foo"
    value = {
      payload_data_0 = 0x0000000000000000
      payload_data_1 = 0xffffffffff000001
      payload_data_2 = 0x0000000100601390
      instance_type = 0x00000001004f6028
    }
  }
}
  2> let v = d["foo"]
v: Any? = Some {
  payload_data_0 = 0x0000000000000000
  payload_data_1 = 0x0000000000000001
  payload_data_2 = 0x0000000000000000
  instance_type = 0x00000001004f6028
}
  3> v! as Int?
repl.swift:3:2: error: cannot convert value of type 'Any' (aka 'protocol<>') to type 'Int?' in coercion
v! as Int?
~^
@swift-ci
Copy link
Collaborator Author

swift-ci commented Jan 8, 2016

Comment by Alex Johnson (JIRA)

I see this as well using Swift 2.2 (Jan 6th snapshot). It looks like something has changed in the way that nil is stored. It seems to be converted to an instance of Any, which is confusing to me because that shouldn't be possible.

Compare the result of the dictionary creation in Swift 2.2:

  1> let d: [String:Any] = ["foo": (nil as Int?)]
d: [String : Any] = 1 key/value pair {
  [0] = {
    key = "foo"
    value = {
      payload_data_0 = 0x0000000000000000
      payload_data_1 = 0x0000000100602701
      payload_data_2 = 0x0005000000000000
      instance_type = 0x00000001004f5028
    }
  }
}

... to the result in Swift 2.1:

  1> let d: [String:Any] = ["foo": (nil as Int?)]
d: [String : Any] = 1 key/value pair {
  [0] = {
    key = "foo"
    value = nil
  }
}

And compare the result of accessing the value in Swift 2.2:

  3> let v = d["foo"]!
v: Any = {
  payload_data_0 = 0x0000000000000000
  payload_data_1 = 0x0000000000000001
  payload_data_2 = 0x0000000000000000
  instance_type = 0x00000001004f5028
}

... to the result in Swift 2.1:

  3> let v = d["foo"]!
v: Int? = nil

@Dante-Broggi
Copy link
Contributor

Is this resolved, or no longer valid? If either, this should be closed.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis
Copy link
Collaborator

The coercion in the original example is diagnosed correctly—you cannot coerce from Any to a concrete type. Even though the output differs, nothing seems to suggest that there was an issue with downcasting the value instead.

The output is back to normal now, but I wonder: was it a mirroring issue or something else, and how do we test for the expected output? @lorentey Would be most appreciative of any tips.

@tbkka
Copy link
Contributor

tbkka commented Jun 22, 2022

Use as? or as! for runtime dynamic type checking.

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Jun 25, 2022

@tbkka Sorry, I should have expressed myself more clearly. I am willing to add a regression test before closing this and was asking where to look at (which 'print' or 'dump' function does the REPL call?).

@tbkka
Copy link
Contributor

tbkka commented Jun 27, 2022

Unfortunately, I'm not that familiar with how REPL testing works. @gottesmm , can you give any pointers?

@adrian-prantl
Copy link
Member

The output is back to normal now, but I wonder: was it a mirroring issue or something else, and how do we test for the expected output? @lorentey Would be most appreciative of any tips.

It looks like that would have been an LLDB data formatting bug.

@adrian-prantl
Copy link
Member

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

5 participants