diff --git a/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs b/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs index 6ab40a77af..06b69feb34 100644 --- a/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs +++ b/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs @@ -918,6 +918,20 @@ type SeqModule2() = for i = 11 to 20 do CheckThrowsArgumentException (fun () -> Seq.item i { 10 .. 20 } |> ignore) + [] + member this.``item should fail with correct number of missing elements``() = + try + Seq.item 0 (Array.zeroCreate 0) |> ignore + failwith "error expected" + with + | exn when exn.Message.Contains("seq was short by 1 element") -> () + + try + Seq.item 2 (Array.zeroCreate 0) |> ignore + failwith "error expected" + with + | exn when exn.Message.Contains("seq was short by 3 elements") -> () + [] member this.Of_Array() = // integer Seq diff --git a/src/fsharp/FSharp.Core/seq.fs b/src/fsharp/FSharp.Core/seq.fs index 31c70f57ee..d2cfe54622 100644 --- a/src/fsharp/FSharp.Core/seq.fs +++ b/src/fsharp/FSharp.Core/seq.fs @@ -64,9 +64,10 @@ namespace Microsoft.FSharp.Collections let rec nth index (e : IEnumerator<'T>) = if not (e.MoveNext()) then + let shortBy = index + 1 invalidArgFmt "index" "{0}\nseq was short by {1} {2}" - [|SR.GetString SR.notEnoughElements; index; (if index=1 then "element" else "elements")|] + [|SR.GetString SR.notEnoughElements; shortBy; (if shortBy = 1 then "element" else "elements")|] if index = 0 then e.Current else nth (index-1) e