I just tried installing Visual Studio 2015, and when trying to compile an old project, I got the warning
CS0675 Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first
for a piece of code that does not give the same warning when compiling in Visual Studio 2013. I found out that all it takes to reproduce is this very simple code:
short a = 0;
int b = 0;
a |= (short)b;
Now, I have read this SO question, I have read Eric Lippert's blog post on this issue, and I quickly read up on sign extension, but my understanding is that sign extension happens when you cast from a signed number type consisting of a smaller number of bits to one with a larger number of bits, such as short
to int
for example.
But since I'm casting from an int
to a short
, no sign extension should happen if I'm not mistaken. The fact that this does not issue a warning in earlier versions of Visual Studio, it leads me to believe that this must be a bug in the Visual Studio 2015 compiler (Roslyn). Am I misunderstanding how sign extension and/or the compiler works here, or is this most likely a compiler bug?
Jon Skeet pointed out that there actually is indeed a sign extension happening since the |
operator isn't defined for short
and so there's an implicit cast to int
before the result is cast back to short
again. However, the compiler shouldn't have issued this warning since the cast is harmless. There was a bug in the Roslyn compiler as pointed out in the accepted answer.
This is just a bug. The code to detect and report this error was added very late in the VS2015 development (see https://github.com/dotnet/roslyn/issues/909 and https://github.com/dotnet/roslyn/pull/2416) and it detects too many cases compared to VS2013. There is now a bug report (https://github.com/dotnet/roslyn/issues/4027) to repair this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With