Skip to content

Commit d7f6732

Browse files
fix(array): make the Array.length null exception containing the param… (#10337)
1 parent 07e12af commit d7f6732

File tree

3 files changed

+43
-42
lines changed

3 files changed

+43
-42
lines changed

src/fsharp/FSharp.Core/array.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ namespace Microsoft.FSharp.Collections
2323
let inline indexNotFound() = raise (KeyNotFoundException(SR.GetString(SR.keyNotFoundAlt)))
2424

2525
[<CompiledName("Length")>]
26-
let length (array: _[]) = array.Length
26+
let length (array: _[]) =
27+
checkNonNull "array" array
28+
array.Length
2729

2830
[<CompiledName("Last")>]
2931
let inline last (array: 'T[]) =

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@ type ArrayModule2() =
4444

4545
// null array
4646
let nullArr = null:string[]
47-
CheckThrowsNullRefException (fun () -> Array.length nullArr |> ignore)
47+
CheckThrowsArgumentNullException (fun () -> Array.length nullArr |> ignore)
4848

49+
// null array, argument name showing up
50+
try
51+
Array.length nullArr |> ignore
52+
with
53+
| :? ArgumentNullException as e -> Assert.Equal("array", e.ParamName) |> ignore
54+
4955
()
5056

5157
[<Fact>]
Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11

2-
// Microsoft (R) .NET Framework IL Disassembler. Version 4.6.1055.0
2+
// Microsoft (R) .NET Framework IL Disassembler. Version 4.8.3928.0
33
// Copyright (c) Microsoft Corporation. All rights reserved.
4-
5-
6-
74
// Metadata version: v4.0.30319
85
.assembly extern mscorlib
96
{
@@ -13,82 +10,78 @@
1310
.assembly extern FSharp.Core
1411
{
1512
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) // .?_....:
16-
.ver 4:4:1:0
13+
.ver 5:0:0:0
1714
}
1815
.assembly ZeroToArrLength02
1916
{
2017
.custom instance void [FSharp.Core]Microsoft.FSharp.Core.FSharpInterfaceDataVersionAttribute::.ctor(int32,
2118
int32,
2219
int32) = ( 01 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 )
23-
2420
// --- The following custom attribute is added automatically, do not uncomment -------
2521
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 01 00 00 00 00 00 )
26-
2722
.hash algorithm 0x00008004
2823
.ver 0:0:0:0
2924
}
3025
.mresource public FSharpSignatureData.ZeroToArrLength02
3126
{
32-
// Offset: 0x00000000 Length: 0x000001E0
27+
// Offset: 0x00000000 Length: 0x0000020E
3328
}
3429
.mresource public FSharpOptimizationData.ZeroToArrLength02
3530
{
36-
// Offset: 0x000001E8 Length: 0x0000007B
31+
// Offset: 0x00000218 Length: 0x0000007B
3732
}
3833
.module ZeroToArrLength02.dll
39-
// MVID: {59B18AEE-A36B-03A7-A745-0383EE8AB159}
34+
// MVID: {5F9557F9-A36B-03A7-A745-0383F957955F}
4035
.imagebase 0x00400000
4136
.file alignment 0x00000200
4237
.stackreserve 0x00100000
4338
.subsystem 0x0003 // WINDOWS_CUI
4439
.corflags 0x00000001 // ILONLY
45-
// Image base: 0x01550000
46-
47-
40+
// Image base: 0x04D90000
4841
// =============== CLASS MEMBERS DECLARATION ===================
49-
5042
.class public abstract auto ansi sealed ZeroToArrLength02
5143
extends [mscorlib]System.Object
5244
{
5345
.custom instance void [FSharp.Core]Microsoft.FSharp.Core.CompilationMappingAttribute::.ctor(valuetype [FSharp.Core]Microsoft.FSharp.Core.SourceConstructFlags) = ( 01 00 07 00 00 00 00 00 )
5446
.method public static void f1(int32[] arr) cil managed
5547
{
56-
// Code size 23 (0x17)
48+
// Code size 34 (0x22)
5749
.maxstack 5
58-
.locals init ([0] int32 i)
50+
.locals init ([0] int32 V_0,
51+
[1] int32 i)
5952
.language '{AB4F38C9-B6E6-43BA-BE3B-58080B2CCCE3}', '{994B45C4-E6E9-11D2-903F-00C04FA302A1}', '{5A869D0B-6611-11D3-BD2A-0000F80849BD}'
60-
.line 6,6 : 5,41 'C:\\GitHub\\dsyme\\visualfsharp\\tests\\fsharpqa\\Source\\Optimizations\\ForLoop\\ZeroToArrLength02.fs'
53+
.line 6,6 : 5,41 'C:\\Users\\Michelle\\Desktop\\Personal\\Repos\\fsharp\\tests\\fsharpqa\\source\\Optimizations\\ForLoop\\ZeroToArrLength02.fs'
6154
IL_0000: ldc.i4.0
62-
IL_0001: stloc.0
63-
IL_0002: br.s IL_0010
64-
55+
IL_0001: stloc.1
56+
IL_0002: ldarg.0
57+
IL_0003: call int32 [FSharp.Core]Microsoft.FSharp.Collections.ArrayModule::Length<int32>(!!0[])
58+
IL_0008: ldc.i4.1
59+
IL_0009: sub
60+
IL_000a: stloc.0
61+
IL_000b: ldloc.0
62+
IL_000c: ldloc.1
63+
IL_000d: blt.s IL_0021
6564
.line 7,7 : 9,21 ''
66-
IL_0004: ldarg.0
67-
IL_0005: ldloc.0
68-
IL_0006: ldloc.0
69-
IL_0007: stelem [mscorlib]System.Int32
70-
IL_000c: ldloc.0
71-
IL_000d: ldc.i4.1
72-
IL_000e: add
73-
IL_000f: stloc.0
65+
IL_000f: ldarg.0
66+
IL_0010: ldloc.1
67+
IL_0011: ldloc.1
68+
IL_0012: stelem [mscorlib]System.Int32
69+
IL_0017: ldloc.1
70+
IL_0018: ldc.i4.1
71+
IL_0019: add
72+
IL_001a: stloc.1
7473
.line 6,6 : 5,41 ''
75-
IL_0010: ldloc.0
76-
IL_0011: ldarg.0
77-
IL_0012: ldlen
78-
IL_0013: conv.i4
79-
IL_0014: blt.s IL_0004
80-
81-
IL_0016: ret
74+
IL_001b: ldloc.1
75+
IL_001c: ldloc.0
76+
IL_001d: ldc.i4.1
77+
IL_001e: add
78+
IL_001f: bne.un.s IL_000f
79+
IL_0021: ret
8280
} // end of method ZeroToArrLength02::f1
83-
8481
} // end of class ZeroToArrLength02
85-
8682
.class private abstract auto ansi sealed '<StartupCode$ZeroToArrLength02>'.$ZeroToArrLength02
8783
extends [mscorlib]System.Object
8884
{
8985
} // end of class '<StartupCode$ZeroToArrLength02>'.$ZeroToArrLength02
90-
91-
9286
// =============================================================
93-
9487
// *********** DISASSEMBLY COMPLETE ***********************

0 commit comments

Comments
 (0)