Skip to content

Commit

Permalink
fixed List.map3 misleading error message on different length lists (#…
Browse files Browse the repository at this point in the history
…6980)

* fixed map3 misleading error message on different length lists

* fixed previous fix bug

* added map3 exception message test

* fixed map3 exception message test
  • Loading branch information
reacheight authored and brettfo committed Jun 11, 2019
1 parent a4cbfe1 commit 027e8c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/fsharp/FSharp.Core/local.fs
Original file line number Diff line number Diff line change
Expand Up @@ -294,24 +294,24 @@ module internal List =
| [], xs2 -> invalidArgDifferentListLength "list1" "list2" xs2.Length
| xs1, [] -> invalidArgDifferentListLength "list2" "list1" xs1.Length

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

let map3 mapping xs1 xs2 xs3 =
match xs1, xs2, xs3 with
| [], [], [] -> []
| h1 :: t1, h2 :: t2, h3 :: t3 ->
let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(mapping)
let cons = freshConsNoTail (f.Invoke(h1, h2, h3))
map3ToFreshConsTail cons f t1 t2 t3
map3ToFreshConsTail cons f t1 t2 t3 1
cons
| xs1, xs2, xs3 ->
invalidArg3ListsDifferent "list1" "list2" "list3" xs1.Length xs2.Length xs3.Length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,16 @@ type ListModule02() =
let longerList = [1; 2]
CheckThrowsArgumentException (fun () -> List.map3 funcInt shortList shortList longerList |> ignore)
CheckThrowsArgumentException (fun () -> List.map3 funcInt shortList longerList shortList |> ignore)
CheckThrowsArgumentException (fun () -> List.map3 funcInt shortList shortList longerList |> ignore)
CheckThrowsArgumentException (fun () -> List.map3 funcInt shortList shortList longerList |> ignore)

// exception message checking
let expectedMessage =
"The lists had different lengths.\n" +
sprintf " list1.Length = %i, list2.Length = %i, list3.Length = %i" shortList.Length shortList.Length longerList.Length +
Environment.NewLine + "Parameter name: list1, list2, list3"
let ex = Assert.Throws(typeof<ArgumentException>,
(fun () -> List.map3 funcInt shortList shortList longerList |> ignore))
Assert.AreEqual(expectedMessage, ex.Message)

// empty List
let resultEpt = List.map3 funcInt List.empty List.empty List.empty
Expand Down

0 comments on commit 027e8c9

Please sign in to comment.