You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apple Swift version 4.0 (swiftlang-900.0.65 clang-900.0.37)
Apple Swift version 4.1 (swiftlang-902.0.34 clang-902.0.30)
Swift version 4.2-dev (LLVM a481df9074, Clang 051a73f5bc, Swift 599b4bc952)
We currently assume that the elements of an NSSet (and the keys of an NSDictionary) are convertible to AnyHashable. Unfortunately, this is not necessarily the case – NSSets may contain elements of Swift types that aren't hashable:
import Foundation
struct NotHashable {
let value: Int
}
let a = NotHashable(value: 42)
let set = NSSet(object: a as AnyObject)
let s1 = set as? Set<AnyHashable> // crashes
let s2 = set as AnyHashable // crashes
What if we made all _SwiftValues convertible to AnyHashable? (They already are in a way, although only when Foundation is imported, and the resulting AnyHashable is broken.) If the original Swift value isn't Hashable, its AnyHashable representation could still use _SwiftValue identity for hashing – this wouldn't be very useful, but at least it wouldn't crash.
$ swift
Welcome to Apple Swift version 4.1 (swiftlang-902.0.34 clang-902.0.30). Type :help for assistance.
1> struct NotHashable { let value: Int }
2> let a = NotHashable(value: 42)
a: NotHashable = {
value = 42
}
3> let o = a as AnyObject
o: Swift.AnyObject = {
instance_type = 0x0000000101700130
}
4> o as? AnyHashable
$R0: AnyHashable? = nil
5> import Foundation
6> o as? AnyHashable
$R1: AnyHashable? = some {
_box = {
_baseHashable = {
value = 42
}
}
_usedCustomRepresentation = false
}
7> (o as? AnyHashable)?.hashValue
Execution interrupted. Enter code to recover and continue.
Enter LLDB commands to investigate (type :help for assistance.)
Process 31806 stopped
* thread #​1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x2a)
frame #​0: 0x00007fff78635e57 libobjc.A.dylib`objc_msgSend + 23
libobjc.A.dylib`objc_msgSend:
-> 0x7fff78635e57 <+23>: andq (%rdi), %r10
0x7fff78635e5a <+26>: movq %rsi, %r11
0x7fff78635e5d <+29>: andl 0x18(%r10), %r11d
0x7fff78635e61 <+33>: shlq $0x4, %r11
Target 0: (repl_swift) stopped.
8>
Environment
Apple Swift version 4.0 (swiftlang-900.0.65 clang-900.0.37)
Apple Swift version 4.1 (swiftlang-902.0.34 clang-902.0.30)
Swift version 4.2-dev (LLVM a481df9074, Clang 051a73f5bc, Swift 599b4bc952)
Additional Detail from JIRA
md5: 3c6a82511a3c7c4552d6181c15f93ce9
Issue Description:
We currently assume that the elements of an NSSet (and the keys of an NSDictionary) are convertible to AnyHashable. Unfortunately, this is not necessarily the case – NSSets may contain elements of Swift types that aren't hashable:
4.2 stack trace for the Set<AnyHashable> crash:
Trace for AnyHashable conversion:
The text was updated successfully, but these errors were encountered: