Repository navigation
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.
Milestone
Description
Two snippets below fails with NRE
- Equality
type Bar = { b: obj }
type Foo = { f: Bar | null }
let a = { f = null }
let b = { f = null }
a = bError
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
- 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 nowRelated 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]
Reacted by Friedrich von Never and gbtb
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
- StatusShow more project fieldsNew
Activity
vzarytovskii commentedon Jan 31, 2025
@T-Gro equality codegen is not accounting for the nullability now it seems, before we would've had when trying to assign
nulldirectly, you would've hadAlthough you would've hit NRE when using unchecked default value, this makes it easier to achieve.
Since now we allow
| nullfor records, nullability will be more explicit and used. Should we generate a defensive nullcheck? I think any performance hit will be negligible.Szer commentedon Jan 31, 2025
This code also fails with NRE
Added that to the first post
vzarytovskii commentedon Jan 31, 2025
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:fsharp/src/Compiler/Checking/AugmentWithHashCompare.fs
Lines 355 to 364 in 58560f8
Hashing will be somewhere around there as well.