Skip to content

Instantly share code, notes, and snippets.

@keith
Last active October 14, 2016 20:07
Show Gist options
  • Save keith/dd3204857997c18cc07806c07d1e2566 to your computer and use it in GitHub Desktop.
Save keith/dd3204857997c18cc07806c07d1e2566 to your computer and use it in GitHub Desktop.
Swift 3.0 optimizations crash (fixed in Xcode 8.1 beta 3)
///
/// This code crashes with Swift 3.0 with optimizations enabled
/// Run with:
/// $ swift -O optimizationCrash.swift
///
extension Dictionary {
// init(pairs: [(Key, Value)]?) { // With `(Key, Value)` this doesn't crash
init(pairs: [Element]?) { // With `Element` this crashes with -0
self.init()
}
}
let elements: [(Int, Int)] = [(1, 1)]
dump(Dictionary(pairs: elements))
extension Dictionary {
init(_ pairs: [(Key, Value)]?) {
self.init()
for (k, v) in pairs ?? [] {
self[k] = v
}
}
}
let dictionary: [String: Int] = ["hi": 1]
let array: [(String, Int)] = dictionary.filter { _ in return true } // Crashes using filter
// var array = [(String, Int)]() // Doesn't crash if you use a for loop
// for (key, value) in dictionary {
// array.append((key, value))
// }
dump(Dictionary(array))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment