Skip to content

Null reference on equality with nested record #18286

Closed

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]

Activity

  1. added this to the Backlog milestone on Jan 31, 2025
  2. vzarytovskii commented on Jan 31, 2025

    @vzarytovskii
    Member

    @T-Gro equality codegen is not accounting for the nullability now it seems, before we would've had when trying to assign null directly, you would've had

        error FS0043: The type 'Bar' does not have 'null' as a proper value
        error FS0043: The type 'Bar' does not have 'null' as a proper value

    Although you would've hit NRE when using unchecked default value, this makes it easier to achieve.

    Since now we allow | null for records, nullability will be more explicit and used. Should we generate a defensive nullcheck? I think any performance hit will be negligible.

  3. Szer commented on Jan 31, 2025

    @Szer
    ContributorAuthor

    This code also fails with NRE

    type Bar = { b: obj }
    type Foo = { f: Bar | null }
    let a = { f = null }
    a.GetHashCode()
    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
    

    Added that to the first post

  4. self-assigned this
    on Jan 31, 2025
  5. vzarytovskii commented on Jan 31, 2025

    @vzarytovskii
    Member

    If someone wants to fix it, it's a really easy one, for equality, there needs to be a tester added for each field, before .Equals(that) call is generated, around here (around when we mapping over the fields), and made a part of expression:

    let expr = mkEqualsTestConjuncts g m (List.map mkTest fields)
    let expr =
    if tycon.IsStructOrEnumTycon then
    expr
    else
    mkBindThatNullEquals g m thise thataddre expr
    let thatv, expr = mkThatVarBind g m ty thataddrv expr
    thisv, thatv, expr

    Hashing will be somewhere around there as well.

  6. added
    Impact-Medium
    Area-Compiler-CodeGen
    and removed on Feb 3, 2025
  7. modified the milestones: Backlog, February 2025 on Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area-Compiler-CodeGenBugImpact-Medium

Type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions