Skip to content

Commit 062d672

Browse files
authored
Merge pull request #1454 from cloudRoutine/improve-errors
[WIP] Adding extra detail to error messages
2 parents ec32fa2 + 467fb8c commit 062d672

File tree

9 files changed

+836
-710
lines changed

9 files changed

+836
-710
lines changed

src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/CollectionModulesConsistency.fs

Lines changed: 160 additions & 122 deletions
Large diffs are not rendered by default.

src/fsharp/FSharp.Core/array.fs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ namespace Microsoft.FSharp.Collections
4646
else Some array.[array.Length-1]
4747

4848
[<CompiledName("Initialize")>]
49-
let inline init count f = Array.init count f
49+
let inline init count f = Array.init count f
5050

5151
[<CompiledName("ZeroCreate")>]
5252
let zeroCreate count =
53-
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
53+
if count < 0 then invalidArgInputMustBeNonNegative "count" count
5454
Array.zeroCreateUnchecked count
5555

5656
[<CompiledName("Create")>]
5757
let create (count:int) (x:'T) =
58-
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
58+
if count < 0 then invalidArgInputMustBeNonNegative "count" count
5959
let array: 'T[] = Array.zeroCreateUnchecked count
6060
for i = 0 to Operators.Checked.(-) array.Length 1 do // use checked arithmetic here to satisfy FxCop
6161
array.[i] <- x
@@ -110,7 +110,7 @@ namespace Microsoft.FSharp.Collections
110110

111111
[<CompiledName("Replicate")>]
112112
let replicate count x =
113-
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
113+
if count < 0 then invalidArgInputMustBeNonNegative "count" count
114114
let arr : 'T array = Array.zeroCreateUnchecked count
115115
for i = 0 to arr.Length-1 do
116116
arr.[i] <- x
@@ -128,7 +128,7 @@ namespace Microsoft.FSharp.Collections
128128
[<CompiledName("SplitAt")>]
129129
let splitAt index (array:'T[]) =
130130
checkNonNull "array" array
131-
if index < 0 then invalidArg "index" (SR.GetString(SR.inputMustBeNonNegative))
131+
if index < 0 then invalidArgInputMustBeNonNegative "index" index
132132
if array.Length < index then raise <| InvalidOperationException (SR.GetString(SR.notEnoughElements))
133133
if index = 0 then
134134
let right = Array.subUnchecked 0 array.Length array
@@ -145,7 +145,7 @@ namespace Microsoft.FSharp.Collections
145145
[<CompiledName("Take")>]
146146
let take count (array : 'T[]) =
147147
checkNonNull "array" array
148-
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
148+
if count < 0 then invalidArgInputMustBeNonNegative "count" count
149149
if count = 0 then
150150
empty
151151
else
@@ -276,7 +276,7 @@ namespace Microsoft.FSharp.Collections
276276
checkNonNull "array1" array1
277277
checkNonNull "array2" array2
278278
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
279-
if array1.Length <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths));
279+
if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
280280
for i = 0 to array1.Length-1 do
281281
f.Invoke(array1.[i], array2.[i])
282282

@@ -298,7 +298,7 @@ namespace Microsoft.FSharp.Collections
298298
checkNonNull "array1" array1
299299
checkNonNull "array2" array2
300300
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
301-
if array1.Length <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths));
301+
if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
302302
let res = Array.zeroCreateUnchecked array1.Length
303303
for i = 0 to res.Length-1 do
304304
res.[i] <- f.Invoke(array1.[i], array2.[i])
@@ -311,7 +311,7 @@ namespace Microsoft.FSharp.Collections
311311
checkNonNull "array3" array3
312312
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
313313
let len1 = array1.Length
314-
if not (len1 = array2.Length && len1 = array3.Length) then invalidArg "" (SR.GetString(SR.arraysHadDifferentLengths))
314+
if len1 <> array2.Length || len1 <> array3.Length then invalidArg3ArraysDifferent "array1" "array2" "array3" len1 array2.Length array3.Length
315315

316316
let res = Array.zeroCreateUnchecked len1
317317
for i = 0 to res.Length-1 do
@@ -323,7 +323,7 @@ namespace Microsoft.FSharp.Collections
323323
checkNonNull "array1" array1
324324
checkNonNull "array2" array2
325325
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
326-
if array1.Length <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths));
326+
if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
327327
let res = Array.zeroCreateUnchecked array1.Length
328328
for i = 0 to res.Length-1 do
329329
res.[i] <- f.Invoke(i,array1.[i], array2.[i])
@@ -341,7 +341,7 @@ namespace Microsoft.FSharp.Collections
341341
checkNonNull "array1" array1
342342
checkNonNull "array2" array2
343343
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
344-
if array1.Length <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths));
344+
if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
345345
for i = 0 to array1.Length-1 do
346346
f.Invoke(i,array1.[i], array2.[i])
347347

