From b25b4263edc9e848dd69440643777b2027c68474 Mon Sep 17 00:00:00 2001 From: Petr Date: Tue, 25 Jun 2024 17:08:57 +0200 Subject: [PATCH] Fix exception throwing inconsistency (#17328) * Fix exception throwing inconsitency * release notes * Update 8.0.400.md --- docs/release-notes/.FSharp.Core/8.0.400.md | 4 ++++ src/FSharp.Core/local.fs | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/release-notes/.FSharp.Core/8.0.400.md b/docs/release-notes/.FSharp.Core/8.0.400.md index ee24d8d0a53..9c01378c5ef 100644 --- a/docs/release-notes/.FSharp.Core/8.0.400.md +++ b/docs/release-notes/.FSharp.Core/8.0.400.md @@ -6,3 +6,7 @@ * Cache delegate in query extensions. ([PR #17130](https://github.com/dotnet/fsharp/pull/17130)) * Update `AllowNullLiteralAttribute` to also use `AttributeTargets.Interface` ([PR #17173](https://github.com/dotnet/fsharp/pull/17173)) + +### Breaking Changes + +* Fixed argument exception throwing inconsistency - accessing an out-of-bounds collection index will now throw `ArgumentOutOfRangeException` instead of `ArgumentException` ([#17328](https://github.com/dotnet/fsharp/pull/17328)) diff --git a/src/FSharp.Core/local.fs b/src/FSharp.Core/local.fs index a57621c651f..063feb4b243 100644 --- a/src/FSharp.Core/local.fs +++ b/src/FSharp.Core/local.fs @@ -14,6 +14,11 @@ module internal DetailedExceptions = let msg = String.Format (format, paramArray) raise (new ArgumentException (msg, arg)) + /// takes an argument, a formatting string, a param array to splice into the formatting string + let inline invalidArgOutOfRangeFmt (arg:string) (format:string) paramArray = + let msg = String.Format (format, paramArray) + raise (new ArgumentOutOfRangeException (arg, msg)) + /// takes a formatting string and a param array to splice into the formatting string let inline invalidOpFmt (format:string) paramArray = let msg = String.Format (format, paramArray) @@ -38,7 +43,6 @@ module internal DetailedExceptions = "{0}\nThe list was {1} {2} shorter than the index" [|SR.GetString SR.notEnoughElements; index; (if index=1 then "element" else "elements")|] - /// eg. tried to {skip} {2} {elements} past the end of the seq. Seq.Length = {10} let invalidOpExceededSeqLength (fnName:string) (diff:int) (len:int) = invalidOpFmt "{0}\ntried to {1} {2} {3} past the end of the seq\nSeq.Length = {4}" @@ -52,11 +56,11 @@ module internal DetailedExceptions = let inline invalidArgInputMustBePositive (arg:string) (count:int) = invalidArgFmt arg "{0}\n{1} = {2}" [|SR.GetString SR.inputMustBePositive; arg; count|] - /// throws an invalid argument exception and returns the out of range index, + /// throws an invalid argument out of range exception and returns the out of range index, /// a text description of the range, and the bound of the range /// e.g. sourceIndex = -4, source axis-0 lower bound = 0" let invalidArgOutOfRange (arg:string) (index:int) (text:string) (bound:int) = - invalidArgFmt arg + invalidArgOutOfRangeFmt arg "{0}\n{1} = {2}, {3} = {4}" [|SR.GetString SR.outOfRange; arg; index; text; bound|]