Skip to content

Commit

Permalink
Bugfix for Array.insertManyAt returns original array for empty insert…
Browse files Browse the repository at this point in the history
…ion (dotnet#18352)

- Update test, release note and comment for Array.InsertManyAt
  • Loading branch information
muqiuhan committed Mar 5, 2025
1 parent 32c2764 commit 5e4cecb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Core/9.0.300.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### Fixed
* 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))

### Added
* Added nullability annotations to `.Using` builder method for `async` and `task` builders ([PR #18292](https://github.com/dotnet/fsharp/pull/18292))
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Core/array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,7 @@ module Array =
let valuesArray = Seq.toArray values

if valuesArray.Length = 0 then
source
source.Clone() :?> 'T array
else
let length = source.Length + valuesArray.Length
let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked length
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Core/array.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -3080,7 +3080,7 @@ module Array =
/// <param name="values">The values to insert.</param>
/// <param name="source">The input array.</param>
///
/// <returns>The result array.</returns>
/// <returns>A new array (even if values is empty).</returns>
///
/// <exception cref="T:System.ArgumentException">Thrown when index is below 0 or greater than source.Length.</exception>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1679,4 +1679,10 @@ type ArrayModule2() =
// empty list & out of bounds
Assert.AreEqual([0; 0], Array.insertManyAt 0 [0; 0] [||])
CheckThrowsArgumentException (fun () -> Array.insertManyAt -1 [0; 0] [|1|] |> ignore)
CheckThrowsArgumentException (fun () -> Array.insertManyAt 2 [0; 0] [|1|] |> ignore)
CheckThrowsArgumentException (fun () -> Array.insertManyAt 2 [0; 0] [|1|] |> ignore)

// Do not return the original array when inserting an empty array
let originalArr = [| 1; 2; 3 |]
let insertionEmptyResultArr = Array.insertManyAt 3 [| |] originalArr
insertionEmptyResultArr[0] <- 3
Assert.AreEqual([| 1; 2; 3 |], originalArr)

0 comments on commit 5e4cecb

Please sign in to comment.