void main(){
ushort a = 1;
ushort b = 2;
ushort c = a|b;
}
dmd -w bug.d
warning - bug.d(5): Error: implicit conversion of expression (cast(int)a | cast(
int)b) of type int to ushort can cause loss of data
-------
This is ridiculous. Logical operations on two operands of the same size should not be implicitly converted. The code above is _never_ a bug.
This is particularly bad because
int c = a|b;
does _not_ generate a warning, even though it involves a change from unsigned to signed, _and_ a change of size, and is therefore likely to be a bug!
Comment #1 by paching — 2008-09-26T23:53:17Z
I've been bitten by this too. Simple operations like assigning the sum of two shorts to another short give warnings; I've had to shut them off as a workaround.
Comment #2 by andrei — 2008-09-27T00:18:09Z
(In reply to comment #1)
> I've been bitten by this too. Simple operations like assigning the sum of two
> shorts to another short give warnings; I've had to shut them off as a
> workaround.
I think the sum of any integrals smaller than int/uint should result in int/uint, so that warning is legit in that case. The reason is a mix of being principled (operations should avoid overflow) and being practical (int won't spill into long). Bitwise operations should indeed preserve size.