From 5e4cecb1e0acdbd29f4e20a4a19589fc6d8caf17 Mon Sep 17 00:00:00 2001 From: "Somhairle H. Marisol" Date: Mon, 3 Mar 2025 16:37:19 +0800 Subject: [PATCH] Bugfix for Array.insertManyAt returns original array for empty insertion (#18352) - Update test, release note and comment for Array.InsertManyAt --- docs/release-notes/.FSharp.Core/9.0.300.md | 1 + src/FSharp.Core/array.fs | 2 +- src/FSharp.Core/array.fsi | 2 +- .../Microsoft.FSharp.Collections/ArrayModule2.fs | 8 +++++++- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/release-notes/.FSharp.Core/9.0.300.md b/docs/release-notes/.FSharp.Core/9.0.300.md index 5e6903f3fa4..3b2fdab5cfe 100644 --- a/docs/release-notes/.FSharp.Core/9.0.300.md +++ b/docs/release-notes/.FSharp.Core/9.0.300.md @@ -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)) diff --git a/src/FSharp.Core/array.fs b/src/FSharp.Core/array.fs index 63df73e5014..27c6d6426b6 100644 --- a/src/FSharp.Core/array.fs +++ b/src/FSharp.Core/array.fs @@ -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 diff --git a/src/FSharp.Core/array.fsi b/src/FSharp.Core/array.fsi index 6e76dbc32c6..151081c300c 100644 --- a/src/FSharp.Core/array.fsi +++ b/src/FSharp.Core/array.fsi @@ -3080,7 +3080,7 @@ module Array = /// The values to insert. /// The input array. /// - /// The result array. + /// A new array (even if values is empty). /// /// Thrown when index is below 0 or greater than source.Length. /// diff --git a/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs b/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs index 5c5afc3c234..b0532b8929f 100644 --- a/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs +++ b/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs @@ -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) \ No newline at end of file + 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) \ No newline at end of file