Skip to content

Hash function returns different result in F# and C# #2103

@liboz

Description

@liboz

The Hash function returns always returns 0 for null values in C#, but in F# calling the hash function with a null value can result in a NullReferenceException.

I discovered it while working on #2096. When the GetHashCode function is small enough in size, it causes the Hash function to return 0 in debug mode and not a NullReferenceException .

Repro steps

  1. Create an F# library:
module Test

type Class1 = A |B

let test = Operators.hash (Unchecked.defaultof<Class1>)
  1. Create a C# console application with the following code:
using Microsoft.FSharp.Core;

namespace ConsoleApplication
{
    class Program
    {

        static void Main(string[] args)
        {
            Test.Class1 a = null;
            System.Console.WriteLine(Operators.Hash(a));
            System.Console.WriteLine(Test.test);
        }

    }
}
  1. Reference the F# library created in step 1 in the C# application created in step 2.

  2. Build/Run the solution in Release or Debug configuration with any bitness.

Expected behavior

The same result is printed twice.

Actual behavior

The output is 0 for the first line followed by a NullReferenceException

Known workarounds

None.

Related information

Provide any related information

  • Operating system: Windows 10
  • Branch: F# 4.0 and master
  • .NET Runtime, CoreCLR or Mono Version: .NET 4.6
  • Editing Tools (e.g. Visual Studio Version): Visual Studio 2015

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    In Progress

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions