Skip to content

Commit 027e8c9

Browse files
reacheightbrettfo
authored andcommitted
fixed List.map3 misleading error message on different length lists (#6980)
* fixed map3 misleading error message on different length lists * fixed previous fix bug * added map3 exception message test * fixed map3 exception message test
1 parent a4cbfe1 commit 027e8c9

File tree

2 files changed

+14
-5
lines changed
  • src/fsharp/FSharp.Core
  • tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Collections

2 files changed

+14
-5
lines changed

src/fsharp/FSharp.Core/local.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,24 +294,24 @@ module internal List =
294294
| [], xs2 -> invalidArgDifferentListLength "list1" "list2" xs2.Length
295295
| xs1, [] -> invalidArgDifferentListLength "list2" "list1" xs1.Length
296296

297-
let rec map3ToFreshConsTail cons (f:OptimizedClosures.FSharpFunc<_, _, _, _>) xs1 xs2 xs3 =
297+
let rec map3ToFreshConsTail cons (f:OptimizedClosures.FSharpFunc<_, _, _, _>) xs1 xs2 xs3 cut =
298298
match xs1, xs2, xs3 with
299299
| [], [], [] ->
300300
setFreshConsTail cons []
301301
| h1 :: t1, h2 :: t2, h3 :: t3 ->
302302
let cons2 = freshConsNoTail (f.Invoke(h1, h2, h3))
303303
setFreshConsTail cons cons2
304-
map3ToFreshConsTail cons2 f t1 t2 t3
304+
map3ToFreshConsTail cons2 f t1 t2 t3 (cut + 1)
305305
| xs1, xs2, xs3 ->
306-
invalidArg3ListsDifferent "list1" "list2" "list3" xs1.Length xs2.Length xs3.Length
306+
invalidArg3ListsDifferent "list1" "list2" "list3" (xs1.Length + cut) (xs2.Length + cut) (xs3.Length + cut)
307307

308308
let map3 mapping xs1 xs2 xs3 =
309309
match xs1, xs2, xs3 with
310310
| [], [], [] -> []
311311
| h1 :: t1, h2 :: t2, h3 :: t3 ->
312312
let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(mapping)
313313
let cons = freshConsNoTail (f.Invoke(h1, h2, h3))
314-
map3ToFreshConsTail cons f t1 t2 t3
314+
map3ToFreshConsTail cons f t1 t2 t3 1
315315
cons
316316
| xs1, xs2, xs3 ->
317317
invalidArg3ListsDifferent "list1" "list2" "list3" xs1.Length xs2.Length xs3.Length

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,16 @@ type ListModule02() =
102102
let longerList = [1; 2]
103103
CheckThrowsArgumentException (fun () -> List.map3 funcInt shortList shortList longerList |> ignore)
104104
CheckThrowsArgumentException (fun () -> List.map3 funcInt shortList longerList shortList |> ignore)
105-
CheckThrowsArgumentException (fun () -> List.map3 funcInt shortList shortList longerList |> ignore)
105+
CheckThrowsArgumentException (fun () -> List.map3 funcInt shortList shortList longerList |> ignore)
106+
107+
// exception message checking
108+
let expectedMessage =
109+
"The lists had different lengths.\n" +
110+
sprintf " list1.Length = %i, list2.Length = %i, list3.Length = %i" shortList.Length shortList.Length longerList.Length +
111+
Environment.NewLine + "Parameter name: list1, list2, list3"
112+
let ex = Assert.Throws(typeof<ArgumentException>,
113+
(fun () -> List.map3 funcInt shortList shortList longerList |> ignore))
114+
Assert.AreEqual(expectedMessage, ex.Message)
106115

107116
// empty List
108117
let resultEpt = List.map3 funcInt List.empty List.empty List.empty

0 commit comments

Comments
 (0)