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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…