1

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.

embee
  • 359
  • 2
  • 8
  • Incidentally your implementation of object.Equals is very interesting. Had this been a reference type I would be reporting it is bugged. – Joshua Dec 18 '16 at 21:24
  • 1
    Your repro instructions are a bit misleading and got me on the wrong track, just recommend to use the Debug build. Its behavior is very similar to [this bug](http://stackoverflow.com/a/36593473/17034), also a codegen problem and also related to sign extension and also disappears when the optimizer is enabled. Be sure to file it separately however. – Hans Passant Dec 18 '16 at 22:36
  • Interesting and all, but this doesn't appear to be a question. Unless it's implicitly "am I right". – Jeroen Mostert Dec 19 '16 at 10:22
  • Yes, it's like the question "Am I right?" – embee Dec 19 '16 at 10:52

1 Answers1

1

Bug confirmed by inspection.

I can't imagine what other answer could appear here. If the bug were unreal you would get an answer showing you where your code is wrong, but your code is not wrong.

Joshua
  • 40,822
  • 8
  • 72
  • 132