While porting a 32bit managed application to 64bit I've observed a strange behavior by a Equals() override within a struct.
You find the a repro at github.
To reproduce the bug, you should compile the library with "optimize" flag on. This is default on the Release config. The consuming TestApp must be compiled without any optimization. Prefer 32 bit must be disabled to start as 64bit App. See notes on github!
The library contains a struct that implements the IEquatable interface which is implemented with a simple line of code.
public bool Equals(StructWithValue other)
{
return value.Equals(other.value);
}
This code calls the Equals method of the ushort/UInt16 type. If you build the solution with the proposed configuration, all values above 32767 will fail. You call Equal on a ushort value of 32768 and the value of 'other' is also 32768. But Equals() will return false for all values above 32767.
If you change the method to use the '==' operator the code will work. Also if you change the type from struct to class the the code runs as expected.
public bool Equals(StructWithValue other)
{
return value == othervalue;
}
I think this is a bug in the RyuJIT-Compiler. If I use the legacy JIT-Compiler the code works fine.
Tested with Visual Studio 2015 and TargetFramework 4.6.2 on different windows versions.