@@ -387,7 +387,7 @@ namespace Microsoft.FSharp.Collections
387387
checkNonNull "array2" array2
388388
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
389389
let len1 = array1.Length
390-
if len1 <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths))
390+
if len1 <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
391391
let rec loop i = i < len1 && (f.Invoke(array1.[i], array2.[i]) || loop (i+1))
392392
loop 0
393393

@@ -404,7 +404,7 @@ namespace Microsoft.FSharp.Collections
404404
checkNonNull "array2" array2
405405
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
406406
let len1 = array1.Length
407-
if len1 <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths))
407+
if len1 <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
408408
let rec loop i = i >= len1 || (f.Invoke(array1.[i], array2.[i]) && loop (i+1))
409409
loop 0
410410

@@ -614,7 +614,7 @@ namespace Microsoft.FSharp.Collections
614614
[<CompiledName("Skip")>]
615615
let skip count (array:'T[]) =
616616
checkNonNull "array" array
617-
if count > array.Length then invalidArg "count" (SR.GetString(SR.outOfRange))
617+
if count > array.Length then invalidArgOutOfRange "count" count "array.Length" array.Length
618618
if count = array.Length then
619619
empty
620620
else
@@ -654,7 +654,7 @@ namespace Microsoft.FSharp.Collections
654654
[<CompiledName("Windowed")>]
655655
let windowed windowSize (array:'T[]) =
656656
checkNonNull "array" array
657-
if windowSize <= 0 then invalidArg "windowSize" (SR.GetString(SR.inputMustBePositive))
657+
if windowSize <= 0 then invalidArgInputMustBePositive "windowSize" windowSize
658658
let len = array.Length
659659
if windowSize > len then
660660
empty
@@ -667,7 +667,7 @@ namespace Microsoft.FSharp.Collections
667667
[<CompiledName("ChunkBySize")>]
668668
let chunkBySize chunkSize (array:'T[]) =
669669
checkNonNull "array" array
670-
if chunkSize <= 0 then invalidArg "chunkSize" (SR.GetString(SR.inputMustBePositive))
670+
if chunkSize <= 0 then invalidArgInputMustBePositive "chunkSize" chunkSize
671671
let len = array.Length
672672
if len = 0 then
673673
empty
@@ -685,15 +685,15 @@ namespace Microsoft.FSharp.Collections
685685
[<CompiledName("SplitInto")>]
686686
let splitInto count (array:_[]) =
687687
checkNonNull "array" array
688-
if count <= 0 then invalidArg "count" (SR.GetString(SR.inputMustBePositive))
688+
if count <= 0 then invalidArgInputMustBePositive "count" count
689689
Array.splitInto count array
690690

691691
[<CompiledName("Zip")>]
692692
let zip (array1: _[]) (array2: _[]) =
693693
checkNonNull "array1" array1
694694
checkNonNull "array2" array2
695695
let len1 = array1.Length
696-
if len1 <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths))
696+
if len1 <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
697697
let res = Array.zeroCreateUnchecked len1
698698
for i = 0 to res.Length-1 do
699699
res.[i] <- (array1.[i],array2.[i])
@@ -705,8 +705,7 @@ namespace Microsoft.FSharp.Collections
705705
checkNonNull "array2" array2
706706
checkNonNull "array3" array3
707707
let len1 = array1.Length
708-
if len1 <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths))
709-
if len1 <> array3.Length then invalidArg "array3" (SR.GetString(SR.arraysHadDifferentLengths))
708+
if len1 <> array2.Length || len1 <> array3.Length then invalidArg3ArraysDifferent "array1" "array2" "array3" len1 array2.Length array3.Length
710709
let res = Array.zeroCreateUnchecked len1
711710
for i = 0 to res.Length-1 do
712711
res.[i] <- (array1.[i],array2.[i],array3.[i])
@@ -798,7 +797,7 @@ namespace Microsoft.FSharp.Collections
798797
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
799798
let mutable res = acc
800799
let len = array1.Length
801-
if len <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths))
800+
if len <> array2.Length then invalidArgDifferentArrayLength "array1" len "array2" array2.Length
802801
for i = len-1 downto 0 do
803802
res <- f.Invoke(array1.[i],array2.[i],res)
804803
res
@@ -809,7 +808,7 @@ namespace Microsoft.FSharp.Collections
809808
checkNonNull "array2" array2
810809
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
811810
let mutable state = acc
812-
if array1.Length <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths))
811+
if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
813812
for i = 0 to array1.Length-1 do
814813
state <- f.Invoke(state,array1.[i],array2.[i])
815814
state
@@ -1076,9 +1075,9 @@ namespace Microsoft.FSharp.Collections
10761075
[<CompiledName("GetSubArray")>]
10771076
let sub (array:'T[]) (startIndex:int) (count:int) =
10781077
checkNonNull "array" array
1079-
if startIndex < 0 then invalidArg "startIndex" (SR.GetString(SR.inputMustBeNonNegative))
1080-
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
1081-
if startIndex + count > array.Length then invalidArg "count" (SR.GetString(SR.outOfRange))
1078+
if startIndex < 0 then invalidArgInputMustBeNonNegative "startIndex" startIndex
1079+
if count < 0 then invalidArgInputMustBeNonNegative "count" count
1080+
if startIndex + count > array.Length then invalidArgOutOfRange "count" count "array.Length" array.Length
10821081
Array.subUnchecked startIndex count array
10831082

10841083
[<CompiledName("Item")>]
@@ -1102,8 +1101,8 @@ namespace Microsoft.FSharp.Collections
11021101
[<CompiledName("Fill")>]
11031102
let fill (target:'T[]) (targetIndex:int) (count:int) (x:'T) =
11041103
checkNonNull "target" target
1105-
if targetIndex < 0 then invalidArg "targetIndex" (SR.GetString(SR.inputMustBeNonNegative))
1106-
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
1104+
if targetIndex < 0 then invalidArgInputMustBeNonNegative "targetIndex" targetIndex
1105+
if count < 0 then invalidArgInputMustBeNonNegative "count" count
11071106
for i = targetIndex to targetIndex + count - 1 do
11081107
target.[i] <- x
11091108

src/fsharp/FSharp.Core/array2.fs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ namespace Microsoft.FSharp.Collections
3737

3838
[<CompiledName("ZeroCreate")>]
3939
let zeroCreate (n:int) (m:int) =
40-
if n < 0 then invalidArg "n" (SR.GetString(SR.inputMustBeNonNegative))
41-
if m < 0 then invalidArg "m" (SR.GetString(SR.inputMustBeNonNegative))
40+
if n < 0 then invalidArgInputMustBeNonNegative "length1" n
41+
if m < 0 then invalidArgInputMustBeNonNegative "length2" m
4242
(# "newarr.multi 2 !0" type ('T) n m : 'T[,] #)
4343

4444
[<CompiledName("ZeroCreateBased")>]
@@ -130,17 +130,27 @@ namespace Microsoft.FSharp.Collections
130130
init (length1 array) (length2 array) (fun i j -> array.[b1+i,b2+j])
131131

132132
[<CompiledName("CopyTo")>]
133-
let blit (source : 'T[,]) sourceIndex1 sourceIndex2 (target : 'T[,]) targetIndex1 targetIndex2 count1 count2 =
133+
let blit (source : 'T[,]) sourceIndex1 sourceIndex2 (target: 'T[,]) targetIndex1 targetIndex2 count1 count2 =
134134
checkNonNull "source" source
135135
checkNonNull "target" target
136-
if sourceIndex1 < source.GetLowerBound(0) then invalidArg "sourceIndex1" (SR.GetString(SR.outOfRange))
137-
if sourceIndex2 < source.GetLowerBound(1) then invalidArg "sourceIndex2" (SR.GetString(SR.outOfRange))
138-
if targetIndex1 < target.GetLowerBound(0) then invalidArg "targetIndex1" (SR.GetString(SR.outOfRange))
139-
if targetIndex2 < target.GetLowerBound(1) then invalidArg "targetIndex2" (SR.GetString(SR.outOfRange))
140-
if sourceIndex1 + count1 > (length1 source) + source.GetLowerBound(0) then invalidArg "count1" (SR.GetString(SR.outOfRange))
141-
if sourceIndex2 + count2 > (length2 source) + source.GetLowerBound(1) then invalidArg "count2" (SR.GetString(SR.outOfRange))
142-
if targetIndex1 + count1 > (length1 target) + target.GetLowerBound(0) then invalidArg "count1" (SR.GetString(SR.outOfRange))
143-
if targetIndex2 + count2 > (length2 target) + target.GetLowerBound(1) then invalidArg "count2" (SR.GetString(SR.outOfRange))
136+
137+
let sourceX0, sourceY0 = source.GetLowerBound 0 , source.GetLowerBound 1
138+
let sourceXN, sourceYN = (length1 source) + sourceX0, (length2 source) + sourceY0
139+
let targetX0, targetY0 = target.GetLowerBound 0 , target.GetLowerBound 1
140+
let targetXN, targetYN = (length1 target) + targetX0, (length2 target) + targetY0
141+
142+
if sourceIndex1 < sourceX0 then invalidArgOutOfRange "sourceIndex1" sourceIndex1 "source axis-0 lower bound" sourceX0
143+
if sourceIndex2 < sourceY0 then invalidArgOutOfRange "sourceIndex2" sourceIndex2 "source axis-1 lower bound" sourceY0
144+
if targetIndex1 < targetX0 then invalidArgOutOfRange "targetIndex1" targetIndex1 "target axis-0 lower bound" targetX0
145+
if targetIndex2 < targetY0 then invalidArgOutOfRange "targetIndex2" targetIndex2 "target axis-1 lower bound" targetY0
146+
if sourceIndex1 + count1 > sourceXN then
147+
invalidArgOutOfRange "count1" count1 ("source axis-0 end index = " + string(sourceIndex1+count1) + " source axis-0 upper bound") sourceXN
148+
if sourceIndex2 + count2 > sourceYN then
149+
invalidArgOutOfRange "count2" count2 ("source axis-1 end index = " + string(sourceIndex2+count2) + " source axis-1 upper bound") sourceYN
150+
if targetIndex1 + count1 > targetXN then
151+
invalidArgOutOfRange "count1" count1 ("target axis-0 end index = " + string(targetIndex1+count1) + " target axis-0 upper bound") targetXN
152+
if targetIndex2 + count2 > targetYN then
153+
invalidArgOutOfRange "count2" count2 ("target axis-1 end index = " + string(targetIndex2+count2) + " target axis-1 upper bound") targetYN
144154

145155
for i = 0 to count1 - 1 do
146156
for j = 0 to count2 - 1 do

src/fsharp/FSharp.Core/array3.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ namespace Microsoft.FSharp.Collections
3535

3636
[<CompiledName("ZeroCreate")>]
3737
let zeroCreate (n1:int) (n2:int) (n3:int) =
38-
if n1 < 0 then invalidArg "n1" (SR.GetString(SR.inputMustBeNonNegative))
39-
if n2 < 0 then invalidArg "n2" (SR.GetString(SR.inputMustBeNonNegative))
40-
if n3 < 0 then invalidArg "n3" (SR.GetString(SR.inputMustBeNonNegative))
38+
if n1 < 0 then invalidArgInputMustBeNonNegative "n1" n1
39+
if n2 < 0 then invalidArgInputMustBeNonNegative "n2" n2
40+
if n3 < 0 then invalidArgInputMustBeNonNegative "n3" n3
4141
(# "newarr.multi 3 !0" type ('T) n1 n2 n3 : 'T[,,] #)
4242

4343
[<CompiledName("Create")>]
@@ -127,10 +127,10 @@ namespace Microsoft.FSharp.Collections
127127

128128
[<CompiledName("ZeroCreate")>]
129129
let zeroCreate (n1:int) (n2:int) (n3:int) (n4:int) =
130-
if n1 < 0 then invalidArg "n1" (SR.GetString(SR.inputMustBeNonNegative))
131-
if n2 < 0 then invalidArg "n2" (SR.GetString(SR.inputMustBeNonNegative))
132-
if n3 < 0 then invalidArg "n3" (SR.GetString(SR.inputMustBeNonNegative))
133-
if n4 < 0 then invalidArg "n4" (SR.GetString(SR.inputMustBeNonNegative))
130+
if n1 < 0 then invalidArgInputMustBeNonNegative "n1" n1
131+
if n2 < 0 then invalidArgInputMustBeNonNegative "n2" n2
132+
if n3 < 0 then invalidArgInputMustBeNonNegative "n3" n3
133+
if n4 < 0 then invalidArgInputMustBeNonNegative "n4" n4
134134
(# "newarr.multi 4 !0" type ('T) n1 n2 n3 n4 : 'T[,,,] #)
135135

136136
[<CompiledName("Create")>]
@@ -156,7 +156,7 @@ namespace Microsoft.FSharp.Collections
156156

157157

158158
[<CompiledName("Get")>]
159-
let get (array: 'T[,,,]) n1 n2 n3 n4 = array.[n1,n2,n3,n4]
159+
let get (array: 'T[,,,]) n1 n2 n3 n4 = array.[n1,n2,n3,n4]
160160

161161
[<CompiledName("Set")>]
162-
let set (array: 'T[,,,]) n1 n2 n3 n4 x = array.[n1,n2,n3,n4] <- x
162+
let set (array: 'T[,,,]) n1 n2 n3 n4 x = array.[n1,n2,n3,n4] <- x

0 commit comments

Comments
 (0)