Skip to content

Null reference on equality with nested record #18286

@Szer

Description

@Szer

Two snippets below fails with NRE

  1. Equality
type Bar = { b: obj }
type Foo = { f: Bar | null }
let a = { f = null }
let b = { f = null }
a = b

Error

System.NullReferenceException: Object reference not set to an instance of an object.
   at FSI_0045.Foo.Equals(Foo obj, IEqualityComparer comp)
   at <StartupCode$FSI_0048>.$FSI_0048.main@()
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
Stopped due to error
  1. Hash code
type Bar = { b: obj }
type Foo = { f: Bar | null }
let a = { f = null }
a.GetHashCode()

Error

System.NullReferenceException: Object reference not set to an instance of an object.
   at FSI_0017.Foo.GetHashCode(IEqualityComparer comp)
   at FSI_0017.Foo.GetHashCode()
   at <StartupCode$FSI_0019>.$FSI_0019.main@()
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
Stopped due to error

Known workarounds

change Bar to be NON record

type Bar = class end
type Foo = { f: Bar | null }
let a = { f = null }
let b = { f = null }
a = b // works now

Related information

  • Windows 10
  • Microsoft (R) F# Interactive version 12.9.101.0 for F# 9.0
dotnet --list-sdks
5.0.408 [C:\Program Files\dotnet\sdk]
6.0.203 [C:\Program Files\dotnet\sdk]
6.0.321 [C:\Program Files\dotnet\sdk]
6.0.428 [C:\Program Files\dotnet\sdk]
7.0.120 [C:\Program Files\dotnet\sdk]
7.0.203 [C:\Program Files\dotnet\sdk]
7.0.317 [C:\Program Files\dotnet\sdk]
7.0.410 [C:\Program Files\dotnet\sdk]
8.0.112 [C:\Program Files\dotnet\sdk]
8.0.308 [C:\Program Files\dotnet\sdk]
8.0.405 [C:\Program Files\dotnet\sdk]
9.0.102 [C:\Program Files\dotnet\sdk]

Metadata

Metadata

Assignees

Labels

Area-Compiler-CodeGenIlxGen, ilwrite and things at the backendBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

Type

Projects

Status

New

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions