Bug 1257 – Erroneous warning for ushort = (ushort | ushort)

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-06-05T01:55:00Z
Last change time
2014-02-16T15:25:29Z
Keywords
rejects-valid
Assigned to
bugzilla
Creator
clugdbug

Comments

Comment #0 by clugdbug — 2007-06-05T01:55:28Z
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.
Comment #3 by bugzilla — 2008-12-08T03:39:27Z
It doesn't give those warnings anymore.
Comment #4 by smjg — 2008-12-08T05:51:03Z
See also issue 1977.