Bug 2466 – -w error for homogeneous arithmetic is too stringent
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2008-11-21T13:11:00Z
Last change time
2014-03-01T00:35:57Z
Keywords
diagnostic
Assigned to
bugzilla
Creator
schveiguy
Comments
Comment #0 by schveiguy — 2008-11-21T13:11:17Z
The following code produces four warnings with -w:
void foo()
{
char c;
short s;
byte b;
int i;
c = c + c;
s = s + s;
b = b + b;
i = i + i;
s = c + c;
}
outputs:
warning - testbug.d(8): Error: implicit conversion of expression (cast(int)c + cast(int)c) of type int to char can cause loss of data
warning - testbug.d(9): Error: implicit conversion of expression (cast(int)s + cast(int)s) of type int to short can cause loss of data
warning - testbug.d(10): Error: implicit conversion of expression (cast(int)b + cast(int)b) of type int to byte can cause loss of data
warning - testbug.d(13): Error: implicit conversion of expression (cast(int)c + cast(int)c) of type int to short can cause loss of data
This is inconsistent. If the warning system is warning about adding two of the same type together might produce loss of data, yes that could happen, but why not the same warning for integers? I can certainly add two ints together and lose data if I don't assign it to a long. And if that warning was added, the warning system would flag way too much code.
Also, the s = c + c; producing a loss of data warning is plain wrong.
I think if you are adding two of the same type together, and assigning it to the same type or larger type, it should compile without warnings.
Comment #1 by jarrett.billingsley — 2008-11-21T13:28:04Z
Yeah, I'm pretty sure this is a dup.
*** This bug has been marked as a duplicate of 1977 ***