Skip to content

Compiler creates TOO MUCH msil code in GetHashCode : IEqualityComparer -> int #1366

@Unknown6656

Description

@Unknown6656

(moved from dotnet/roslyn#12634)
I do not know, whether the issue does apply to newer F# compiler versions, but this is what I found:

The F# compiler v12 creates WAY TOO MUCH MSIL code in the overridden method
GetHashCode : IEqualityComparer -> int in F# enumerations.

Version Used:
Microsoft Visual Studio Ultimate 2013 Version 12.0.40629.00 Update 5, 64Bit
Microsoft .NET Framework Version 4.5.51209
F# Compiler Version 12.0.30815.0

Steps to Reproduce:

  1. Create a (big) enum, e.g.:
type MathematicalOperators =
    | OR | AND | XOR | SHL | SHR | ROL | ROR | ADD | SUB | MOD | DIV | MUL | POW | EQ | NEQ | LOW | LEQ | GRT | GEQ
  1. Compile 😉
  2. Inspect the assembly using a decompiler (e.g. dnSpy or ILSpy).

Actual Behavior:
You will see a big chunk of IL code inside the method MathematicalOperators.GetHashCode : IEqualityComparer -> int.
I will not post the IL code, as it is much to long, I will however post the corresponding C# interpretation, as it is pretty accurate:

if (this != null)
{
    switch (this.Tag)
    {
    case 0:
        return 0;
    case 1:
        return 1;
    case 2:
        return 2;
    case 3:
        return 3;
    case 4:
        return 4;
    case 5:
        return 5;
    case 6:
        return 6;
    case 7:
        return 7;
    case 8:
        return 8;
    case 9:
        return 9;
    case 10:
        return 10;
    case 11:
        return 11;
    case 12:
        return 12;
    case 13:
        return 13;
    case 14:
        return 14;
    case 15:
        return 15;
    case 16:
        return 16;
    case 17:
        return 17;
    case 18:
        return 18;
    }
    return 0;
}
return 0;

Expected Behavior:
It is needless to say, that I expected less compiled code. The following lines would just do it fine:

if (this == null) // rather useless, but whatever
    return 0;
else
    return this.Tag;

Or did I somehow miss some kind of feature/joke/...?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions