Fix replaceSubrange(_:addingCount:initializingWith:) returning a range sized by the requested count

The closure-based replaceSubrange overloads on RigidArray (and, through
them, UniqueArray and the generic RangeReplaceableContainer overloads)
returned subrange.lowerBound ..< subrange.lowerBound + newItemCount even
when the initializer performed a short fill, so the returned range could
cover old elements that were never replaced and extend past endIndex.
Return the number of items actually initialized (span.count), matching
insert(addingCount:at:initializingWith:).

Fixes #772
This commit is contained in:
Aashish committed 2026-10-20 19:45:05 +05:45
1 parent b905c889ff
commit 96648ca1d8
3 files changed
+26 -6

No files matched your search

@@ -96,8 +96,7 @@ extension RigidArray where Element: ~Copyable {
span = OutputSpan()
}
try initializer(&span)
return unsafe Range(uncheckedBounds: (
subrange.lowerBound, subrange.lowerBound + newItemCount))
return subrange.lowerBound ..< (subrange.lowerBound + span.count)
}
}
@@ -201,9 +200,8 @@ extension RigidArray where Element: ~Copyable {
span = OutputSpan()
}
try initializer(&span)
return subrange.lowerBound ..< (subrange.lowerBound + span.count)
}
return unsafe Range(uncheckedBounds: (
subrange.lowerBound, subrange.lowerBound + newItemCount))
}
#endif
}