Skip to content

Commit 7cb2cea

Browse files
Bugfix for Array.insertManyAt returns original array for empty insertion (#18352) (#18353)
- Update test, release note and comment for Array.InsertManyAt Co-authored-by: Kevin Ransom (msft) <[email protected]>
1 parent b99cd60 commit 7cb2cea

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

docs/release-notes/.FSharp.Core/9.0.300.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
### Fixed
2+
* Modified the behavior of `Array.insertManyAt` to return a copy of the original array when inserting an empty array. ([PR #18353](https://github.com/dotnet/fsharp/pull/18353))
23

34
### Added
45
* Added nullability annotations to `.Using` builder method for `async` and `task` builders ([PR #18292](https://github.com/dotnet/fsharp/pull/18292))

src/FSharp.Core/array.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,7 @@ module Array =
19341934
let valuesArray = Seq.toArray values
19351935

19361936
if valuesArray.Length = 0 then
1937-
source
1937+
source.Clone() :?> 'T array
19381938
else
19391939
let length = source.Length + valuesArray.Length
19401940
let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked length

src/FSharp.Core/array.fsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3080,7 +3080,7 @@ module Array =
30803080
/// <param name="values">The values to insert.</param>
30813081
/// <param name="source">The input array.</param>
30823082
///
3083-
/// <returns>The result array.</returns>
3083+
/// <returns>A new array (even if values is empty).</returns>
30843084
///
30853085
/// <exception cref="T:System.ArgumentException">Thrown when index is below 0 or greater than source.Length.</exception>
30863086
///

tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1679,4 +1679,10 @@ type ArrayModule2() =
16791679
// empty list & out of bounds
16801680
Assert.AreEqual([0; 0], Array.insertManyAt 0 [0; 0] [||])
16811681
CheckThrowsArgumentException (fun () -> Array.insertManyAt -1 [0; 0] [|1|] |> ignore)
1682-
CheckThrowsArgumentException (fun () -> Array.insertManyAt 2 [0; 0] [|1|] |> ignore)
1682+
CheckThrowsArgumentException (fun () -> Array.insertManyAt 2 [0; 0] [|1|] |> ignore)
1683+
1684+
// Do not return the original array when inserting an empty array
1685+
let originalArr = [| 1; 2; 3 |]
1686+
let insertionEmptyResultArr = Array.insertManyAt 3 [| |] originalArr
1687+
insertionEmptyResultArr[0] <- 3
1688+
Assert.AreEqual([| 1; 2; 3 |], originalArr)

0 commit comments

Comments
 (0)