Bitwise or operator used on a sign extended operand in Visual Studio 2015

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?

Update

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.

Jon Skeet
people
quotationmark

Sign extension is happening, but possibly not for the obvious reason, and not in a worrying way, IMO.

This code:

a |= (short) b;

is equivalent to:

// No warning here... (surprisingly, given that `a` is being sign-extended...)
a = (short) (a | (short) b);

Which is equivalent to:

// No warning here...
a = (short) ((int) a | (int) (short) b;

because the | operator isn't defined for short operands anyway. Both operands are promoted to int, then the result is cast back to short.

It's not clear why the compiler decides to warn in this case, but there is sign extension occurring... albeit in a harmless way.

Note that you get the same warning if no int variables are involved at all:

short a = 10;
short b = 20;
a |= b; // CS0675

Given the way that the | operator works with casts, this looks entirely harmless to me. I'm not sure whether I'd call it a compiler bug, but it definitely looks like non-ideal behaviour to me. Will ping C# compiler team members to see what I've missed :)

people

See more on this question at Stackoverflow