Skip to content

Commit b714a31

Browse files
authored
Merge pull request #34639 from eeckstein/fix-cow-checks
stdlib: remove a wrong internal check for COW array mutation
2 parents 963b4e6 + 6d0bbba commit b714a31

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

stdlib/public/core/ArrayBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ extension _ArrayBuffer {
138138

139139
/// Puts the buffer in an immutable state.
140140
///
141-
/// - Precondition: The buffer must be mutable.
141+
/// - Precondition: The buffer must be mutable or the empty array singleton.
142142
///
143143
/// - Warning: After a call to `endCOWMutation` the buffer must not be mutated
144144
/// until the next call of `beginCOWMutation`.

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -470,18 +470,17 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
470470
nonmutating set {
471471
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
472472
if (_COWChecksEnabled()) {
473-
if newValue {
474-
if capacity > 0 {
475-
let wasImmutable = _swift_setImmutableCOWBuffer(_storage, true)
473+
// Make sure to not modify the empty array singleton (which has a
474+
// capacity of 0).
475+
if capacity > 0 {
476+
let wasImmutable = _swift_setImmutableCOWBuffer(_storage, newValue)
477+
if newValue {
476478
_internalInvariant(!wasImmutable,
477479
"re-setting immutable array buffer to immutable")
480+
} else {
481+
_internalInvariant(wasImmutable,
482+
"re-setting mutable array buffer to mutable")
478483
}
479-
} else {
480-
_internalInvariant(capacity > 0,
481-
"setting empty array buffer to mutable")
482-
let wasImmutable = _swift_setImmutableCOWBuffer(_storage, false)
483-
_internalInvariant(wasImmutable,
484-
"re-setting mutable array buffer to mutable")
485484
}
486485
}
487486
}
@@ -698,7 +697,7 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
698697

699698
/// Puts the buffer in an immutable state.
700699
///
701-
/// - Precondition: The buffer must be mutable.
700+
/// - Precondition: The buffer must be mutable or the empty array singleton.
702701
///
703702
/// - Warning: After a call to `endCOWMutation` the buffer must not be mutated
704703
/// until the next call of `beginCOWMutation`.

stdlib/public/core/SliceBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ internal struct _SliceBuffer<Element>
329329

330330
/// Puts the buffer in an immutable state.
331331
///
332-
/// - Precondition: The buffer must be mutable.
332+
/// - Precondition: The buffer must be mutable or the empty array singleton.
333333
///
334334
/// - Warning: After a call to `endCOWMutation` the buffer must not be mutated
335335
/// until the next call of `beginCOWMutation`.

0 commit comments

Comments
 (0)