Re: your latest update:
This is a bug in the nullable arithmetic optimizer.
The nullable optimizer will remove the unnecessary conversion to int? when you do something like:
short? s = null;
int? x = s + 1;
The unoptimized codegen does the equivalent of:
short? s = null;
int? x;
int? temp = s.HasValue ? new int?((int)s.Value) : new int?();
x = temp.HasValue ? new int?(x.Value + 1) : new int?();
The optimized codegen does the equivalent of:
short? s = null;
int? x;
x = s.HasValue ? new int?((int)s.Value + 1) : new int?();
However, the optimizer contains a bug; we do not remove the unnecessary conversion for equality.
Thanks for bringing it to my attention; we’ll fix it for Roslyn. I’m actually about to write the nullable optimizer for Roslyn in the next couple of weeks.
UPDATE: I did write that optimizer, and if you are interested in how it works, I wrote a series of articles on it which starts here: