[SR-14164] Unexpected crash while using Array(unsafeUninitializedCapacity:) #56543
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
standard library
Area: Standard library umbrella
Environment
Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
Target: x86_64-apple-darwin20.3.0
Additional Detail from JIRA
md5: bd27733c794bd22f1e3fa53e590d95bd
Issue Description:
While trying to make a small improvement to SwiftLint by using https://github.com/apple/swift-evolution/blob/master/proposals/0223-array-uninitialized-initializer.md I came across an unexpected and interesting edge case.
This is the code that the evolution proposal uses for it's simple example:
On it's own this code is fine. What ends up not being fine is that when the Array.Element type is a class (or an unspecialized generic) `buffer[x] = x` crashes. It seems like it's doing an assignWithTake to release the old value, which makes sense for a class, but not for uninitialized data. I'm not entirely sure how the compiler would fix this, because there is nothing preventing you from doing something like:
and for the second write at an index the compiler would need to release for original instance.
This is an unsafe initializer after all, so if it can't be fixed it should at least be documented.
The documentation for this function is a bit light, doesn't contain any examples, so people are likely to go to the evolution proposal and try the first example. It would be better if the standard library documented the danger with using the subscript and instead directed people towards something like:
SwiftLint Original PR here: realm/SwiftLint#3515
Reproduction code:
The text was updated successfully, but these errors were encountered: