Skip to content

[4.0][stdlib] Fix array slice self-assignment bug #11022

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,10 @@ public struct ${Self}<Element>
set(rhs) {
_checkIndex(bounds.lowerBound)
_checkIndex(bounds.upperBound)
if self[bounds]._buffer.identity != rhs._buffer.identity {
// If the replacement buffer has same identity, and the ranges match,
// then this was a pinned in-place modification, nothing further needed.
if self[bounds]._buffer.identity != rhs._buffer.identity
|| bounds != rhs.startIndex..<rhs.endIndex {
self.replaceSubrange(bounds, with: rhs)
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/stdlib/Inputs/CommonArrayTests.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,23 @@ ${Suite}.test(
// Special cases and one-off tests.
//===----------------------------------------------------------------------===//

${Suite}.test(
"${ArrayType}/subscriptSelfAssignDifferentRanges") {
var arr: ${ArrayType}<Int> = [ 1010, 1020, 1030 ]
arr[0..<0] = arr[...]
expectEqual([ 1010, 1020, 1030, 1010, 1020, 1030 ], arr)
arr[1..<1] = arr[1..<2]
expectEqual([ 1010, 1020, 1020, 1030, 1010, 1020, 1030 ], arr)
arr[1..<1] = arr[1..<2]
expectEqual([ 1010, 1020, 1020, 1020, 1030, 1010, 1020, 1030 ], arr)
arr[1..<2] = arr[0..<1]
expectEqual([ 1010, 1010, 1020, 1020, 1030, 1010, 1020, 1030 ], arr)
arr[0..<5] = arr.dropFirst(5)
expectEqual([ 1010, 1020, 1030, 1010, 1020, 1030 ], arr)
arr[...] = arr[0..<0]
expectEqual([ ], arr)
}

${Suite}.test("${ArrayType}<Void>/map") {
// This code used to crash because it generated an array of Void with
// stride == 0.
Expand Down