PR for context: [stdlib] Dictionary.Keys, .Values: Implement Custom[Debug]StringConvertible by lorentey · Pull Request #12249 · apple/swift · GitHub.
Note: The setup in which I experienced the crash was on Xcode 9.2 with Swift 4.0. The issue has been resolved for me on Xcode 9.3 running Swift 4.1 so this post is more exploratory.
Context
I experienced the same issue that #12249 fixes where there was a
...crash involving _HashableTypedNativeDictionaryStorage when printing dictionaries whose keys or values aren't bridged verbatim.
I am curious to learn more about why this happened especially since I was able to reproduce it quite easily.
The playground (running in Xcode 9.2 with Swift 4.0) I used look like this:
import Foundation
let testKey: String = "Somebody once told me"
let dictionary: [String: NSNumber] = [testKey: 12]
print(dictionary)
// ["Somebody once told me": 12]
print(dictionary.keys)
// error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT).
// The process has been left at the point where it was interrupted, use "thread return -x"
// to return to the state before expression evaluation.
However, when I change the testKey
to be of type NSString
then it no longer crashes:
import Foundation
let testKey: NSString = "Somebody once told me"
let dictionary: [String: NSNumber] = [testKey as String: 12]
print(dictionary)
// ["Somebody once told me": 12]
print(dictionary.keys)
// ["Somebody once told me"]
After looking through the variable inspector, the only difference I noticed was:
- using just Swift strings, I observed they had no "owner"
- using a Swift string that was casted from an NSString, there was an "owner" (the backing NSString I assume)
Swift string
- I'm unable to post the image because of the 1 image upload limit but this looks similar to the below screenshot except
owner
is marked asnil
. - I posted the screenshot in the PR as a comment.
Swift string casted from NSString
Question
How could I find out more about what "owner"
and "bridged verbatim"
means? How did the latter affect the crash? Thanks for taking the time to read this, I'm just curious to understand this better
I found one post that mentions the latter which does mention that "String/NSString aren’t. [verbatim bridged]"
. This makes sense as per the description of the PR.
Let me know if this post is in the wrong section but the PR was marked as [stdlib] so this seemed appropriate