Files
Aditya Singh 0a3dcfd3a8 [InternalCollectionsUtilities] Fix _trim returning the wrong buffer region
The `_trim(first:)` and `_trim(last:)` helpers reassign `self` to the
remaining slice and then build the trimmed result from `self`'s now-updated
`baseAddress`/`count`, so they hand back a region that points into the wrong
place in the buffer.

`UnsafeMutableBufferPointer._trim(first:)` had this exact bug fixed earlier (it
captures `oldStart` before the reassignment), but three sibling helpers were
left untouched:

  * `UnsafeBufferPointer._trim(first:)` reads `baseAddress` after advancing
    `self`, so the returned prefix starts at the remainder instead of at the
    original head.
  * `UnsafeBufferPointer._trim(last:)` and
    `UnsafeMutableBufferPointer._trim(last:)` recompute `count &- cut` after
    shrinking `self`, so the returned suffix starts `cut` elements too early.

For a five-element buffer, `_trim(first: 2)` returned the bytes of `[30, 40]`
rather than `[10, 20]`, and `_trim(last: 2)` returned `[20, 30]` rather than
`[40, 50]`; trimming more than the count walked off the end of the buffer.

Capture the original start (and, for the suffix case, the post-trim count)
before mutating `self`, matching the already-correct `_trim(first:)` on the
mutable buffer. Adds direct tests covering both overloads on both buffer types.
2026-05-24 18:22:45 -07:00
..